Linux下实现Rsync目录同步备份

时间:2021-06-01 11:31:11

需求:对于开发机器做目录的数据备份

测试机IP:192.168.1.100   WEB目录:/bckup/

下面我将用一台机器来备份上面测试机 /bckup下的所有数据,并实现时时同步

备份机器IP:192.168.1.101    备份到目录/bckup下     (需要创建目录  mkdir -p /bckup

安装  192.168.1.101 需要操作如下:

rsync-2.6.8.tar.gz 包下载到本机 http://pan.baidu.com/s/1jIEzwxw

[root@cdn ~]#tar -zxvf rsync-2.6.8.tar.gz 
rsync-2.6.8/
rsync-2.6.8/COPYING
rsync-2.6.8/Doxyfile
rsync-2.6.8/INSTALL
rsync-2.6.8/Makefile.in
rsync-2.6.8/NEWS
rsync-2.6.8/OLDNEWS
rsync-2.6.8/README
rsync-2.6.8/TODO

  

[root@cdn ~]# cd rsync-2.6.8

[root@cdn rsync-2.6.8]# ./configure 

[root@cdn rsync-2.6.8]# make && make install

 

  

配置 vi /etc/rsyncd.conf
 

uid = nobody
gid = nobody

use chroot = yes
max connections = 4
pid file= /var/run/rsyncd.pid
lock file= /var/run/rsync.lock
log file= /var/log/rsyncd.log

[downloads]
path = /bckup/     //同步的目录
ignore errors
readonly = false
list = false
hosts allow = 192.168.1.100/24  //允许的Ip
hosts deny = 0.0.0.0/32  //拒绝的IP  0.0.0.0代表无黑名单用户ip
auth users= root   //用户
启动服务并加入开机自启
/usr/bin/rsync --daemon

 echo "/usr/bin/rsync --daemon" >>/etc/rc.local 

192.168.1.100 实现同步脚本

[root@cdn etc]# rsync -vrtL --progress  /bckup/*  root@192.168.1.101:/bckup/

The authenticity of host '192.168.1.101 (192.168.1.101)' can't be established.

RSA key fingerprint is c1:c3:e2:7e:e6:bb:e7:1a:42:5d:02:9d:35:6e:6a:c8.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.1.101' (RSA) to the list of known hosts.

root@192.168.1.101's password: 

  

 

----直接执行的话会有一个繁琐的问题,就是需要每次都输入密码,但如果放在shell脚本中实现自动同步是无法实现的,所以需要做一个SSH密钥登录功能

[root@cdn etc]# ssh-keygen -t rsa   此命令会有一些提示,全部选择默认确定即可

[root@cdn etc]# cat /root/.ssh/id_rsa.pub 

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAvpnDDCEUHeN6AWjkxue60ol0z4cLfXkmn6SXr1rRd2le45nOh7idCCF9B3+cOkLaA/IlRejBas5E7zodnyFIpad8X5Cj0C9vc31a1Wo33y6vo7KHsTvHsLBE4PcXjxkSN9fjmNekzOMNkvCne4bq1h9zvkvocuigTjUkQ1i9TG3Tg3DZ2lA5X8/jQFoebA9hX2WMCiwJNc5g0rhhtoQuQ8yYVd5fnzzvbcnXTLE/CB69UefkmVSvUkzsYd+PcNHbGVGpivO7jk2eR8c7WKl39kJ3LbqKHNL4rayuXpSCtvfKiwlkFjzxDYZ2ubAUreru8B/YQyJikEypNhq4IzTZUQ== root@cdn.oss.letv.com

[root@cdn etc]# 

  

复制自己创建的密钥,放到  192.168.1.101  /root/.ssh/authorized_keys 目录内  实现登录无需密码登录。

将同步命令写入计划任务,定期执行,即可实现自动同步。