Linux搭建rsync服务
一、Rsync的简单介绍
Rsync是一款开源的、快速的、多功能的、可实现全量及增量(全量备份是指全部备份,增量备份是在上一次备份的基础上只备份更新的内容)的本地货远程数据同步备份的优秀工具。Rsync软件适用于unix/linux/windows等多种操作系统平台。
二、Rsync的三种工作模式
#rsync 本地模式 Local: rsync [OPTION...] SRC... [DEST] #rsync shell模式 Access via remote shell: Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST:DEST #rsync daemon模式 Access via rsync daemon: Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST::DEST rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
三、Rsync常用参数
-v |
详细模式输出,传输时的进度等信息 |
-z |
传输时进行压缩以提高传输效率,--compress-level=NUM可按级别压缩 |
-a |
以递归方式传输文件,并保持所有文件的属性 |
-r |
对子目录以递归模式,即目录下的所有目录都以同样的模式传输 |
-t |
保持文件的时间信息 |
-o |
保持文件的属主信息 |
-p |
保持文件的权限 |
-g |
保持文件的属组信息 |
-P(大写) |
显示同步的过程及传输时的进度等信息 |
-D |
保持设备文件信息 |
-I |
保留软链接 |
-e,--rsh=COMMAND |
使用的信道协议,指定替代rsh的shell程序 |
-n |
测试选项,模拟执行 |
--exclude=PATTERN |
指定排除不需要传输的文件模式 |
--exclide-from=FILE |
从文本文件读取需要排除的文件列表 |
--bwlimit=KBPS |
限制传输速度 |
--partial |
断点续传 |
--delete |
使目标目录内容和源保持目录一致,删除不同的文件 |
四、搭建Rsync服务
实验环境:
<1>系统环境:
•CentOS7.5
<2>主机环境:
•backup01 IP地址:10.0.0.41 rsync服务端(备份服务端)
•nfs01 IP地址:10.0.0.31 rsync客户端(用户数据)
1、服务端(远程备份服务器)
第一步:查看rsync安装包 rpm -qa rsync 第二步:添加rsync服务的用户,管理本地目录 useradd -s /sbin/nologin -M rsync id rsync 第三步:配置rsync的进程模式(vim /etc/rsyncd.conf) uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [backup] path = /backup ignore errors read only = false list = false fake super = yes hosts allow = 172.16.2.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password 第四步:根据rsync.conf的auth_users配置帐户,远程连接的,并根据secreets file参数生成密码文件 echo "rsync_backup:wt">>/etc/rsync.password cat /etc/rsync.password 第五步:更改密码配置文件的权限 chmod 600 /etc/rsync.password ls -l /etc/rsync.password 第六步:创建共享的目录授权rsync服务管理 mkdir -p /backup chown -R rsync.rsync /backup #提示:如果没有/backup目录,就会chdir failed 第七步:启动rsync服务并检查 rsync --daemon ps -ef|grep rsync|grep -v grep lsof -i :873 第八步:开机自启动 echo "/usr/bin/rsync --daemon">>/etc/rc.local tail -1 /etc/rc.local
2、客户端
第一步:生成连接服务器需要的密码文件 echo "wt">>/etc/rsync.password cat /etc/rsync.password 第二步:为密码文件配置权限 chmod 600 /etc/rsync.password ls -l /etc/rsync.password
3、客户端推送数据到服务端
提示:rsync: chgrp "data" (in backup) failed: Operation not permitted (1),出现这个语句是因为rsyncd.conf没有配置fake super = yes
[root@nfs01 ~]# mkdir /data [root@nfs01 ~]# cd /data/ [root@nfs01 data]# touch aaa [root@nfs01 data]# rsync -avz /data rsync_backup@172.16.2.41::backup/ Password: sending incremental file list rsync: chgrp "data" (in backup) failed: Operation not permitted (1) data/ data/aaa rsync: chgrp "data/.aaa.3HdDEQ" (in backup) failed: Operation not permitted (1) sent 118 bytes received 203 bytes 37.76 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(1178) [sender=3.1.2]
4、服务端查看数据是否推送成功
[root@backup01 ~]# cd /backup/ [root@backup01 backup]# ls data [root@backup01 backup]# cd [root@backup01 ~]# cd /backup [root@backup01 backup]# ls data [root@backup01 backup]# ls data/ aaa
五、Rsync多模块配置
uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [backup] path = /backup ignore errors read only = false list = false fake super = yes hosts allow = 172.16.2.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password [wtt] path = /wtt ignore errors read only = false list = false fake super = yes hosts allow = 172.16.2.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_wtt secrets file = /etc/rsync.password.wtt
六、深入使用Rsync
1、从远程存储节点拉取数据到本地
[root@nfs01 ~]# cd /data/ [root@nfs01 data]# ls aaa ssss [root@nfs01 data]# rm -f * [root@nfs01 data]# rsync -avz rsync_backup@172.16.2.41::backup/ /data/ --password-file=/etc/rsync.password receiving incremental file list ./ aaa ssss sent 69 bytes received 181 bytes 500.00 bytes/sec total size is 0 speedup is 0.00
2、排除某些文件推送数据
#同步除名为eee文件之外的文件 [root@nfs01 data]# ls aaa bbb ccc ddd eee ssss [root@nfs01 data]# rsync -avz --exclude=eee /data rsync_backup@172.16.2.41::backup/ --password-file=/etc/rsync.password sending incremental file list data/ data/aaa data/bbb data/ccc data/ddd data/ssss sent 338 bytes received 123 bytes 922.00 bytes/sec total size is 0 speedup is 0.00 #查看远程存储节点的数据内容 [root@backup01 backup]# ls data [root@backup01 backup]# ls data/ aaa bbb ccc ddd ssss
3、排除多个文件推送数据
#同步除名为ccc和eee文件之外的文件 [root@nfs01 data]# ls aaa bbb ccc ddd eee ssss [root@nfs01 data]# rsync -avz --exclude={ccc,eee} /data rsync_backup@172.16.2.41::backup/ --password-file=/etc/rsync.password sending incremental file list data/ data/aaa data/bbb data/ddd data/ssss sent 288 bytes received 104 bytes 784.00 bytes/sec total size is 0 speedup is 0.00 #查看远程存储节点的数据内容 [root@backup01 backup]# ls data [root@backup01 backup]# ls data/ aaa bbb ddd ssss
8.4、排除连续文件推送数据
[root@nfs01 data]# ls a aaa b bbb c ccc ddd eee ssss [root@nfs01 data]# rsync -avz --exclude={a..c} /data rsync_backup@172.16.2.41::backup/ --password-file=/etc/rsync.password sending incremental file list data/ data/aaa data/bbb data/ccc data/ddd data/eee data/ssss sent 393 bytes received 142 bytes 356.67 bytes/sec total size is 0 speedup is 0.00
5、通过排除文件推送数据
[root@nfs01 data]# cat /tmp/file.txt ssss eee g [root@nfs01 data]# ls a aaa b bbb c ccc ddd eee ssss [root@nfs01 data]# rsync -avz --exclude-from=/tmp/file.txt /data rsync_backup@172.16.2.41::backup/ --password-file=/etc/rsync.password sending incremental file list data/ data/a data/aaa data/b data/bbb data/c data/ccc data/ddd sent 443 bytes received 161 bytes 402.67 bytes/sec total size is 0 speedup is 0.00
6、无差异同步(--delete)
注意:使用—delete选项,即本地有啥,远程存储节点就业啥,如果远程存储节点有别的数据。那么这些“别的数据”将全部被删除掉,所有慎用这个--delete选项。
#远端存储节点的数据如下 [root@backup01 backup]# ls data [root@backup01 backup]# ls data/ a aaa b bbb c ccc ddd #本地数据如下 [root@nfs01 data]# ls a b c d e f g #本地数据推送到远程存储服务器 [root@nfs01 data]# rsync -avz --delete /data rsync_backup@172.16.2.41::backup/ --password-file=/etc/rsync.password sending incremental file list deleting data/ddd deleting data/ccc deleting data/bbb deleting data/aaa data/ data/a data/b data/c data/d data/e data/f data/g sent 418 bytes received 209 bytes 1,254.00 bytes/sec total size is 0 speedup is 0.00 #再次查看远程存储服务器的备份数据内容发现 {aaa bbb ccc}这三个文件被删除了 [root@backup01 backup]# ls data [root@backup01 backup]# ls data/ a b c d e f g
8.7 限制速度推送数据(--bwlimit=KBPS)
#未限速 [root@nfs01 data]# dd if=/dev/zero of=test1 bs=1M count=128 [root@nfs01 data]# rsync -avz /data rsync_backup@172.16.2.41::backup/ --password-file=/etc/rsync.password sending incremental file list data/ data/test1 sent 130,760 bytes received 47 bytes 23,783.09 bytes/sec total size is 134,217,728 speedup is 1,026.07 #限速 [root@nfs01 data]# rsync -avz --bwlimit=10k /data rsync_backup@172.16.2.41::backup/ --password-file=/etc/rsync.password sending incremental file list data/ data/test1 sent 130,760 bytes received 47 bytes 9,689.41 bytes/sec total size is 134,217,728 speedup is 1,026.07