rsync三种备份模式
1.本地备份模式
Local: rsync [OPTION...] SRC... [DEST]
##本地 rsync 选项 源 目标
[root@nfs ~]# rsync -avz /etc /opt
#将/etc目录备份到/opt目录下,和cp的区别在于会自动增量备份
2.远程备份
Access via remote shell:
#将远端文件拉(下载)到本地
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
#将本地文件推送(上传)到远端
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
#将etc推送到172.16.1.41下的自定义文件夹下
[root@nfs ~]# rsync -az /etc/ root@172.16.1.41:/opt/etc-$(hostname)-$(date +%F)
3.守护进程模式
安装
[root@backup ~]# yum install rsync -y
配置
[root@backup ~]# vim /etc/rsyncd.conf
uid = rsync # 运行进程的用户
gid = rsync # 运行进程的用户组
port = 873 # 监听端口
fake super = yes # 无需让rsync以root身份运行,允许接收文件的完整属性
use chroot = no # 禁锢推送的数据至某个目录, 不允许跳出该目录
max connections = 200 # 最大连接数
timeout = 600 # 超时时间
ignore errors # 忽略错误信息
read only = false # 对备份数据可读写
list = false # 不允许查看模块信息
auth users = rsync_backup # 定义虚拟用户,作为连接认证用户
secrets file = /etc/rsync.passwd # 定义rsync服务用户连接认证密码文件路径
##############################################
[backup] # 定义模块信息
comment = commit # 模块注释信息
path = /backup # 定义接收备份数据目录
1.需要一个rsync进程运行时使用的普通用户--->rsync
useradd rsync -M -s /sbin/nologin
2.服务端需要定义一个客户端连接 虚拟用户名和密码
echo "rsync_backup:123456" > /etc/rsync.passwd
chmod 600 /etc/rsync.passwd
3.准备模块定义好的目录,客户端的数据都存放在该目录下
mkdir /backup
chown -R rsync.rsync /backup
4.启动rsync,并加入开机自启
systemctl start rsyncd
systemctl enable rsyncd
systemctl status rsyncd
5.检查rsync的进程端口
ps aux | grep rsync
netstat -lntp|grep 873
测试命令
#将本地/opt目录下的文件推送到172.16.1.41下的backup模块下
[root@nfs ~]# rsync -avz /opt/ rsync_backup@172.16.1.41::backup
#将172.16.1.41下的backup模块下的文件拉倒本地/opt下
[root@nfs ~]# rsync -avz rsync_backup@172.16.1.41::backup /opt
无差异(删除多余命令),很危险,不建议使用
无差异推送,以本地/opt为准
[root@nfs ~]# rsync -avz --delete /opt/ rsync_backup@172.16.1.41::backup
Password:
sending incremental file list
deleting test9 #删除backup模块下多的文件
deleting test8
deleting test7
sent 56,698 bytes received 745 bytes 22,977.20 bytes/sec
total size is 32,055,652 speedup is 558.04
####无差异拉取,以远端源为准
[root@nfs ~]# rsync -avz --delete rsync_backup@172.16.1.41::backup ./
Password:
receiving incremental file list
deleting .ssh/known_hosts
deleting .ssh/
deleting .pki/nssdb/
deleting .pki/
deleting test9
deleting test8
deleting .viminfo
deleting .tcshrc
deleting .mysql_history
deleting .cshrc
deleting .bashrc
deleting .bash_profile
deleting .bash_logout
deleting .bash_history
sent 658 bytes received 56,704 bytes 16,389.14 bytes/sec
total size is 32,057,242 speedup is 558.86
这条命令直接把root用户的环境变量都同步删除掉了,很危险,慎重使用
限速
--bwlimit 限速
[root@nfs ~]# rsync -azP --bwlimit 1M ./ rsync_backup@172.16.1.41::backup
sending incremental file list
./
root.tar.gz
11,869,568 100% 1.03MB/s 0:00:10 (xfr#1, to-chk=0/5)
无密码同步
1.方式一: --password-file 准备一个客户端的密码文件
[root@nfs ~]# echo "123456" > 1.txt
[root@nfs ~]# chmod 600 1.txt
[root@nfs ~]# rsync -avz rsync_backup@172.16.1.41::backup ./ --password-file=/root/1.txt
receiving incremental file list
./
sent 661 bytes received 56,711 bytes 10,431.27 bytes/sec
total size is 32,057,242 speedup is 558.76
2.方式二
[root@nfs ~]# export RSYNC_PASSWORD=123456 #临时配置环境变量
[root@nfs ~]# rsync -avz rsync_backup@172.16.1.41::backup ./
receiving incremental file list
sent 658 bytes received 56,704 bytes 114,724.00 bytes/sec
total size is 32,057,242 speedup is 558.86