![宁 静]()
原来使用scp
来备份不同PC的数据,十分不便,便开始寻找 Linux 下的数据自动同步工具,rsync 便步入眼中。
什么是rsync
rsync 是一个快速增量文件传输工具,它可以用于在同一主机备份内部的备分,我们还可以把它作为不同主机网络备份工具之用。本文主要讲述的是如何自架 rsync 服务器,以实现文件传输、备份和镜像。相对 tar 和 wget 来说,rsync 也有其自身的优点,比如速度快、安全、高效。
- rsync 分为服务器端、客户端。
- rsync 服务器是指以 deamon 方式运行 rsync 服务的服务器,需要打开 rsync deamon 和启动 xinetd 服务。默认端口
873
。
- rsync 客户端是发起 rsync 连接的 PC。
- rsync 客户端发起连接后,rsync 服务器会检查 rsync 客户端提交的 rsync 用户名和密码是否正确,如果通过认证检测,则开始文件传输,传输的过程是按要求先比对文件的大小、属性、权限、MD5值等信息,如果两端文件信息不一致,则按要求同步文件的区别块。
rsync的安装
1
| apt-get update && apt-get install rsync
|
rsync 服务器的配置文件
创建配置文件
下面我们将涉及到三个文件 rsyncd.conf
,rsyncd.secrets
和rsyncd.motd
。
- rsyncd.conf:rsync server 主要配置文件。
- rsyncd.secrets:登录 rsync 服务器的密码文件。
- 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 = /usr/local/var/run/rsyncd.pid
uid = root gid = root
use chroot = no
read only = no
write only = no
hosts allow = 192.168.100.97/26
hosts deny = *
max connections = 5
motd file = /etc/rsyncd/rsyncd.motd
log file = /var/logs/rsyncd.log transfer logging = yes log format = %t %a %m %f %b syslog facility = local3 timeout = 300
[linuxsirhome]
path=/home/test
list=yes
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
| rsync -v --password-file=rsync.password rsync://[email protected]:[port]/linuxsirhome/
rsync -v --password-file=rsync.password --port=[port] [email protected]::linuxsirhome/
|
ssh端口非默认22同步
让 rsync 客户端自动与服务器同步数据
1 2 3
| crontab -e 10 0 * * * rsync -avzP --delete --password-file=rsync.password [email protected]::linuxsirhome linuxsirhome `
|
同步时include/exclude正则表达式
rsync 正则例子:
- *:代表所有文件或文件夹
- dir1:仅同步文件夹
dir1
- dir*:仅同步文件夹
dir1
, dir2
, dir3
, 等等…
- file*:同步名字以
file
开头的文件。
- dir**:同步所有路径以
dir
开头的文件,如dir1/file.txt
, dir2/bar/ffaa.html
, 等等…
- dir***:同上
- dir1/*:无效正则
- dir1/**:无效正则
- dir1/***: 同步
dir1
及其子文件夹,如dir1/file.txt
, dir1/fooo.sh
, dir1/fold/baar.py
, 等等…
1 2 3 4 5 6 7 8 9 10 11
|
:~$ cat > /tmp/a <<EOF + .bash + .bashrc - .bash_aliases + .ssh/*** - * EOF :~$ rsync -avzu --include-from=/tmp/a ~/ rsync://192.168.0.1:[port]/linuxsirhome
|
References: