1.rsync简介
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
2.rsync特性
- 可以镜像保存整个目录树和文件系统
- 可以很容易做到保持原来文件的权限、时间、软硬链接等等
- 无须特殊权限即可安装
- 快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽
- 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接
- 支持匿名传输,以方便进行网站镜像
3. rsync的ssh认证协议
rsync命令来同步系统文件之前要先登录remote主机认证,认证过程中用到的协议有2种:
- ssh协议
- rsync协议
rsync server端不用启动rsync的daemon进程,只要获取remote host的用户名和密码就可以直接rsync同步文件
rsync server端因为不用启动daemon进程,所以也不用配置文件/etc/rsyncd.conf
ssh认证协议跟scp的原理是一样的,如果在同步过程中不想输入密码就用ssh-keygen -t rsa打通通道
4.rsync命令
环境说明:
服务器类型 | IP地址 | 应用 | 操作系统 |
---|---|---|---|
源服务器 | 192.168.20.99 | rsync | centos7/redhat7 |
目标服务器 | 192.168.20.120 | rsync | centos7/redhat7 |
1.拷贝到本地
[root@rsync1 ~]# rsync -avz anaconda-ks.cfg abc
sending incremental file list
anaconda-ks.cfg
sent 957 bytes received 31 bytes 1976.00 bytes/sec
total size is 1607 speedup is 1.63
[root@rsync1 ~]# ls
abc anaconda-ks.cfg
[root@rsync1 ~]#
2.拷贝到另一台服务器
[root@rsync1 ~]# rsync -avz abc 192.168.20.120:/root/
root@192.168.20.120's password:
sending incremental file list
abc
sent 945 bytes received 31 bytes 177.45 bytes/sec
total size is 1607 speedup is 1.65
[root@rsync1 ~]#
[root@rsync2 ~]# ls
abc anaconda-ks.cfg initial-setup-ks.cfg
[root@rsync2 ~]#
3.远程机器的内容拷贝到本地机器
[root@rsync1 ~]# rsync -avz 192.168.20.120:/zzl .
root@192.168.20.120's password:
receiving incremental file list
zzl/
sent 15 bytes received 44 bytes 23.60 bytes/sec
total size is 0 speedup is 0.00
[root@rsync1 ~]# ls
abc anaconda-ks.cfg zzl
[root@rsync1 ~]#
//rsync常用选项:
-a, --archive //归档
-v, --verbose //啰嗦模式
-q, --quiet //静默模式
-r, --recursive //递归
-p, --perms //保持原有的权限属性
-z, --compress //在传输时压缩,节省带宽,加快传输速度
--delete //在源服务器上做的删除操作也会在目标服务器上同步
5. rsync+inotify
环境说明:
服务器类型 | IP地址 | 应用 | 操作系统 |
---|---|---|---|
源服务器 | 192.168.20.99 | rsync inotify-tools 脚本 | centos7/redhat7 |
目标服务器 | 192.168.20.120 | rsync | centos7/redhat7 |
把源服务器上/runtime目录实时同步到目标服务器的/zzl/下
服务器都要关闭防火墙和selinux
在目标服务器上做以下操作:
[root@rsync1 ~]# yum -y install rsync
[root@rsync2 ~]# vim /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
[runtime]
path = /zzl/
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 = 120
auth users = admin
hosts allow =192.168.20.99
//创建用户认证文件
[root@rsync2 ~]# echo 'admin:123456' > /etc/rsync.pass
[root@rsync2 ~]# cat /etc/rsync.pass
admin:123456
//设置文件权限
[root@rsync2 ~]# chmod 600 /etc/rsync.pass
[root@rsync2 ~]# ll /etc/rsync.pass
-rw------- 1 root root 13 7月 27 18:24 /etc/rsync.pass
//设置开机自启
[root@rsync2 ~]# systemctl enable --now rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@rsync2 ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
Active: active (running) since 一 2020-07-27 18:27:01 CST; 16s ago
Main PID: 70688 (rsync)
CGroup: /system.slice/rsyncd.service
└─70688 /usr/bin/rsync --daemon --no-detach
7月 27 18:27:01 rsync2 systemd[1]: Started fast remote file co...
7月 27 18:27:01 rsync2 systemd[1]: Starting fast remote file c...
Hint: Some lines were ellipsized, use -l to show in full.
在源服务器上做以下操作:
//配置yum源
[root@rsync1 ~]# rpm -ivh http://mirror.centos.org/centos/7/os/x86_64/Packages/wget-1.14-18.el7_6.1.x86_64.rpm
[root@rsync1 ~]# cd /etc/yum.repos.d/
[root@rsync1 yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@rsync1 yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@rsync1 yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@rsync1 yum.repos.d]# yum -y install epel-release
//安装rsync服务端软件
[root@rsync1 ~]# yum -y install rsync
[root@rsync1 ~]# yum -y install inotify-tools
//创建认证密码文件
[root@rsync1 ~]# echo '123456' > /etc/rsync.pass
[root@rsync1 ~]# cat /etc/rsync.pass
123456
//设置文件权限,只设置文件所有者具有读取、写入权限
[root@rsync1 ~]# chmod 600 /etc/rsync.pass
[root@rsync1 ~]# ll /etc/rsync.pass
-rw------- 1 root root 7 7月 27 18:44 /etc/rsync.pass
//在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@rsync1 ~]# mkdir /runtime
[root@rsync1 ~]# ll /runtime/
总用量 0
drwxr-xr-x 2 root root 6 7月 27 18:06 123
-rw-r--r-- 1 root root 0 7月 27 18:06 abc
-rw-r--r-- 1 root root 0 7月 27 18:06 def
[root@rsync1 ~]#
[root@rsync1 ~]# rsync -avH --port 873 --progress --delete /runtime/ admin@192.168.20.120::runtime --password-file=/etc/rsync.pass
runtime/
runtime/abc
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=2/4)
runtime/def
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=1/4)
runtime/123/
sent 172 bytes received 54 bytes 150.67 bytes/sec
total size is 0 speedup is 0.00
[root@rsync1 ~]#
//查看是否同步
[root@rsync2 ~]# ll /zzl
总用量 0
drwxr-xr-x 2 root root 6 7月 27 18:06 123
-rw-r--r-- 1 root root 0 7月 27 18:06 abc
-rw-r--r-- 1 root root 0 7月 27 18:06 def
[root@rsync2 ~]#
//查看服务器内核是否支持inotify
[root@rsync1 ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 7月 27 18:46 max_queued_events
-rw-r--r-- 1 root root 0 7月 27 18:46 max_user_instances
-rw-r--r-- 1 root root 0 7月 27 18:46 max_user_watches
[root@rsync1 ~]#
//编写脚本
[root@rsync1 ~]# mkdir /scripts
[root@rsync1 ~]# touch /scripts/inotify.sh
[root@rsync1 ~]# chmod 755 /scripts/inotify.sh
[root@rsync1 ~]# ll /scripts/
总用量 0
-rwxr-xr-x 1 root root 0 7月 27 18:48 inotify.sh
[root@rsync1 ~]# vim /scripts/inotify.sh
host=192.168.20.120
src=/runtime
des=runtime
password=/etc/rsync.pass
user=admin
/usr/bin/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 $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
//启动脚本
[root@rsync1 ~]# nohup /bin/bash /scripts/inotify.sh &
[1] 64791
[root@rsync1 ~]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@rsync1 ~]# jobs
[1]+ 运行中 nohup /bin/bash /scripts/inotify.sh &
[root@rsync1 ~]#
//设置脚本开机自启
[root@rsync1 ~]# chmod +x /etc/rc.d/rc.local
[root@rsync1 ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 6月 27 2017 /etc/rc.d/rc.local
[root@rsync1 ~]# echo 'nohup /bin/bash /scripts/inotify.sh &' >> /etc/rc.d/rc.local
[root@rsync1 ~]# tail -2 /etc/rc.d/rc.local
touch /var/lock/subsys/local
nohup /bin/bash /scripts/inotify.sh &
[root@rsync1 ~]#