rsync远程同步

rsync远程同步

实验材料

两台centos7
一台同步源:192.168.100.102
一台客户端:192.168.100.103

Xshell连接

配置rsync源服务器

进入102同步源
[root@CentOS7-02 ~]# rpm -qa rsync (一般系统自带这个包)
rsync-3.0.9-18.el7.x86_64

1)建立/etc/rsyncd.conf配置文件

[root@CentOS7-02 ~]# vim /etc/rsyncd.conf (进去把所有的文字都删了,按我下面来)
(后面有我打好的,你可以直接复制)
在这里插入图片描述
uid = nobody
gid = nobody
use chroot = yes
address = 192.168.100.102
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.log
hosts allow = 192.168.100.0/24
[wwwroot]
path = /var/www/html
comment = ajbn
read only = yes
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = backuper
secrets file = /etc/rsyncd_users.db

2)为备份账号创建数据文件

[root@CentOS7-02 ~]# vim /etc/rsyncd_users.db (这个文件是手动打的,格式要和我一样)
backuper:pwd123
上面填的是对应刚刚那个文件里的用户,不需要创建本地用户(这句话不用填进去)
[root@CentOS7-02 ~]# chmod 600 /etc/rsyncd_users.db (权限要给,不让会报错)
[root@CentOS7-02 ~]# mkdir -p /var/www/html (这是主配置文件中的目录,创建一下)
[root@CentOS7-02 ~]# cd /var/www/html/
[root@CentOS7-02 html]# mkdir 102
[root@CentOS7-02 html]# touch 112
(上面是随便创建的,一会方便看效果)
[root@CentOS7-02 ~]# rsync --daemon
[root@CentOS7-02 ~]# netstat -anpt | grep rsync
tcp 0 0 192.168.100.102:873 0.0.0.0:* LISTEN 15021/rsync

使用rsync远程备份工具

进入客户端103中

1)rsync命令基本语法

[root@centos7-03 ~]# rsync -rl /etc/fstab /boot/grub /boot/把/etc/fstab文件,/boot/grub目录树备份到/opt目录下,类似cp命令,
-r:表示递归整个目录树 ; -l :用来备份链接文件)

2)配置源的表示方法
1.
在这里插入图片描述
2.
[root@centos7-03 ~]# mkdir myweb
[root@centos7-03 ~]# cd myweb/
[root@centos7-03 myweb]# rsync -avz backuper@192.168.100.102::wwwroot /root/myweb
然后到同步源102中的/var/www/html中
rm -rf 112
然后回到客户机103中 (执行如下命令)
在这里插入图片描述

3)结合任务计划备份实例

[root@centos7-03 ~]# vim /etc/server.pass
pwd123 (这是那个backuper用户的密码,这样可以免交换)
[root@centos7-03 ~]# chmod 600 /etc/server.pass (给权限要不会报错)
[root@centos7-03 backup]# date
2019年 09月 01日 星期日 04:55:55 CST
[root@centos7-03 ~]# crontab -e
在这里插入图片描述
[root@centos7-03 backup]# cd /backup/
[root@centos7-03 backup]# ls
102

配置inotify+rsync实时同步

需要的包
链接:https://pan.baidu.com/s/1-YNg6SUhTk4Y4UR6x3pZvg
提取码:qed0

进入客户端103中

1.调整inotify参数

查看默认参数
我下面的第二个行,是max_user_instances
在这里插入图片描述
如果你监控的文件数量多的话要改一些
[root@centos7-03 ~]# vim /etc/sysctl.conf (进入后随便加到哪)
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

[root@centos7-03 ~]# sysctl -p (会显示刚刚加的项不是报错)

2.安装inotify-tools

把包托到Xshell中
[root@centos7-03 inotify-tools-3.14]# tar zxf inotify-tools-3.14.tar.gz
[root@centos7-03 inotify-tools-3.14]# cd inotify-tools-3.14
[root@centos7-03 inotify-tools-3.14]# ./configure
[root@centos7-03 inotify-tools-3.14]# make && make install

回到root目录
[root@centos7-03 ~]# mkdir -p /var/www/html
[root@centos7-03 ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html (会显示空白不是报错)
复制一个终端103的就是再连一个103终端
[root@centos7-03 ~]# cd /var/www/html/
[root@centos7-03 html]# touch yyy
[root@centos7-03 html]# touch iii
回到原来的监控终端查看
在这里插入图片描述

3.编写触发式同步脚本

还是在客户端103上操作
[root@centos7-03 ~]# vim /opt/backup.sh (自己创建的)
后面有我打好的
在这里插入图片描述
#!/bin/bash
inotify_cmd="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"
rsync_cmd="rsync -azH --delete --password-file=/etc/server.pass /var/www/html backuper@192.168.100.102::wwwroot"
$inotify_cmd | while read DIRECTORY EVENT FILE
do
$rsync_cmd
done

[root@centos7-03 ~]# chmod 600 /etc/server.pass (如果你没有创建,创建一下,里面写backuper用户的密码,我前面已经创建了)

进入同步源102中
[root@CentOS7-02 ~]# vim /etc/rsyncd.conf
read only = no (改一些这项刚刚是yes的改成no)
[root@CentOS7-02 www]# setfacl -R -m user:nobody:rwx /var/www/html/
[root@CentOS7-02 www]# setcacl -R -m default:nobody:rwx /var/www/html/
nobody是backuper用户映射的本地账号,要有写入的权限

回到客户端103中
[root@centos7-03 ~]# cd /var/www/html/
[root@centos7-03 html]# sh /opt/backup.sh & (后台运行)
[root@centos7-03 html]# mkdir aaa
[root@centos7-03 html]# touch 333

验证同步

进入同步源102
在这里插入图片描述

实验成功

如果想停止对文件的监控
可以回到客户端103
[root@centos7-03 ~]# jobs -l
[1]+ 17676 运行中 sh /opt/backup.sh &(工作目录:/var/www/html)
[root@centos7-03 ~]# kill -9 17676
这样就不会同步了

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值