一篇文章教你学会数据备份利器rsync
作为一个系统办理员,数据备份是非常重要的。阿铭有一次没有做好备份策略,成果磁盘坏了,数据悉数丢掉。所以在今后的系统维护工作中,你必定要时刻紧记给数据做备份。

Linux系统下数据备份的工具许多,但阿铭只用一种,那便是rsync,从字面意思上能够理解为remote sync(长途同步)。rsync不只能够长途同步数据(类似于scp),而且能够本地同步数据(类似于cp),但不同于cp或scp的一点是,它不会覆盖曾经的数据(假如数据现已存在),而是先判别现已存在的数据和新数据的差异,只有数据不同时才会把不相同的部分覆盖。假如你的Linux没有rsync指令,请运用指令yum install -y rsync装置。下面阿铭先举一个比方,然后再详细讲解rsync的用法。

# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd 
sent 1,205 bytes received 35 bytes  2,480.00 bytes/sec
total size is 1,113 speedup is 0.90

上例将会把/etc/passwd同步到/tmp/目录下,并改名为1.txt。假如是长途仿制,数据备份便是这样的方法——IP:path,比方192.168.72.128:/root/。详细用法如下:

# rsync -av /etc/passwd 192.168.72.128:/tmp/1.txt
The 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])? yes
Warning: 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/sec
total size is 1,113 speedup is 19.53

初次衔接时会提示是否要继续衔接,咱们输入yes继续。当树立衔接后,需求输入暗码。假如手动履行这些操作比较简单,但若是写在脚本中该怎么办呢?这就触及增加信任关系了,该部分内容稍后会详细介绍。14.7.1 rsync的指令格局

rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]...  [USER@]HOST:SRC DEST
rsync [OPTION]... [USER@]HOST::SRC DEST
rsync [OPTION]... SRC [USER@]HOST::DEST

在阿铭前面举的两个比方中,第一个比方为第一种格局,第二个比方为第二种格局。但不同的是,阿铭并没有加user@host,假如不加默许指的是root。第三种格局是从长途目录同步数据到本地。第四种和第五种格局运用了两个冒号,这种格局和其他格局的验证方法不同。14.7.2 rsync常用选项rsync指令各选项的含义如下。

  • -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:加上该选项,将会在传输过程中紧缩

选项虽然多,但阿铭常用的选项也就-a、-v、-z、–delete和–exclude这几个,请紧记它们!下面阿铭将会针对这些选项做一系列小实验。1. 树立目录和文件过程如下所示:

# 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 root
root 0 6月  26 17:30 1
lrwxrwxrwx 1 root
root 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
# cd ..

阿铭树立这些文件的目的便是为后续实验做一些预备工作。2. 运用**-a**选项首要来看看-a选项的用法,如下所示:

# rsync -a test1 test2
# ls test2 
test1
# ls test2/test1/
1 123.txt 2  3

这儿有一个问题,便是本来想把test1目录直接仿制成test2目录,可成果rsync却新建了test2目录,然后把test1放到test2傍边。为了防止这样的状况发生,能够这样做:

# rm -rf test2
# rsync -a test1/ test2/
# ls -l test2/
总用量 0
-rw-r--r-- 1 root
root 0 6月  26 17:30 1
lrwxrwxrwx 1 root
root 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

这儿加一个斜杠就好了,所以阿铭主张你在运用rsync备份目录时,要养成加斜杠的习气。前面现已讲了-a选项等同于-rlptgoD,且-a还能够和–no-OPTIN一并运用。下面再来看看-l选项的效果,如下所示:

# rm -rf test2
# rsync -av --no-l test1/ test2/
sending incremental file list
created directory test2
skipping non-regular file "123.txt"
./
1
2
3
sent 234 bytes received 144 bytes  756.00 bytes/sec
total size is 13 speedup is 0.03

上例中运用了-v选项,跳过了非一般文件123.txt。其实123.txt是一个软衔接文件,假如不运用-l选项,系统则不理睬软衔接文件。虽然加-l选项能仿制软衔接文件,但软衔接的方针文件却没有仿制。有时咱们需求仿制软衔接文件所指向的方针文件,这又该怎么办呢?3. 运用**-L**选项详细用法如下:

# rm -rf test2
# rsync -avL test1/ test2/
sending incremental file list
created directory test2
./
1
123.txt
2
3
sent 265 bytes received 123 bytes  776.00 bytes/sec
total size is 0 speedup is 0.00
# ls -l test2/
总用量 0
-rw-r--r-- 1 root root 0 626 17:30 1
-rw-r--r-- 1 root root 0 626 17:30 123.txt
-rw-r--r-- 1 root root 0 626 17:30 2
-rw-r--r-- 1 root root 0 626 17:30 3

上例加上-L选项就能够把SRC中软衔接的方针文件仿制到DST。4. 运用**-u**选项首要检查一下test1/1和test2/1的创立时刻(肯定是相同的),然后运用touch修正一下test2/1的创立时刻(此刻test2/1要比test1/1的创立时刻晚一些)。假如不加-u选项,会把test2/1的创立时刻变成和test1/1相同,如下所示:

# 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

从上例能够看出二者的创立时刻是相同的。下面修正test2/1的创立时刻,然后不加-u同步,如下所示:

# 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

这儿test2/1的创立时刻仍是和test1/1相同。下面加上-u选项,如下所示:

# echo "1111" > test2/1
# ll test2/1
-rw-r--r-- 1 root root 5 626 17:34 test2/1
# rsync -avu test1/ test2/
sending incremental file list
./
123.txt -> /root/123.txt
sent 134 bytes received 22 bytes  312.00 bytes/sec
total size is 13 speedup is 0.08
# ll test1/1 test2/1
-rw-r--r-- 1 root root 0 626 17:30 test1/1
-rw-r--r-- 1 root root 5 626 17:34 test2/1

加上-u选项后,不会再把test1/1同步为test2/1了。5. 运用**–delete**选项首要删去test1/123.txt,如下所示:

# rm -f test1/123.txt
# ls test1/
1 2  3

然后把test1/目录同步到test2/目录下,如下所示:

# rsync -av test1/ test2/
sending incremental file list
./
1
sent 130 bytes received 38 bytes  336.00 bytes/sec
total size is 0 speedup is 0.00
# ls test2/
1 123.txt 2  3

上例中,test2/目录并没有删去123.txt。下面加上–delete选项,示例如下:

# rsync -av --delete test1/ test2/
sending incremental file list
deleting 123.txt
sent 84 bytes received 23 bytes  214.00 bytes/sec
total size is 0 speedup is 0.00
# ls test2/
1 2  3

这儿test2/目录下的123.txt也被删去了。别的还有一种状况,便是假如在DST中增加文件了,而SRC傍边没有这些文件,同步时加上–delete选项后同样会删去新增的文件。如下所示:

# 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
  1. 运用**–exclude**选项详细用法如下:
# 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

上例中,阿铭也运用了–progress选项,它主要是用来调查rsync同步过程状况的。总而言之,平时你运用rsync同步数据时,运用-a选项根本上就能够到达想要的效果了。当有个别需求时,也会用到–no-OPTION、-u、 -L、–delete、–exclude以及–progress等选项。其他选项阿铭都没有介绍,假如在今后的工作中遇到特别需求,能够查一下rsync的man文档。14.7.3 rsync运用实例上面列举了许多小事例,都是为了让大家了解rsync各个选项的根本用法。本节正式介绍rsync的实践运用,请大家认真学习。在正式实验前,你需求预备两台Linux机器,由于下面的小事例都是从一台机器仿制文件到另一台机器。前面阿铭也带着大家克隆过一台虚拟机,所以把那台克隆的虚拟机翻开即可,阿铭的两台机器IP地址分别为192.168.72.128和192.168.72.129。1. 经过ssh的方法在之前介绍的rsync的5种指令格局中,第二种和第三种(一个冒号)就属于经过ssh的方法备份数据。这种方法其实便是让用户登录到长途机器,然后履行rsync的使命:

# 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])? yes
Warning:
Permanently added '192.168.72.129' (ECDSA) to the list of known hosts.
root@192.168.72.129's password:
sending incremental file list
created directory /tmp/test2
./
1
1.txt
2
2.txt
3
4
sent 377 bytes received 166 bytes  98.73 bytes/sec
total size is 0speedup is 0.00

这种方法便是前面介绍的第二种方法了,是经过ssh仿制的数据,需求输入192.168.72.129那台机器root账户的暗码。当然也能够运用第三种方法仿制,如下所示:

# rsync -avL 192.168.72.129:/tmp/test2/ ./test3/
root@192.168.72.129's password:
receiving incremental file list
created directory
./test3
./
1
1.txt
2
2.txt
3
4
sent 141 bytes received 389 bytes  117.78 bytes/sec
total size is 0speedup is 0.00

以上两种方法假如写入脚本,做备份麻烦,要输入暗码,但咱们能够经过密钥(不树立暗码)验证。下面阿铭详细介绍一下经过密钥登录长途主机的方法。你能够依据3.33节,把128机器上的公钥内容放到129机器下的authorized_keys里边,这样128机器登录129机器时不再输入暗码,如下所示:

# ssh 192.168.72.129
Last login: Fri Jun 26 15:46:33 2020 from 192.168.72.1

现在不必输入暗码也能够登录主机129了。下面先从129主机退出来,再从主机128上履行一下rsync指令试试吧:

# rsync -avL test1/ 192.168.72.129:/tmp/test4/
sending incremental file list
created directory /tmp/test4
./
1
1.txt
2
2.txt
3
4
sent 377 bytes received 166 bytes  362.00 bytes/sec
total size is 0 speedup is 0.00
  1. 经过后台服务的方法这种方法能够理解为:在长途主机上树立一个rsync的服务器,在服务器上装备好rsync的各种运用,然后将本机作为rsync的一个客户端衔接长途的rsync服务器。下面阿铭就介绍一下如何装备一台rsync服务器。在128主机上树立并装备rsync的装备文件/etc/rsyncd.conf,如下所示(请把你的rsyncd.conf修正成如下内容):
# vim /etc/rsyncd.conf
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.72.128
[test]
path=/root/rsync
use chroot=true
max connections=4
read only=no
list=true
uid=root
gid=root
auth users=test
secrets file=/etc/rsyncd.passwd
hosts allow=192.168.72.0/24

