rsync简介
rsync是Linux系统下的数据镜像备份工具,使用快速增量备份工具 Remote sync 可以远程同步,支持本地复制,或者与其他SSH,rsync主机同步
rsync
-a 归档模式
-v 详细输出
-q 静默输出
-r 对子目录递归模式处理
-p 保持原有的权限属性
-z 在传输时压缩
--delete 在源服务器上做删除操作也会在目标服务器上同步
-c 打开效验开关
-R 使用相对路径
-b 创建备份
rsync命令
//Rsync的三种命令格式
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
//对应以上三种命令格式,rsync有三种不同的工作模式:
1.拷贝本地文件
[root@localhost ~]# rsync -a nfs.sh a.sh
2.使用远程shell程序(rsh,ssh)来实现将本地机器的内容拷贝到远程机器
[root@localhost ~]# rsync -avz nfs.sh [email protected]:/root/b.sh
[root@localhost ~]# ssh [email protected] 'ls -l /root'
3.使用一个远程shell程序(如rsh,ssh)来实现将远程机器的内容拷贝到本地机器
[root@localhost ~]# rsync -avz [email protected]:/etc/yum.repos.d /root/
环境说明
A机——源服务器——IP192.168.56.11——应用(rsync,inotify-tools,脚本 )——centos7系统
B机——目标服务器——IP192.168.56.138——应用(rsync)——centos7系统
使用ssh,传输密钥给B机,方便免密码登陆
[root@localhost ~]# ssh-keygen -t rsa //密钥生成
[root@localhost ~]# ls .ssh/
id_rsa id_rsa.pub
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] //A机将生成的公钥交给B机
[root@localhost ~]# ssh [email protected] //尝试登陆,发现无需密码
[root@localhost ~]# exit //退出B机
rsync+inotify
需求:把源服务器上/etc目录实时同步到目标服务器的/tmp/下
安装环境 A B 先关闭防火墙
[root@localhost ~]# systemctl status firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
AB机安装rsync
[root@localhost ~]# yum -y install rsync A机
[root@localhost ~]# yum -y install rsync B机
B机设置rsyncd.conf配置文件
[root@localhost ~]# touch /etc/rsync.pass log file = /var/log/rsyncd.log [root@localhost ~]# echo 'admin:123456' > /etc/rsync.pass [root@localhost ~]# chmod 600 /etc/rsync [root@localhost ~]# systemctl start rsyncd A机 [root@localhost ~]# systemctl stop firewalld //安装rsync服务端软件,只需要安装,不要启动,不需配置 //设置文件权限,只设置文件所有者具有读取,写入权限即可 [root@localhost ~]# ls //创建测试目录 [root@localhost ~]# mkdir -pv /root/etc/test [root@localhost ~]# rsync -avH --port 873 --progress --delete /root/etc/ [email protected]::etc_from_client --password-file=/etc/rsync.pass sending incremental file list sent 75 bytes received 670 bytes 1,490.00 bytes/sec B机 [root@localhost ~]# ls /tmp //安装inotify-tools A机 //写同步脚本,最重要的一步,让脚本自动去检测我们制定的目录下 \ [root@localhost ~]# vim /scripts/inotify.sh $inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \ //检查脚本 //启动脚本 [root@localhost bin]# ps -ef|grep inotify //在源服务器上生成一个新文件 //查看inotify生成的日志 设置开机自启动 [root@localhost ~]# chmod +x /etc/rc.d/rc.local [root@localhost ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local In contrast to previous versions due to parallel execution during boot Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure 到目标服务器上查看是否把新生成的文件自动传上去了: [root@localhost tmp]# pwd [root@localhost tmp]# ls etc/httpd24/
[root@localhost ~]# cat >> /etc/rsyncd.conf <
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
[etc_from_client]
path = /tmp/
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 = admin
hosts allow = 192.168.56.11
hosts deny = 192.168.1.1
EOF
//创建用户认证文件,设置文件权限, 启动rsync服务并设置开机自启动
[root@localhost ~]# ll /etc/rsync
-rw-------. 1 root root 841 8月 14 01:14 /etc/rsyncd.conf
-rw-------. 1 root root 13 8月 14 01:18 /etc/rsync.pass
[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 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 :873 :
LISTEN 0 5 :::873 :::
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=)./\1disabled/g' /etc/sysconfig/selinux
//配置yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@localhost yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost yum.repos.d]# sed -i 's/^enabled=./enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost yum.repos.d]# yum -y install epel-release
[root@localhost ~]# yum -y update --skip-broken
[root@localhost ~]# yum -y install rsync
[root@localhost ~]# echo '123456' > /etc/rsync.pass
[root@localhost ~]# cat /etc/rsync.pass
123456
[root@localhost ~]# chmod 600 /etc/rsync.pass
[root@localhost ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 8月 13 17:51 /etc/rsync.pass
anaconda-ks.cfg
mkdir: 已创建目录 "/root/etc"
mkdir: 已创建目录 "/root/etc/test"
deleting systemd-private-5434e220d18940898b6d9672af036026-vmtoolsd.service-MRNEKi/tmp/vmware-root/
deleting systemd-private-5434e220d18940898b6d9672af036026-vmtoolsd.service-MRNEKi/tmp/
deleting systemd-private-5434e220d18940898b6d9672af036026-vmtoolsd.service-MRNEKi/
deleting systemd-private-5434e220d18940898b6d9672af036026-vgauthd.service-EHAGL9/tmp/
deleting systemd-private-5434e220d18940898b6d9672af036026-vgauthd.service-EHAGL9/
deleting systemd-private-5434e220d18940898b6d9672af036026-cups.service-cQXF9F/tmp/
deleting systemd-private-5434e220d18940898b6d9672af036026-cups.service-cQXF9F/
deleting .font-unix/
deleting .esd-1000/
deleting .XIM-unix/
deleting .X11-unix/
deleting .Test-unix/
deleting .ICE-unix/
./
test/
total size is 0 speedup is 0.00
test
[root@localhost ~]# ll /proc/sys/fs/inotify/ 查看服务器是否支持inotify,有三max则支持
总用量 0
-rw-r--r--. 1 root root 0 8月 14 02:04 max_queued_events
-rw-r--r--. 1 root root 0 8月 14 02:04 max_user_instances
-rw-r--r--. 1 root root 0 8月 14 02:04 max_user_watches
[root@localhost ~]# yum -y install make gcc gcc-c++
[root@localhost ~]# yum -y install inotify-tools
//文件发生的变化,然后执行rsunc的命令把它同步到服务器端
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# touch /scripts/inotify.sh
[root@localhost ~]# chmod 755 /scripts/inotify.sh
[root@localhost ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 0 8月 13 18:15 /scripts/inotify.sh
host=192.168.56.138
src=/etc
des=etc_from_client
password=/etc/rsync.pass
user=admin
inotifywait=/usr/bin/inotifywait
| 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@localhost bin]# bash -x /scripts/inotify.sh
[root@localhost ~]# nohup bash /scripts/inotify.sh &
[1] 58297
root 74599 1 0 17:01 pts/0 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
root 74621 2316 0 17:03 pts/0 00:00:00 bash /scripts/inotify.sh
root 74622 74621 0 17:03 pts/0 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
root 74623 74621 0 17:03 pts/0 00:00:00 bash /scripts/inotify.sh
root 74625 2316 0 17:03 pts/0 00:00:00 grep --color=auto inotify
[root@localhost ~]# mkdir -p /etc/httpd24
[root@localhost ~]# ls /etc/httpd24
[root@localhost ~]# echo 'hello world' > /etc/httpd24/test
[root@localhost ~]# tail /tmp/rsync.log
20180816 17:05 /etc/httpd24CREATE,ISDIR was rsynced //创建,ISDIR是同步的
20180816 17:05 /etc/httpd24/testCREATE was rsynced //同步创建
20180816 17:05 /etc/httpd24/testMODIFY was rsynced //同步修改
[root@localhost ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 4月 11 15:36 /etc/rc.d/rc.local
[root@localhost ~]# tail /etc/rc.d/rc.local
to run scripts during boot instead of using this file.
this script will NOT be run after all other services.
that this script will be executed during boot.
touch /var/lock/subsys/local
nohup /bin/bash /scripts/inotify.sh
/tmp
[root@localhost tmp]# ls
etc test
test
//以将源服务器的/etc目录整个同步到了目标服务器,新增的test文件也自动同步了