rsync 免密码同步文件

两台服务器
A:192.168.1.106
B:192.168.1.109

用ssh证书方式实现rsync免密码传输文件

服务器A(Server:192.168.1.106):

[root@localhost ~]# useradd rsync
[root@localhost ~]# passwd rsync
[root@localhost ~]# mkdir /Backup
[root@localhost ~]# chown rsync:root -R /Backup

服务器B(Client:192.168.1.109):

[root@localhost ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/home/rsync/.ssh/id_rsa): 
Created directory '/home/rsync/.ssh'.
Enter passphrase (empty for no passphrase): #直接回车,不用输入密码
Enter same passphrase again:                #同上
Your identification has been saved in /home/rsync/.ssh/id_rsa.
Your public key has been saved in /home/rsync/.ssh/id_rsa.pub.
The key fingerprint is:
3d:1b:4d:68:5a:6a:c9:3a:e0:f2:35:c5:8b:5f:62:9d rsync@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|           .     |
|          + .    |
|       o B o     |
|    .   S + .    |
|   . . = o =     |
|  . . * + E      |
|   o . = o       |
|    .   .        |
+-----------------+
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub rsync@192.168.1.106
The authenticity of host '192.168.1.106 (192.168.1.106)' can't be established.
RSA key fingerprint is ab:d0:88:48:a9:b4:31:3d:fd:a0:1a:23:72:28:c1:2c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.106' (RSA) to the list of known hosts.
rsync@192.168.1.106's password: 
Now try logging into the machine, with "ssh '192.168.1.106'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@localhost ~]# rsync -avz -e ssh rsync@192.168.1.106:/Backup/* .

拓展:

实现mysql异地自动备份,每天两小时备份一次,同时备份文件保留10天

还是上面两台服务器

服务器A(mysql):

[root@localhost ~]# vim rsync_mysqlBackup.sh
[root@localhost ~]# cat rsync_mysqlBackup.sh 
#!/bin/bash
#备份数据库
mysqldump -uroot -ppassword test >/data/Backup/mysql/`data +%Y%m%d_%H%M%S`mysqlBack.sql
#删除10天前的备份数
find /data/Backup/mysql/ -type f -name "*.sql" -mtime +10 -exec rm -rf {} \ ;

[root@localhost ~]# chmod u+x rsync_mysqlBackup.sh

计划任务crontab

[root@localhost ~]# rpm -qa |grep cron
cronie-1.4.4-12.el6.x86_64
crontabs-1.10-33.el6.noarch
cronie-anacron-1.4.4-12.el6.x86_64
[root@localhost ~]#   
  • 如果没有则需要安装
    yum -y install vixie-cron
    yum -y install crontabs

  • 说明:
    vixie-cron 软件包是 cron 的主程序;
    crontabs 软件包是用来安装、卸装、或列举用来驱动 cron 守护进程的表格的程序。


[root@localhost ~]# crontab -e
[root@localhost ~]# crontab -l
* 0-23/2 * * * . /bin/bash /root/rsync_mysqlBackup.sh
[root@localhost ~]# chkconfig --level 35 crond on
[root@localhost ~]# service crond reload
[root@localhost ~]# crontab -l
* 0-23/2 * * * . /bin/bash /root/rsync_mysqlBackup.sh
[root@localhost ~]# 

服务器B(Backup):

[root@localhost ~]# vim back-mysql.sh
[root@localhost ~]# cat back-mysql.sh 
#!/bin/bash
#获取备份数据,前面已经实现rsync免密码同步了
rsync -avz -e ssh rsync@192.168.1.106:/data/Backup/mysql/* /Backup/
#删除10天前的数据
find /data/Backup/mysql -type f -name "*.sql" -mtime +10 -exec rm -rf {} \;
[root@localhost ~]# chmod u+x back-mysql.sh 
[root@localhost ~]# crontab -e
crontab: installing new crontab
[root@localhost ~]# crontab -l
* 0-23/2 * * * . /bin/bash /root/back-mysql.sh
[root@localhost ~]# service crond reload
重新载入 crond:                                           [确定]
[root@localhost ~]# chkconfig --level 35 crond on
[root@localhost ~]# 
comments powered by Disqus