用rsync 命令实现linux 跨服务器差分备份,不需要输入密码

来源:互联网 发布:手机修改图片软件 编辑:程序博客网 时间:2024/06/04 18:20


测试:本地机器:10.100.2.51 远程机器:10.100.2.50

目的:
测试环境:
把本地服务器的test2文件夹的东西移动到远程服务器的test2里面

实现方法:
1, [本地机器] 用ssh-keygen生成密钥
[root@SHTFMGDBS01 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:
*********************************** root@S*********
The key's randomart image is:
+--[ RSA 2048]----+
| .o+.o=+*|
| ..o. = |
| .o ...+|
| . .o . oo=|
| S. = |
| .E o |
| . |
| |
| |
+-----------------+
2,[本地机器] 将本地的公钥传送到远端机器root帐户的.ssh目录下
scp id_rsa.pub root@10.100.2.50:server_1
#mv server_1 .ssh/
cd .ssh/
#cat server_1 >> authorized_keys

3,[本地机器] 创建同期shell
vim diff_backup.sh

测试环境
#!/bin/sh
echo 開始:$(date +%Y%m%d%H%M%S) >> ./filebklog
rsync -auv -e"ssh -i /root/.ssh/id_rsa" ./test2/ root@10.100.2.50:/test2/
echo 完了:$(date +%Y%m%d%H%M%S) >> ./filebklog

4、报错对应
如果提示0755错误
Permissions 0755 for ‘/etc/ssh/id_rsa’ are too open.
执行下面的语句
chmod 0644 *
chmod 0600 id_rsa

5,创建任务
设置定时执行任务:
crontab -e

测试环境
每隔一分钟执行一次/root/diff_backup.sh
*/1 * * * * /root/diff_backup.sh

每天凌晨3点执行/root/diff_backup.sh
0 3 * * * /root/diff_backup.sh

esc
wq

重启定时命令:
service crond restart

查看当前用户的任务
crontab -l

查看执行log,
cat filebklog

查看系统log
tail -n 50 ~/../var/spool/mail/root

乐亿欧原创,禁止转载

原创粉丝点击