学会数据备份利器rsync
作为一个系统管理员,数据备份是非常重要的。阿跃有一次没有做好备份策略,结果磁盘坏了,数据全部丢失。所以在以后的系统维护工作中,你一定要时刻牢记给数据做备份。
# rsync -av /etc/passwd /tmp/1.txtsending incremental file listpasswd
sent 1,205 bytes received 35 bytes 2,480.00 bytes/sectotal size is 1,113 speedup is 0.90
# rsync -av /etc/passwd 192.168.72.128:/tmp/1.txtThe authenticity of host '192.168.72.128 (192.168.72.128)' can't be established.ECDSA key fingerprint is SHA256:gFHUJnoZAjOcnG95pt7Zg9iaPZGDiOrbZyssZtRoQhA.Are you sure you want to continue connecting (yes/no/[fingerprint])? yesWarning: Permanently added '192.168.72.128' (ECDSA) to the list of known hosts.root@192.168.72.128's password:sending incremental file list
sent 45 bytes received 12 bytes 8.77 bytes/sectotal size is 1,113 speedup is 19.53
rsync [OPTION]... SRC DESTrsync [OPTION]... SRC [USER@]HOST:DESTrsync [OPTION]... [USER@]HOST:SRC DESTrsync [OPTION]... [USER@]HOST::SRC DESTrsync [OPTION]... SRC [USER@]HOST::DEST
-
-a:这是归档模式,表示以递归方式传输文件,并保持所有属性,它等同于-rlptgoD。-a选项后面可以跟一个--no-OPTION,表示关闭-rlptgoD中的某一个,比如-a--no-l等同于-rptgoD。 -
-r:表示以递归模式处理子目录。它主要是针对目录来说的,如果单独传一个文件不需要加-r选项,但是传输目录时必须加。 -
-v:表示打印一些信息,比如文件列表、文件数量等。 -
-l:表示保留软连接。 -
-L:表示像对待常规文件一样处理软连接。如果是SRC中有软连接文件,则加上该选项后,将会把软连接指向的目标文件复制到DST。 -
-p:表示保持文件权限。 -
-o:表示保持文件属主信息。 -
-g:表示保持文件属组信息。 -
-D:表示保持设备文件信息。 -
-t:表示保持文件时间信息。 -
--delete:表示删除DST中SRC没有的文件。 -
--exclude=PATTERN:表示指定排除不需要传输的文件,等号后面跟文件名,可以是万用字符模式(如*.txt)。 -
--progress:表示在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等。 -
-u:表示把DST中比SRC还新的文件排除掉,不会覆盖。 -
-z:加上该选项,将会在传输过程中压缩
# mkdir rsync# cd rsync# mkdir test1# cd test1# touch 1 2 3/root/123.txt# ln -s/root/123.txt ./123.txt# ls -l总用量 0-rw-r--r-- 1 rootroot 0 6月 26 17:30 1lrwxrwxrwx 1 rootroot 13 6月 26 17:30 123.txt ->/root/123.txt-rw-r--r-- 1 rootroot 0 6月 26 17:30 2-rw-r--r-- 1 rootroot 0 6月 26 17:30 3# cd ..
# rsync -a test1 test2# ls test2 test1# ls test2/test1/1 123.txt 2 3
# rm -rf test2# rsync -a test1/ test2/# ls -l test2/总用量 0-rw-r--r-- 1 rootroot 0 6月 26 17:30 1lrwxrwxrwx 1 rootroot 13 6月 26 17:30 123.txt -> /root/123.txt-rw-r--r-- 1 root root 0 6月 26 17:30 2-rw-r--r-- 1 root root 0 6月 26 17:30 3
# rm -rf test2# rsync -av --no-l test1/ test2/sending incremental file listcreated directory test2skipping non-regular file "123.txt"./123sent 234 bytes received 144 bytes 756.00 bytes/sectotal size is 13 speedup is 0.03
# rm -rf test2# rsync -avL test1/ test2/sending incremental file listcreated directory test2./1123.txt23sent 265 bytes received 123 bytes 776.00 bytes/sectotal size is 0 speedup is 0.00# ls -l test2/总用量 0-rw-r--r-- 1 root root 0 6月 26 17:30 1-rw-r--r-- 1 root root 0 6月 26 17:30 123.txt-rw-r--r-- 1 root root 0 6月 26 17:30 2-rw-r--r-- 1 root root 0 6月 26 17:30 3
# ll test1/1 test2/1-rw-r--r-- 1 root root 0 6月 26 17:30 test1/1-rw-r--r-- 1 root root 0 6月 26 17:30 test2/1
# echo "1111" > test2/1# ll test2/1- rw-r--r-- 1 root root 5 6月 26 17:33 test2/1# rsync -a test1/1 test2/# ll test2/1-rw-r--r-- 1 root root 0 6月 26 17:30 test2/1
# echo "1111" > test2/1# ll test2/1-rw-r--r-- 1 root root 5 6月 26 17:34 test2/1# rsync -avu test1/ test2/sending incremental file list./123.txt -> /root/123.txtsent 134 bytes received 22 bytes 312.00 bytes/sectotal size is 13 speedup is 0.08# ll test1/1 test2/1-rw-r--r-- 1 root root 0 6月 26 17:30 test1/1-rw-r--r-- 1 root root 5 6月 26 17:34 test2/1
# rm -f test1/123.txt# ls test1/1 2 3
# rsync -av test1/ test2/sending incremental file list./1sent 130 bytes received 38 bytes 336.00 bytes/sectotal size is 0 speedup is 0.00# ls test2/1 123.txt 2 3
# rsync -av --delete test1/ test2/sending incremental file listdeleting 123.txtsent 84 bytes received 23 bytes 214.00 bytes/sectotal size is 0 speedup is 0.00# ls test2/1 2 3
# touch test2/4# ls test1/1 2 3# ls test2/1 2 3 4# rsync -a --delete test1/ test2/# ls test1/1 2 3# ls test2/1 2 3
# touch test1/4# rsync -a --exclude="4" test1/ test2/# ls test1/1 2 3 4# ls test2/1 2 3
# touch test1/1.txt test1/2.txt# ls test1/1 1.txt 2 2.txt 3 4# rsync -a --progress --exclude="*.txt" test1/ test2/sending incremental file list./4 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/5)# ls test2/1 2 3 4
# rsync -avL test1/ 192.168.72.129:/tmp/test2/The authenticity of host '192.168.72.129 (192.168.72.129)' can't be established.ECDSA key fingerprint is SHA256:gFHUJnoZAjOcnG95pt7Zg9iaPZGDiOrbZyssZtRoQhA.Are you sure you want to continue connecting (yes/no/[fingerprint])? yesWarning:Permanently added '192.168.72.129' (ECDSA) to the list of known hosts.root@192.168.72.129's password:sending incremental file listcreated directory /tmp/test2./11.txt22.txt34sent 377 bytes received 166 bytes 98.73 bytes/sectotal size is 0speedup is 0.00
# rsync -avL 192.168.72.129:/tmp/test2/ ./test3/root@192.168.72.129's password:receiving incremental file listcreated directory./test3./11.txt22.txt34sent 141 bytes received 389 bytes 117.78 bytes/sectotal size is 0speedup is 0.00
# ssh 192.168.72.129Last login: Fri Jun 26 15:46:33 2020 from 192.168.72.1
# rsync -avL test1/ 192.168.72.129:/tmp/test4/sending incremental file listcreated directory /tmp/test4./11.txt22.txt34sent 377 bytes received 166 bytes 362.00 bytes/sectotal size is 0 speedup is 0.00
# vim /etc/rsyncd.confport=873log file=/var/log/rsync.logpid file=/var/run/rsyncd.pidaddress=192.168.72.128[test]path=/root/rsyncuse chroot=truemax connections=4read only=nolist=trueuid=rootgid=rootauth users=testsecrets file=/etc/rsyncd.passwdhosts allow=192.168.72.0/24
-
port:指定在哪个端口启动rsyncd服务,默认是873端口。
-
log file:指定日志文件。
-
pid file:指定pid文件,这个文件的作用涉及服务的启动、停止等进程管理操作。
-
address:指定启动rsyncd服务的IP。假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动。
-
[]:指定模块名,里面内容自定义。
-
path:指定数据存放的路径。
-
use chroot true|false:表示在传输文件前,首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但缺点是需要roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true,如果你的数据当中有软连接文件,阿跃建议你设置成false。
-
max connections:指定最大的连接数,默认是0,即没有限制。
-
read only ture|false:如果为true,则不能上传到该模块指定的路径下。
-
list:表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,设定为false则隐藏。
-
uid/gid:指定传输文件时以哪个用户/组的身份传输。
-
auth users:指定传输时要使用的用户名。
-
secrets file:指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。注意,该密码文件的权限一定要是600。
-
hosts allow:表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开。
编辑secrets file并保存后要赋予600权限,如果权限不对,则不能完成同步,如下所示:
# vi /etc/rsyncd.passwd //写入如下内容test:test123# chmod 600 /etc/rsyncd.passwd
# rsync --daemon --config=/etc/rsyncd.conf
# cat /var/log/rsync.log2020/06/26 17:43:11 [4680] rsyncd version 3.1.3 starting, listening on port 873# netstat -lnp |grep rsynctcp 0 0 192.168.72.128:873 0.0.0.0:* LISTEN 4680/rsync
# systemctl stop firewalld ; systemctl disable firewalld //两台机器都执行Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.# rsync -avL test@192.168.72.128::test/test1/ /tmp/test5/Password:receiving incremental file listcreated directory /tmp/test5./11.txt22.txt34sent 141 bytes received 377 bytes 8.56 bytes/sectotal size is 0 speedup is 0.00
# ln -s /etc/passwd /root/rsync/test1/test.txt# ls -l /root/rsync/test1/test.txtlrwxrwxrwx 1 root root 11 6月 26 17:47 /root/rsync/test1/test.txt -> /etc/passwd
# rsync -avLtest@192.168.72.128::test/test1/ /tmp/test6/Password:receiving incremental file listsymlink has no referent: "/test1/test.txt" (in test)created directory /tmp/test6./11.txt22.txt34sent 141 bytes received 436 bytes 42.74 bytes/sectotal size is 0 speedup is 0.00rsync error: some files/attrs were not transferred (see previous errors) (code 23) atmain.c(1659) [generator=3.1.3]
# sed -i 's/use chroot=true/use chroot=false/'/etc/rsyncd.conf# grep 'use chroot' /etc/rsyncd.confuse chroot=false
# rsync -avL test@192.168.72.128::test/test1/ /tmp/test7/Password:receiving incremental file listcreated directory/tmp/test7./11.txt22.txt34test.txtsent 160 bytes received 1,556 bytes 137.28 bytes/sectotal size is 1,113 speedup is 0.65
# vim /etc/pass //写入如下内容test123
# chmod 600 /etc/pass
# rsync -avL test@192.168.72.128::test/test1/ /tmp/test8/ --password-file=/etc/passreceiving incremental file listcreated directory /tmp/test8./11.txt22.txt34test.txtsent 160 bytes received 1,556 bytes 149.22 bytes/sectotal size is 1,113 speedup is 0.65
# sed -i 's/auth users/#auth users/;s/secrets file/#secrets file/' /etc/rsyncd.conf
# rsync -avL test@192.168.72.128::test/test1/ /tmp/test9/receiving incremental file listcreated directory /tmp/test9./11.txt22.txt34test.txtsent 160 bytes received 1,556 bytes 163.43 bytes/sectotal size is 1,113 speedup is 0.65