rsync部署使用
业务描述
现有A、B、C....N台主机。实现对A机器上path_A目录的指定文件进行备份,备份到B、C、D...等机器的path_X路径。现使用rsync实现文件的拷贝,A为服务端,其他机器为客户端,客户端执行定时任务将文件拉取到本地。 通过rsync可以结合crontab实现按时自动备份,在远程自动同步的场景下,rsync 需要以守护进程的方式来运行。 客户端和服务端都要安装rsync,安装完后有些系统不会生成rsyncd.conf,需要自己创建在/etc/rsync.d/rsyncd.conf
Server端安装
rpm -qa rsync // 检查是否安装了rsync yum install rsync -y mkdir -p /etc/rsync.d touch /etc/rsync.d/rsyncd.conf // rsync服务端配置文件 touch /etc/rsync.d/rsyncd.pass // 客户端拉取文件时使用的用户密码 chmod 600 /etc/rsync.d/rsync.conf chmod 600 /etc/rsync.d/rsync.pass
vim /etc/rsync.d/rsyncd.conf log file=/var/log/rsyncd.log pid file=/var/run/rsyncd.pid lock file=/var/run/rsyncd.lock uid=root gid=root max connections=5 hosts allow=10.10.11.21/10.10.11.22 hosts deny=* secrets file = /etc/rsync.d/rsyncd.pass read only = yes use chroot = yes ignore errors = yes list = true #write only = no [nipt] path=/zonghe/sharedisk/sharedisk/word/niptword/ comment= this is lab nipt rpt sync exclude= 2016 2017 2018/01 2018/02 2018/03 2018/04 2018/05 auth users=rsync [niptplus] path=/zonghe/sharedisk/sharedisk/plus comment= this is lab niptplus rpt sync exclude= 2016 2017 2018/01 2018/02 2018/03 2018/04 2018/05 FACT images old TEMP template template.rar auth users=rsync
vim /etc/rsync.d/rsyncd.pass # 用户名密码自定义 rsync:rsync
rsync --daemon --config=/etc/rsync.d/rsyncd.conf
至此,服务端安装启动完毕。
Client端安装
rpm -qa rsync // 检查是否安装了rsync yum install rsync -y mkdir -p /etc/rsync.d touch /etc/rsync.d/rsync.pass touch /etc/rsync.d/include.list chmod 600 /etc/rsync.d/rsync.pass
vim /etc/rsync.d/rsyncd.pass # 配置密码即可,调用命令中指定用户名 rsync
vim /etc/rsync.d/rsyncd.pass # 配置了只想同步哪些文件或目录,这里配置的是只同步pdf文件 + */ + *.pdf - *
调用脚本
mkdir -p /usr/local/test_rsync rsync -avzP ––include-from=/etc/rsync.d/include.list rsync@10.11.11.11::nipt /usr/local/test_rsync/nipt --password-file=/etc/rsync.d/rsyncd.pass rsync -avzP ––include-from=/etc/rsync.d/include.list rsync@10.11.11.11::niptplus /usr/local/test_rsync/niptplus --password-file=/etc/rsync.d/rsyncd.pass
加入crontab
crontab -e*/1 * * * * rsync -av ––include-from=/etc/rsync.d/include.list rsync@10.100.11.33::nipt /usr/local/test_rsync/nipt --password-file=/etc/rsync.d/rsyncd.pass*/1 * * * * rsync -av ––include-from=/etc/rsync.d/include.list rsync@10.100.11.33::niptplus /usr/local/test_rsync/niptplus --password-file=/etc/rsync.d/rsyncd.pass
rsync服务端关闭启动命令
ps -ef | grep rsynckill -9 pidrm -f /var/run/rsyncd.pidrsync --daemon --config=/etc/rsync.d/rsyncd.conf
配置文件与客户端命令参考: