linux rsync(数据同步|增量备份)

宁 静
原来使用scp来备份不同PC的数据,十分不便,便开始寻找 Linux 下的数据自动同步工具,rsync 便步入眼中。

什么是rsync

rsync 是一个快速增量文件传输工具,它可以用于在同一主机备份内部的备分,我们还可以把它作为不同主机网络备份工具之用。本文主要讲述的是如何自架 rsync 服务器,以实现文件传输、备份和镜像。相对 tar 和 wget 来说,rsync 也有其自身的优点,比如速度快、安全、高效。

  1. rsync 分为服务器端、客户端。
  2. rsync 服务器是指以 deamon 方式运行 rsync 服务的服务器,需要打开 rsync deamon 和启动 xinetd 服务。默认端口873
  3. rsync 客户端是发起 rsync 连接的 PC。
  4. rsync 客户端发起连接后,rsync 服务器会检查 rsync 客户端提交的 rsync 用户名和密码是否正确,如果通过认证检测,则开始文件传输,传输的过程是按要求先比对文件的大小、属性、权限、MD5值等信息,如果两端文件信息不一致,则按要求同步文件的区别块。

rsync的安装

1
apt-get update && apt-get install rsync

rsync 服务器的配置文件

创建配置文件

下面我们将涉及到三个文件 rsyncd.confrsyncd.secretsrsyncd.motd

  1. rsyncd.confrsync server 主要配置文件。
  2. rsyncd.secrets:登录 rsync 服务器的密码文件。
  3. rsyncd.motd:定义用户登录信息。

创建/etc/rsyncd目录,用来存放3个配置文件;

1
2
3
4
5
:~$ mkdir -p /etc/rsyncd
:~$ touch /etc/rsyncd/rsyncd.conf
:~$ touch /etc/rsyncd/rsyncd.secrets
:~$ chmod 600 /etc/rsyncd/rsyncd.secrets
:~$ touch /etc/rsyncd/rsyncd.motd

rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
cat > /etc/rsyncd/rsyncd.conf <<EOF
#全局配置
# = 后面的值可根据自己的实际情况更改
# pid file 守护进程pid文件
# port 守护进程监听端口,可更改,由xinetd允许rsyncd时忽略此参数
# address 守护进程监听ip,由xinetd允许rsyncd时忽略此参数
# 我们使用xinetd 运行守护进程
pid file = /usr/local/var/run/rsyncd.pid
# port = 873
# address = 192.168.1.2
#rsyncd 守护进程运行系统用户全局配置,也可在具体的块中独立配置,
uid = root
gid = root
#允许 chroot,提升安全性,客户端连接模块,首先chroot到模块path参数指定的目录下
#chroot为yes时必须使用root权限,且不能备份path路径外的链接文件
use chroot = no
#只读
read only = no
#只写
write only = no
#允许访问rsyncd服务的ip,ip端或者单独ip之间使用空格隔开
hosts allow = 192.168.100.97/26
#不允许访问rsyncd服务的ip,*是全部(不涵盖在hosts allow中声明的ip,注意和hosts allow的先后顺序)
hosts deny = *
#客户端最大连接数
max connections = 5
#欢迎文件路径,可选的,在客户端链接成功后会显示欢迎信息
motd file = /etc/rsyncd/rsyncd.motd
#日志相关
# log file 指定rsync发送消息日志文件,而不是发送给syslog,如果不填这个参数默认发送给syslog
# transfer logging 是否记录传输文件日志
# log format 日志文件格式,格式参数请google
# syslog facility rsync发送消息给syslog时的消息级别,
# timeout连接超时时间
log file = /var/logs/rsyncd.log
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300

#模块 模块名称必须使用[]环绕,客户端要访问data1,则地址应该是data1user@address::data1
[linuxsirhome]
#模块根目录,必须指定
path=/home/test
#是否允许列出模块里的内容
list=yes
#忽略错误
#ignore errors
#模块验证用户名称,可使用空格或者逗号隔开多个用户名
auth users = data1user
#模块验证密码文件 可放在全局配置里
secrets file=/etc/rsyncd.secrets
#注释
comment = some description about this moudle
#排除目录,多个之间使用空格隔开
exclude = test1/ test2/
EOF

rsyncd.secrets

1
2
3
4
5
cat > /etc/rsyncd/rsyncd.secrets <<EOF
# 用户名:密码
root:222222
beinan:333333
EOF

root 是系统用户,这里的密码值得注意,为了安全,你不能把系统用户的密码写在这里。
比如你的系统用户 root 密码是 abcdefg,为了安全,你可以让 rsync 中的 root 密码为 222222 。这和 samba 的用户认证的密码原理是差不多的。

rsyncd.motd

1
2
3
4
5
cat > /etc/rsyncd/rsyncd.motd <<EOF
+++++++++++++++++++++++++++
+ nas.net rsync 2019-* +
+++++++++++++++++++++++++++
EOF

启动rsync 服务器及防火墙的设置

启动rsync服务器

启动 rsync 服务器相当简单,--daemon 是让 rsync 以服务器模式运行;

1
2
:~$ /usr/bin/rsync --daemon  --config=/etc/rsyncd/rsyncd.conf
#>

rsync 服务器和防火墙

Linux 防火墙是用 iptables,所以我们需要在服务器上允许 rsync 端口通过,客户端上也应该让其通过。

1
iptables -A INPUT -p tcp -m state --state NEW  -m tcp --dport 873 -j ACCEPT

查看一下防火墙是不是打开了873端口。

1
iptables -L INPUT | grep 873

通过 rsync 客户端来同步数据

将 rsync 服务器上的文件同步到本地

1
rsync -avzP --delete --password-file=rsync.password --port=[port] [email protected]::linuxsirhome   linuxsirhome
  • Password: 这里要输入 linuxsir 的密码,是服务器端提供的,在前面的例子中,我们用的是 222222,输入的密码并不显示出来;输好后就回车;
  • -a:相当于 -rlptgoD
  • -r:文件夹递归;
  • -l:是链接文件,意思是拷贝链接文件;
  • -p:表示保持文件原有权限;
  • -t:保持文件原有时间;
  • -g:保持文件原有用户组;
  • -o:保持文件原有属主;
  • -D:相当于块设备文件;
  • -z:传输时压缩;
  • -P:传输进度;
  • -v:传输时的进度等信息,和-P有点关系;
  • --delete: 表示客户端上的数据要与服务器端完全一致,如果linuxsirhome目录中有服务器上不存在的文件,则删除。最终目的是让linuxsirhome目录上的数据完全与服务器上保持一致;用的时候一定要小心。
  • --password-file=rsync.password: 这是当我们以linuxsir用户登录rsync服务器同步数据时,从 rsync.password 里读取密码。

将本地文件同步rsync server

1
rsync -avzP --password-file=rsync.password --port=[port] /home/data/ [email protected]::linuxsirhome

本地的 data 内的文件被传输到 /home/test/ 中。若本地路径写为/home/data没有/,则整个data目录上传至/home/test/

列出远程文件

1
2
3
4
# Method 1
rsync -v --password-file=rsync.password rsync://[email protected]:[port]/linuxsirhome/
# Method 2
rsync -v --password-file=rsync.password --port=[port] [email protected]::linuxsirhome/

ssh端口非默认22同步

1
rsync -avzP -e "ssh -p 22222" [USER@]HOST:SRC [DEST]

让 rsync 客户端自动与服务器同步数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
crontab -e
10 0 * * * rsync -avzP --delete --password-file=rsync.password [email protected]::linuxsirhome linuxsirhome
````

## 同步时include/exclude正则表达式

rsync 正则例子:
- <bblue>*</bblue>:代表所有文件或文件夹
- <bblue>dir1<bblue>:仅同步文件夹 `dir1`
- <bblue>dir*</bblue>:仅同步文件夹 `dir1`, `dir2`, `dir3`, 等等...
- <bblue>file*</bblue>:同步名字以`file`开头的文件。
- <bblue>dir**</bblue>:同步所有路径以`dir`开头的文件,如`dir1/file.txt`, `dir2/bar/ffaa.html`, 等等...
- <bblue>dir***</bblue>:同上
- <bblue>dir1/*</bblue>:无效正则
- <bblue>dir1/**</bblue>:无效正则
- <bblue>dir1/***</bblue>: 同步`dir1`及其子文件夹,如`dir1/file.txt`, `dir1/fooo.sh`, `dir1/fold/baar.py`, 等等...


```sh
# + ---> include this file/dir
# - ---> exclude this file/dir

:~$ cat > /tmp/a <<EOF
+ .bash
+ .bashrc
- .bash_aliases
+ .ssh/***
- *
EOF
:~$ rsync -avzu --include-from=/tmp/a ~/ rsync://192.168.0.1:[port]/linuxsirhome

References:

  • wikipedia.org
  • 原文1
  • 原文2