应用市场 服务中心 加入收藏

让您轻松构建企业级的网站运行环境

基于docker容器技术,支持主流Linux系统,一键安装,自动化部署,内置丰富应用市场,完美兼容,关系融洽不互殴!

在crontab中利用scp命令自动备份文件

把你的本地主机用户的ssh公匙文件写入到远程主机用户的~/.ssh/authorized_keys文件中,具体方法是 假设本地主机localhost,远程主机remote

一:先屏蔽scp命令调用时出现的口令提示,在localhost主机里的用户

运行 ssh-keygen -t rsa
结果如下
Generating public/private rsa key pair.

Enter file in which to save the key (/home/.username/ssh/id_rsa):#回车

Enter passphrase (empty for no passphrase):#回车

Enter same passphrase again:#回车

Your identification has been saved in /home/.username /.ssh/id_rsa.

Your public key has been saved in /home/.username /.ssh/id_rsa.pub.

The key fingerprint is:

38:25:c1:4d:5d:d3:89:bb:46:67:bf:52:af:c3:17:0c username@localhost

Generating RSA keys:

Key generation complete.
会在用户目录~/.ssh/产生两个文件,id_rsa,id_rsa.pub

二:把id_rsa.pub文件拷贝到remote主机的用户目录下 然后再运行如下命令

cat id_rsa.pub >~/.ssh/authorized_keys
它将在/root/.ssh/目录下面生成authorized_keys这个文件
完成以后,在命令提示符下面输入SCP命令测试一下
如:scp /opt/dbback/data/aa.dmp.zip root@10.10.29.1:/opt/dbback/
手工测试的时候,系统会提示你Are you sure you want to continue connecting (yes/no)? yes 我在这里输入yes 回车以后,系统就开始拷贝文件

如果在手工测试过程中出现以下错误:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
89:37:d4:6d:06:aa:3b:be:70:68:ca:69:d5:39:a6:68.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending key in /root/.ssh/known_hosts:1
RSA host key for 203.158.16.11 has changed and you have requested strict checking.
Host key verification failed.
lost connection

则请删除本地机上面/root/.ssh/known_hosts这个文件即可

手工测试一下以后,我们在脚本中用SCP来拷贝的话,就可以自动拷贝,而不需要人工来干预了。

三:编写导出数据并压缩后下载到另外一台服务器的脚本程序:
#!/bin/bash
export DISPLAY=localhost:0.0
export ORACLE_BASE=/u01/app
export ORACLE_HOME=/u01/app/oracle/product/10.2.0/db-1
export PATH=$ORACLE_HOME/bin:$PATH
export ORACLE_SID=orcl
export ORACLE_TERM=xterm
export LD_ASSUME_KERNEL=2.4.1
export ORA_NLS33=$ORACLE_HOME/nls/data
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
export PATH=$ORACLE_HOME/bin:$PATH
export NLS_LANG=american_america.ZHS16GBK
backupfile=sentree`date "+%y%m%d"`.dmp
/u01/app/oracle/product/10.2.0/db-1/bin/exp usrname/pass@orcl file=/opt/dbback/data/$backupfile
/bin/gzip -S .zip /opt/dbback/data/$backupfile
copyfile=sentree`date "+%y%m%d"`.dmp.zip
/usr/bin/scp /opt/dbback/data/$copyfile root@10.39.201.0:/opt/dbback/

四:在crontab中加入这个脚本程序,让它在一定的时间自动运行:

编辑/etc/crontab文件
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
0 6 * * * root /opt/dbback/oraautoback.sh
最后一行才是我们自己加入的,表示在早上六点运行这个脚本程序
这样处理以后,对于数据的安全方面我们就多了一层保护。

上一篇:linux解压 tar命令
下一篇:CentOS常用命令

点击这里在线咨询  

在线咨询
分享按钮