Rsync实操
Rsync实操
一.rsync命令
#类似于cp[root@user2 ~]# rsync info.sh root@192.168.168.130:/rootroot@192.168.168.130's password: [root@user1 ~]# lsanaconda-ks.cfg ceph-release-1-1.el7.noarch.rpm info.sh
二、使用rsync备份push方式
-
服务器:server 192.168.168.130(需要进行数据备份的主机)
-
客户端:client 192.168.168.131 (备份存储的主机)
# 在客户端上编写rsync配置文件,创建一个存放备份的同步目录[root@targetpc ~]# vim /etc/rsyncd.conf[root@targetpc ~]# cat /etc/rsyncd.confport=873address = 192.168.168.131 #客户端地址uid = rootgid = rootuse chroot = yesmax connections = 4pid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locklog file = /var/log/rsyncd.logmotd file = /etc/rsyncd.motd #可弄可不弄hosts allow = 192.168.168.0/24 #允许哪个网段传输数据[data] #网络名path = /data/backup #数据存储目录 真实名 需要创建comment = bakcup dataread only = falselist = yes#独立验证auth users = rsyncusersecrets file = /etc/rsync.passwd #自己创建#设置独立账户密码[root@targetpc ~]# vim /etc/rsync.passwd#添加权限[root@targetpc ~]# chmod 600 /etc/rsync.passwd[root@targetpc ~]# vim /etc/rsync.passwd[root@targetpc ~]# cat /etc/rsync.passwdrsyncuser:123#设置标语[root@targetpc ~]# echo "来送数据了" > /etc/rsyncd.motd[root@targetpc ~]# systemctl restart rsyncd#测试 服务器测试[root@sourcepc data]# ls1.txt 2.txt 3.txt aaa[root@sourcepc data]# rsync -avz /data/* rsyncuser@192.168.168.131::data来送数据了Password: sending incremental file list3.txtsent 155 bytes received 36 bytes 20.11 bytes/sectotal size is 0 speedup is 0.00#客户主机结果[root@targetpc backup]# ls1.txt 2.txt 3.txt aaa
三、测试--delete命令push
#--delete实验 不能与*同时用[root@targetpc backup]# ls1.txt 2.txt 3.txt aaa del.txt[root@sourcepc data]# ls1.txt 2.txt 3.txt aaa[root@sourcepc data]# rsync -avz --delete /data/* rsyncuser@192.168.168.131::data来送数据了Password: sending incremental file listsent 119 bytes received 13 bytes 13.89 bytes/sectotal size is 0 speedup is 0.00[root@targetpc backup]# ls1.txt 2.txt 3.txt aaa del.txt#使用--delete时 去掉*[root@sourcepc data]# rsync -avz --delete /data/ rsyncuser@192.168.168.131::data来送数据了Password: sending incremental file listdeleting del.txt./sent 149 bytes received 31 bytes 18.95 bytes/sectotal size is 0 speedup is 0.00[root@targetpc backup]# ls1.txt 2.txt 3.txt aaa
四、免密同步
#免密[root@sourcepc data]# echo "123" > /etc/rsync.passwd[root@sourcepc data]# rsync -avz --delete /data/ rsyncuser@192.168.168.131::data --password-file=/etc/rsync.passwd #注意时--password 全称来送数据了ERROR: password file must not be other-accessiblersync error: syntax or usage error (code 1) at authenticate.c(196) [sender=3.1.2]#设置权限[root@sourcepc data]# chmod 600 /etc/rsync.passwd[root@sourcepc data]# rsync -avz --delete /data/ rsyncuser@192.168.168.131::data --password-file=/etc/rsync.passwd来送数据了sending incremental file listsent 142 bytes received 13 bytes 23.85 bytes/sectotal size is 0 speedup is 0.00
五、pull模式
以上的同步过程都是服务端主动推送数据给目标主机,这里演示下目标主机主动拉取数据进行同步
# 服务端上配置/etc/rsyncd.conf文件port=873address = 192.168.168.130uid = rootgid = rootuse chroot = yesmax connections = 4pid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locklog file = /var/log/rsyncd.log#motd file = /etc/rsyncd.motdhosts allow = 192.168.168.0/24[data]path = /datacomment = dataread only = falselist = yesauth users = rsyncusersecrets file = /etc/rsync.passwd# 认证文件[root@sourcepc log]# cat /etc/rsync.passwdrsyncuser:123# 服务端上启动rsync[root@sourcepc log]# systemctl start rsyncd[root@sourcepc log]# systemctl status rsyncd● rsyncd.service - fast remote file copy program daemonLoaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled)Active: active (running) since 四 2025-06-19 20:35:23 CST; 2s agoMain PID: 2097 (rsync)CGroup: /system.slice/rsyncd.service└─2097 /usr/bin/rsync --daemon --no-detach6月 19 20:35:23 sourcepc systemd[1]: Started fast remote file copy program daemon.# 目标主机上拉取数据[root@client data]# rsync -avz rsyncuser@192.168.168.130::data /dataPassword: receiving incremental file list./1.txt2.txt3.txtaaa12/bbb/ccc/sent 100 bytes received 305 bytes 162.00 bytes/sectotal size is 0 speedup is 0.00[root@client data]# ls1.txt 2.txt 3.txt aaa12 backup bbb ccc
六、rsync+sersync 实现数据实时同步push
#在需要备份主机处 安装sersync 使用的外部安装包#使用rz 调入安装包[root@sourcepc ~]# rz[root@sourcepc ~]# lsanaconda-ks.cfg info.shceph-release-1-1.el7.noarch.rpm sersync2.5.4_64bit_binary_stable_final.tar.gz#解压[root@sourcepc ~]# tar -xf sersync2.5.4_64bit_binary_stable_final.tar.gz[root@sourcepc ~]# lsanaconda-ks.cfg GNU-Linux-x86 sersync2.5.4_64bit_binary_stable_final.tar.gzceph-release-1-1.el7.noarch.rpm info.sh#进行配置[root@sourcepc ~]# cd GNU-Linux-x86/[root@sourcepc GNU-Linux-x86]# lsconfxml.xml sersync2[root@sourcepc GNU-Linux-x86]# vim confxml.xml #修改的部分</filter><inotify><delete start="true"/><createFolder start="true"/><createFile start="true"/><closeWrite start="true"/><moveFrom start="true"/><moveTo start="true"/><attrib start="true"/><modify start="true"/></inotify><sersync><localpath watch="/data"><remote ip="192.168.168.131" name="data"/><!--<remote ip="192.168.8.39" name="tongbu"/>--><!--<remote ip="192.168.8.40" name="tongbu"/>--></localpath><rsync><commonParams params="-artuz"/><auth start="true" users="rsyncuser" passwordfile="/etc/rsync.passwd"/><userDefinedPort start="false" port="874"/><!-- port=874 --><timeout start="false" time="100"/><!-- timeout=100 --><ssh start="false"/></rsync>#执行文件[root@sourcepc GNU-Linux-x86]# ./sersync2 -d -r -o ./confxml.xml set the system paramexecute:echo 50000000 > /proc/sys/fs/inotify/max_user_watchesexecute:echo 327679 > /proc/sys/fs/inotify/max_queued_eventsparse the command paramoption: -d run as a daemonoption: -r rsync all the local files to the remote servers before the sersync workoption: -o config xml name: ./confxml.xmldaemon thread num: 10parse xml config filehost ip : localhost host port: 8008will ignore the inotify createFile event daemon start,sersync run behind the console use rsync password-file :user is rsyncuserpasswordfile is /etc/rsync.passwdconfig xml parse successplease set /etc/rsyncd.conf max connections=0 Manuallysersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)please according your cpu ,use -n param to adjust the cpu rate------------------------------------------rsync the directory recursivly to the remote servers onceworking please wait...execute command: cd /data && rsync -artuz -R --delete ./ rsyncuser@192.168.168.131::data --password-file=/etc/rsync.passwd >/dev/null 2>&1 [root@sourcepc GNU-Linux-x86]# run the sersync: watch path is: /data#测试[root@sourcepc data]# ls1.txt 2.txt 3.txt aaa[root@sourcepc data]# mkdir bbb[root@targetpc backup]# ls1.txt 2.txt 3.txt aaa bbb[root@sourcepc data]# ls1.txt 2.txt 3.txt aaa bbb ccc[root@sourcepc data]# mv aaa aaa12[root@targetpc backup]# ls1.txt 2.txt 3.txt aaa12 bbb ccc