rsync是一款开源的备份工具,可以在不同主机之间进行同步,
可实现全量备份与增量备份,因此非常适合用于架构集中式备份或异地备份等应用。
rsync监听端口:873
rsync运行模式:C/S 客户端/服务端 B/S 浏览器/服务端
关于数据同步的两种方式
01.rsync应用场景-上传(推)
会导致数据同步缓慢(适合少量数据备份)
02.rsync应用场景-下载(拉)
会导致备份服务器开销大
03.rsync应用场景-多server备份
04.异地备份实现思路
Rsync大致使用三种主要的数据传输方式
01.本地方式cp PC U盘
Local: rsync [OPTION...] SRC... [DEST]
[root@backup ~]# rsync /etc/passwd /tmp/
[root@backup ~]# ls /tmp/passwd
/tmp/passwd
02.远程方式scp
Access via remote shell: 远程传输
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] 下载(拉)
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST 上传(推)
下载pull``
[root@nfs ~]# pwd
/root
[root@nfs ~]# echo "This Nfs" > file
[root@backup ~]# rsync -avz [email protected]:/root/file /opt/
[root@backup ~]# cat /opt/file
This Nfs
上传push(将backup的file2文件上传至NFS服务器的/mnt目录)
[root@backup ~]# pwd
/root
[root@backup ~]# echo "This Rsync" > file2
[root@backup ~]# rsync -avz /root/file2 [email protected]:/mnt
[root@nfs ~]# cat /mnt/file2
This Rsync
推送目录(推送/root/目录下面的所有文件和目录,不会推送/root/目录本身)
[root@backup ~]# rsync -avz /root/ [email protected]:/tmp
推送目录,推送目录本身以及目录下面的所有文件
[root@backup ~]# rsync -avz /root [email protected]:/tmp
远程方式存在的缺陷:
1.需要使用系统用户(不安全)
2.使用普通用户(权限存在问题)
3.需要走ssh协议
03.守护进程(服务,持续后台运行)
Access via rsync daemon: 守护进程方式传输
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] 下载
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
首先环境说明
服务器说明 | ip地址 | 应用 | 系统 |
---|---|---|---|
源服务器 | 192.168.100.100 | rsync inotify tools脚本 | red hat7 |
目标服务器 | 192.168.100.129 | rsync | centos7 |
1.部署rsync+inotify同步/dxk目录至目标服务器/tmp/dxk下
部署rsync服务同步
部署rsync+inotify同步/qinyong目录至目标服务器/tmp/qinyong下
源服务器
关闭源服务器和目标服务器的防火墙和SELINUX
[root@localhost ~]# systemctl stop firewalld(源服务器)
[root@localhost ~]# getenforce
Disabled
[root@localhost ~]# systemctl stop firewalld(目标服务器)
[root@localhost ~]# getenforce
Disabled
[root@localhost ~]# yum install rsync -y
[root@localhost run]# echo 'qinyong:123456'>/etc/rsync.pass
[root@localhost run]# cat /etc/rsync.pass
qinyong:123456
[root@localhost ~]# cat >> /etc/rsyncd.conf < log file = /var/log/rsyncd.log
> pidfile = /var/run/rsyncd.pid
> lock file = /var/run/rsync.lock
> secrets file = /etc/rsync.pass
> [qinyong]
> path = /nihao/
> comment = sync etc from client
> uid = root
> gid = root
> port = 873
> ignore errors
> use chroot = no
> read only = no
> list = no
> max connections = 200
> timeout = 600
> auth users = qinyong
> EOF
[root@localhost ~]# chmod 600 /etc/rsync*
[root@localhost ~]# ll /etc/rsync*
-rw------- 1 root root 793 9月 19 10:03 /etc/rsyncd.conf
-rw------- 1 root root 16 9月 19 10:06 /etc/rsync.pass
[root@localhost ~]# systemctl start rsyncd
[root@localhost ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@localhost ~]# yum install epel-release -y
[root@localhost ~]# echo '123456' > /etc/rsync.pass
[root@localhost ~]# chmod 600 /etc/rsync.pass
[root@localhost ~]# ll /etc/rsync.pass
-rw------- 1 root root 8 9月 19 10:32 /etc/rsync.pass
[root@localhost ~]# mkdir -pv /root/nihao
mkdir: 已创建目录 "/root/nihao"
[root@localhost ~]# rsync -avH --port 873 --progress --delete /root/nihao/ [email protected]::qinyong --password-file=/etc/rsync.pass
sending incremental file list
./
.inotify.sh.swp
4,096 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=4/6)
abc
20 100% 26.53kB/s 0:00:00 (xfr#2, to-chk=3/6)
nohup.out
151 100% 230.46kB/s 0:00:00 (xfr#3, to-chk=2/6)
123/
aaaa/
sent 4,586 bytes received 92 bytes 9,356.00 bytes/sec
total size is 4,267 speedup is 0.91
[root@localhost ~]# yum install inotify-tools
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# touch inotify.sh
[root@localhost scripts]# chmod 755 inotify.sh
[root@localhost scripts]# vim inotify.sh
host=192.168.100.129
src=/root/nihao
des=qinyong
password=/etc/rsync.pass
user=root
inotifywait=/usr/bin/inotifywait
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files ;do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $u
ser@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
[root@localhost scripts]# nohup bash /scripts/inotify.sh &
[1] 59758
[root@localhost ~]# cd nihao/
[root@localhost nihao]# ls
55 da hehe nohup.out
[root@localhost tmp]# cd /qinyong/
[root@localhost qinyong]# ls
55 da hehe nohup.out
[root@localhost nihao]# mkdir qinyong
[root@localhost nihao]# ls
dad 555 aaa nihao nohup.out
[root@localhost nihaoi]# touch lala
[root@localhost qinyong]# ls
dad 555 aaa nihao nohup.out lala