其中装备文件分为两部分:大局装备部分和模块装备部分。大局部分便是几个参数,比方阿铭的rsyncd.conf中的port、log file、pid file和address都属于大局装备;而[test]以下部分便是模块装备部分了。一个装备文件中能够有多个模块,模块名可自定义,格局就像阿铭的rsyncd.conf中的这样。其实模块中的一些参数(如use chroot、max connections、udi、gid、auth users、secrets file以及hosts allow都能够装备成大局参数。当然阿铭并未给出所有的参数,你能够经过指令man rsyncd.conf获得更多信息。下面就简单解释一下这些参数的效果。

  • 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

发动rsyncd服务,如下所示:

# rsync --daemon --config=/etc/rsyncd.conf

发动后能够检查一下日志,并检查端口是否发动,如下所示:

# cat /var/log/rsync.log
2020/06/26 17:43:11 [4680] rsyncd version 3.1.3 starting, listening on port 873
# netstat -lnp |grep rsync
tcp 0     0 192.168.72.128:873     0.0.0.0:*               LISTEN      4680/rsync

假如想开机发动rsyncd服务,请把/usr/bin/rsync –daemon –confg=/etc/rsyncd.conf写入/etc/rc.d/rc.local文件。为了不影响实验过程,还需求把两台机器的firewalld服务关闭,并设置为不开机发动,操作过程如下所示:

# 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 list
created directory /tmp/test5
./
1
1.txt
2
2.txt
3
4
sent 141 bytes received 377 bytes  8.56 bytes/sec
total size is 0 speedup is 0.00

阿铭刚刚提到了选项use chroot,默许为true。首要在主机128的/root/rsync/test1/目录下创立一个软衔接文件,如下所示:

# ln -s /etc/passwd /root/rsync/test1/test.txt
# ls -l /root/rsync/test1/test.txt
lrwxrwxrwx 1 root root 11 6月  26 17:47 /root/rsync/test1/test.txt -> /etc/passwd

然后再到主机129上履行同步,如下所示:

# rsync -avL
test@192.168.72.128::test/test1/ /tmp/test6/
Password:
receiving incremental file list
symlink has no referent: "/test1/test.txt" (in test)
created directory /tmp/test6
./
1
1.txt
2
2.txt
3
4
sent 141 bytes received 436 bytes  42.74 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at
main.c(1659) [generator=3.1.3]

从上例能够看出,假如设置use chroot为true,则同步软衔接文件会有问题。下面阿铭把主机128的rsync装备文件修正一下,把true改为false,如下所示:

# sed -i 's/use chroot=true/use chroot=false/'/etc/rsyncd.conf
# grep 'use chroot' /etc/rsyncd.conf
use chroot=false

然后再到主机129上再次履行同步,如下所示:

# rsync -avL test@192.168.72.128::test/test1/ /tmp/test7/
Password:
receiving incremental file list
created directory
/tmp/test7
./
1
1.txt
2
2.txt
3
4
test.txt
sent 160 bytes received 1,556 bytes  137.28 bytes/sec
total size is 1,113 speedup is 0.65

这样问题就解决了。别的,修正完rsyncd.conf装备文件后不需求重启rsyncd服务,这是rsync的一个特定机制,装备文件是即时收效的。上面的比方中,阿铭都有输入暗码,这意味着咱们仍是不能写入脚本中主动履行。其实这种方法能够不必手动输入暗码,它有两种完结方法。(1) 指定暗码文件在客户端(即主机129)上修正一个暗码文件:/etc/pass,参加test用户的暗码:

# vim /etc/pass //写入如下内容
test123

修正暗码文件的权限:

# chmod 600 /etc/pass

在同步时指定暗码文件,就能够省去输入暗码的过程,如下所示:

# rsync -avL test@192.168.72.128::test/test1/ /tmp/test8/ --password-file=/etc/pass
receiving incremental file list
created directory /tmp/test8
./
1
1.txt
2
2.txt
3
4
test.txt
sent 160 bytes received 1,556 bytes  149.22 bytes/sec
total size is 1,113 speedup is 0.65

(2) 在rsync服务端不指定用户在服务端(即主机128)上修正装备文件rsyncd.conf,删去关于认证账户的装备项(auth user和secrets file这两行),如下所示:

# sed -i 's/auth users/#auth users/;s/secrets file/#secrets file/' /etc/rsyncd.conf

上例是在auth users和secrets file这两行的最前面加一个#,这表明将这两行作为注释,使其失掉含义。在前面阿铭未曾讲过sed的这种用法,它是用分号把两个替换的子指令块替换了。然后咱们再到客户端主机129上进行测验,如下所示:

# rsync -avL test@192.168.72.128::test/test1/ /tmp/test9/
receiving incremental file list
created directory /tmp/test9
./
1
1.txt
2
2.txt
3
4
test.txt
sent 160 bytes received 1,556 bytes  163.43 bytes/sec
total size is 1,113 speedup is 0.65

留意,这儿不必再加test这个用户了,默许是以root的身份仿制的。现在登录时现已不需求输入暗码了。