Rsync
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rsync相关的知识,希望对你有一定的参考价值。
记一次线上数据同步 不用模块和秘钥文件,通过两台服务器之间做互信的方式在同步文件。 首先拷贝发送端的公钥到接收端上面
# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory ‘/root/.ssh‘. 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: eb:1d:9a:b9:d8:42:ab:2e:7c:31:eb:7d:24:69:81:a4 [email protected] The key‘s randomart image is: +--[ RSA 2048]----+ | | | . | | o . | | E . . | | oS | | o = .. | | . * +. . | | o o.o+.= . | | =+.ooB.. | +-----------------+
# vim .ssh/id_rsa.pub
这里我直接复制到接收端的.ssh/authorized_keys文件中。因为是复制的,很可能会自动换行。需要手动调整。 将其调整问一行,当然你把发送端的公钥文件scp到接收端上,然后重命名文件为authorized_keys也是可以的。 测试互信是否成功 以/tmp文件夹下的文件做同步测试
# ls /tmp/ test test1.txt test2.txt test3.txt test4.txt test5.txt test6.txt test1 test2 test3 test4 test5 test6 yum.log
# /usr/bin/rsync -vzrtopg --progress /tmp/ [email protected]:/tmp sending incremental file list ./ test1 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=34/36) test1.txt 0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=33/36) test2 0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=32/36) test2.txt 0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=31/36) test3 0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=30/36) test3.txt 0 100% 0.00kB/s 0:00:00 (xfer#6, to-check=29/36) test4 0 100% 0.00kB/s 0:00:00 (xfer#7, to-check=28/36) test4.txt 0 100% 0.00kB/s 0:00:00 (xfer#8, to-check=27/36) test5 0 100% 0.00kB/s 0:00:00 (xfer#9, to-check=26/36) test5.txt 0 100% 0.00kB/s 0:00:00 (xfer#10, to-check=25/36) test6 0 100% 0.00kB/s 0:00:00 (xfer#11, to-check=24/36) test6.txt 0 100% 0.00kB/s 0:00:00 (xfer#12, to-check=23/36) yum.log 0 100% 0.00kB/s 0:00:00 (xfer#13, to-check=22/36) .ICE-unix/ test/ test/a1 12 100% 0.00kB/s 0:00:00 (xfer#14, to-check=19/36) test/a2 0 100% 0.00kB/s 0:00:00 (xfer#15, to-check=18/36) test/a3 0 100% 0.00kB/s 0:00:00 (xfer#16, to-check=17/36) test/a4 0 100% 0.00kB/s 0:00:00 (xfer#17, to-check=16/36) test/a5 0 100% 0.00kB/s 0:00:00 (xfer#18, to-check=15/36) test/a6 0 100% 0.00kB/s 0:00:00 (xfer#19, to-check=14/36) test/a7 0 100% 0.00kB/s 0:00:00 (xfer#20, to-check=13/36) test/aaa111.txt 0 100% 0.00kB/s 0:00:00 (xfer#21, to-check=12/36) test/tes11.txt 0 100% 0.00kB/s 0:00:00 (xfer#22, to-check=11/36) test/tes112.txt 0 100% 0.00kB/s 0:00:00 (xfer#23, to-check=10/36) test/tes113.txt 0 100% 0.00kB/s 0:00:00 (xfer#24, to-check=9/36) test/tes114.txt 0 100% 0.00kB/s 0:00:00 (xfer#25, to-check=8/36) test/tes115.txt 0 100% 0.00kB/s 0:00:00 (xfer#26, to-check=7/36) test/tes116.txt 0 100% 0.00kB/s 0:00:00 (xfer#27, to-check=6/36) test/test1123.txt 0 100% 0.00kB/s 0:00:00 (xfer#28, to-check=5/36) test/zz1111.txt 0 100% 0.00kB/s 0:00:00 (xfer#29, to-check=4/36) test/zz2222.txt 0 100% 0.00kB/s 0:00:00 (xfer#30, to-check=3/36) test/zzz.txt 0 100% 0.00kB/s 0:00:00 (xfer#31, to-check=2/36) test/test1/ test/zzz/ sent 1745 bytes received 620 bytes 1576.67 bytes/sec total size is 12 speedup is 0.01
加入inotify实现增量同步,脚本如下
# auth_rsync.sh #!/bin/bash user1="root" host1="119.23.211.200" dst1="/data" src="/data/" /soft/inotify/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f‘ -e modify,delete,create,attrib ${src} | while read files do nohup /usr/bin/rsync -vzrtopg --progress ${src} ${user1}@${host1}:${dst1} echo "${files} was rsynced" >> /var/log/rsync.log 2>&1 done
修改权限
# chmod 755 auth_rsync.sh
添加开机启动
# cp auth_rsync.sh /etc/init.d/ # chkconfig --add /etc/init.d/auth_rsync.sh
以守护进程启动脚本并放在后台运行
# nohup /etc/init.d/auth_rsync.sh > /tmp/rsync.log 2>&1 &
以上是关于Rsync的主要内容,如果未能解决你的问题,请参考以下文章