参数 | 解释 |
---|---|
-a | #归档模式传输, 等于-tropgDl |
-v | #详细模式输出, 打印速率, 文件数量等 |
-z | #传输时进行压缩以提高效率 |
-r | #递归传输目录及子目录,即目录下得所有目录都同样传输。 |
-t | #保持文件时间信息 |
-o | #保持文件属主信息 |
-p | #保持文件权限 |
-g | #保持文件属组信息 |
-l | #保留软连接 |
-D | #保持设备文件信息 |
-P | #显示同步的过程及传输时的进度等信息 |
-L | #保留软连接指向的目标文件 |
-e | #使用的信道协议,指定替代rsh的shell程序 |
--exclude=PATTERN | #指定排除不需要传输的文件模式 |
--exclude-from=file | #文件名所在的目录文件 |
--bwlimit=100 | #限速传输 |
--delete | #让目标目录和源目录数据保持一致 |
服务段
1.安装服务
[root@backup ~]# yum install rsyncd -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirrors.cqu.edu.cn
* updates: mirror.bit.edu.cn
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
2.修改配置文件
[root@backup ~]# vim /etc/rsyncd.conf
#rsync_config
#created by HQ at 2017
##rsyncd.conf start##
uid = rsync --- 备份目录的管理用户(属主信息)???
gid = rsync --- 备份目录的管理用户(属组信息)???
port = 873 --- rsync守护进程服务端口号
fake(伪装) super = yes --- ???
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 --- 服务的日志文件
ignore errors --- 忽略错误,提高传输数据效率
read only = false --- 设置备份目录是可读可写
list = false --- ???
hosts allow = 172.16.1.0/24 --- 白名单(允许指定网段或者主机)
hosts deny = 0.0.0.0/32 --- 黑名单(拒绝指定网段或者主机)
auth users = rsync_backup --- 指定认证的用户
secrets file = /etc/rsync.password --- 创建一个密码文件(用户名称:用户密码)
[backup] --- 模块信息
comment = "backup dir by oldboy"
path = /backup --- 指定备份目录路径信息
3.根据配置创建一些初识环境
创建rsync运行用户
[root@backup ~]# useradd rsync -s /sbin/nologin -M
[root@backup ~]# id rsync
uid=1000(rsync) gid=1000(rsync) groups=1000(rsync
创建虚拟用户密码文件 并授权为600
[root@backup ~]#echo 'rsync_backup:123456' >/etc/rsync.password
[root@backup ~]# chmod 600 /etc/rsync.password
创建一个/backup目录 并授权给rsync
[root@backup ~]# mkdir /backup
[root@backup ~]# chown -R rsync. /backup
4.启动
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# systemctl enable rsyncd
5.检测
[root@backup ~]# netstat -lntp | grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 8616/rsync
客户端
创建密码文件,并修改权限
[root@nfs ~]# echo "123456" >/etc/rsync.password
[root@nfs ~]# chmod 600 /etc/rsync.password
应用
推送
把本地/root/1.txt 以root身份推送至对方/tmp 目录下
[root@nfs ~]# rsync -avz /root/1.txt root@172.16.1.41:/tmp
root@172.16.1.41's password:
sending incremental file list
1.txt
sent 84 bytes received 35 bytes 47.60 bytes/sec
total size is 0 speedup is 0.00
[root@backup tmp]# ls
1.txt
[root@backup tmp]#
拉取
把对方/root/2.txt 以root 身份拉取到己方/tmp
[root@nfs tmp]# rsync -avz root@172.16.1.41:/root/2.txt /tmp
root@172.16.1.41's password:
receiving incremental file list
2.txt
sent 43 bytes received 83 bytes 84.00 bytes/sec
total size is 0 speedup is 0.00
[root@nfs tmp]# ls
2.txt
[root@nfs tmp]#
使用虚拟用户推送
使用虚拟用户rsync_backup 、的backup模块把本地/root/1.txt 推送至对方到/backup目录下
[root@nfs ~]# rsync -avz /root/1.txt rsync_backup@172.16.1.41::backup
Password:
sending incremental file list
1.txt
sent 95 bytes received 43 bytes 39.43 bytes/sec
total size is 4 speedup is 0.03
[root@backup ~]# ls /backup/
1.txt
[root@backup ~]#
免密码使用虚拟用户推送
[root@nfs ~]# rsync -avz /root/4.txt rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
4.txt
sent 95 bytes received 43 bytes 276.00 bytes/sec
total size is 4 speedup is 0.03
delete 无差别同步
[root@nfs ~]# rsync -avz --delete rsync_backup@172.16.1.41::backup /root
Password:
receiving incremental file list
deleting .ssh/known_hosts
deleting .ssh/
deleting anaconda-ks.cfg
deleting .viminfo
deleting .tcshrc
deleting .cshrc
deleting .bashrc
deleting .bash_profile
deleting .bash_logout
deleting .bash_history
./
sent 27 bytes received 94 bytes 34.57 bytes/sec
total size is 4 speedup is 0.03
bwlimit 限速
推送的速度限速为5兆 默认单位=KB
[root@nfs ~]# rsync -avzP --bwlimit=5m ubuntu-18.04.1-desktop-amd64.iso rsync_backup@172.16.1.41::backup
sending incremental file list
ubuntu-18.04.1-desktop-amd64.iso 159,186,944 8% 5.12MB/s 0:05:41
--exclude 排除 不同步
排除 123.txt 124.txt
其他同步
[root@nfs ~]# vim /tmp/li.txt
123.txt
124.txt
[root@nfs ~]# rsync -avz /root/ --exclude-from=/tmp/li.txt rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
./
.viminfo
121.txt
122.txt
125.txt
sent 663 bytes received 103 bytes 1,532.00 bytes/sec
total size is 720 speedup is 0.94