环境:
192.168.91.103 数据源服务器 192.168.18.220 异地备份服务器
1.在192.168.91.103服务器生成秘钥文件
[root@Lenovobox3 file]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): #回车 Enter passphrase (empty for no passphrase): #输入当前系统的密码 Enter same passphrase again: #重复 Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: ......
2.将公钥文件下发至异地备份服务器
[root@Lenovobox3 file]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.18.220 The authenticity of host '192.168.18.220 (192.168.18.220)' can't be established. ECDSA key fingerprint is e1:98:4f:fd:c9:12:4d:4a:c8:b5:a5:c5:77:0c:65:ea. Are you sure you want to continue connecting (yes/no)? y Please type 'yes' or 'no': y Please type 'yes' or 'no': yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.18.220's password: #输入异地备份服务器密码 Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.18.220'" and check to make sure that only the key(s) you wanted were added.
3.进行备份测试,当前IP是192.168.91.103
[root@Lenovobox3 file]# ls backup_test.txt [root@Lenovobox3 file]# pwd /root/file
4.连接至异地备份服务器192.168.18.220,执行备份
[root@localhost ~]# rsync -avzp -e 'ssh -p 22' root@192.168.91.103:/root/file/backup_test.txt /data receiving incremental file list backup_test.txt sent 30 bytes received 90 bytes 240.00 bytes/sec total size is 5 speedup is 0.04
5.验证备份数据
[root@localhost ~]# cd /data/ [root@localhost data]# ls backup_test.txt [root@localhost data]# cat backup_test.txt test
6.设置备份脚本
[root@localhost data]# vim backup.sh rsync -avzp -e 'ssh -p 22' --delete root@192.168.91.103:/data/ball/dirmap/database /data/103
7.设置定时任务
[root@localhost data]# crontab -e 0 0 * * 6 /data/backup.sh > /data/log/log$(date +\%Y\%m\%d).txt