rsync远程同步

1、配置rsync源服务器

        rsync是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,保持链接和权限,且采用优化的同步算法。

[root@localhost ~]# rpm -q rsync
rsync-3.1.2-4.el7.x86_64

        在远程同步任务中,负责发起rsync同步操作的客户机称为发前端,而负责响应来自客户机的rsync同步操作的服务器称为同步源。在同步过程中,同步与负责提供文档的原始位置,发前端应对该位置具有读取权限。

        配置rsync同步源需要建立配置文件rsyncd.conf,创建备份账号,然后将rsync程序以守护进程("--daemon"选项)方式运行。

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

        配置文件rsyncd.conf位于/etc目录下。

[root@localhost ~]# cat /etc/rsyncd.conf 

uid = nobody
gid = nobody
use chroot = yes
address = 192.168.136.24
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
[wwwroot]
    path = /var/www/html
    comment = Document Root of www.bdqn.com
    read only = yes
    dont compress = *.gz *.bz2 *.taz *.zip *.rar 
    auth users = backuper
    secrets file = /etc/rsyncd_users.db

rsync远程同步_第1张图片

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

        创建账号数据文件,添加一行用户记录,以冒号分隔,用户名称为"backuper",密码为"pwd123".由于账号信息采用明文存放,因此应调整文件权限,避免账号信息泄露。

[root@localhost ~]# cat /etc/rsyncd_users.db
backuper:pwd123
[root@localhost ~]# chmod 600 /etc/rsyncd_users.db 

        备份用户backuper需要对源目录/var/www/html有相应的读取权限,实际上只要other组有读取权限,则备份用户backuper和运行用户nobody也就有读取权限了。

[root@localhost ~]# ll -d /var/www/html/
drwxr-xr-x 2 root root 6 11月  5 2018 /var/www/html/

3)启动rsync服务程序,运行参数为"--daemon"。

        执行"rsync --daemon"命令就可以启动rsync服务,以独立监听服务的方式运行。若要关闭rsync服务,可以采取kill进程的方式,如kill $(cat /var/run/rsyncd.pid)

[root@localhost ~]# rsync --daemon
[root@localhost ~]# netstat -anpt | grep rsync
tcp        0      0 192.168.136.24:873      0.0.0.0:*               LISTEN      63516/rsync   

2、使用rsync备份工具

2.1、rsync命令的基本用法

        最简单的rsync用法类似于cp命令。例如,可以将文件/etc/fstab、目录/boot/grub同步备份到/opt目录下,其中"-t"选择表示递归整个目录树,"-l"选项用来备份链接文件。

[root@localhost ~]# rsync /etc/fstab /opt/
[root@localhost ~]# rsync -rl /etc/fstab /boot/grub /opt/

1)命令格式及常用备份选项 

        备份的基本格式为"rsync [选项] 原始位置 目标位置"

rsync远程同步_第2张图片

2)配置源的表示方法

[root@localhost ~]# rsync -avz [email protected]::wwwroot /root
[root@localhost ~]# ls 
anaconda-ks.cfg  initial-setup-ks.cfg  test.html  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@localhost ~]# cat test.html 
www.bdqn.com

或者第二种方式

[root@localhost ~]# rsync -avz rsync://[email protected]/wwwroot /root
Password: 
receiving incremental file list
./
test.html

sent 46 bytes  received 124 bytes  6.94 bytes/sec
total size is 13  speedup is 0.08

2.2、rsync备份操作示例

        访问源服务器中的wwwroot共享模块,并下载到本地的/myweb目录下

[root@localhost ~]# mkdir /myweb
[root@localhost ~]# rsync -avzH --delete [email protected]::wwwroot /myweb
Password: 
receiving incremental file list
./
test.html

sent 50 bytes  received 128 bytes  50.86 bytes/sec
total size is 13  speedup is 0.07
[root@localhost ~]# ls /myweb/  //确认同步结果
test.html

        实际生产环境中的备份工作通常是按照计划重复执行的,例如,每天晚上22:30对服务器的网站目录做一次同步,定期任务可以交给crond服务来完成。

        为了在同步过程不用输入密码,需要创建一个密码文件,保持backuper用户的密码,如/etc/server.pass。在执行rsync同步时使用选项"--password-file=/etc/server.pass"指定即可

[root@localhost ~]# cat /etc/server.pass
pwd123
[root@localhost ~]# chmod 600 /etc/server.pass 
[root@localhost ~]# crontab -e
30 22 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass  
[root@localhost ~]# systemctl restart crond
[root@localhost ~]# systemctl enable crond                                     

 3、配置inotify+rsync实时同步

        Linux内核从2.6.13版本开始提供了inotify通知接口,用来监控文件系统的各种变化情况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。

3.1、调整inotify内核参数

[root@localhost ~]# cat /proc/sys/fs/inotify/max_queued_events 
16384
[root@localhost ~]# cat /proc/sys/fs/inotify/max_user_instances 
128
[root@localhost ~]# cat /proc/sys/fs/inotify/max_user_watches 
8192

        当要监控的目录、文件数量较多或者变化较频繁时,建议加大这三个参数的值。

[root@localhost ~]# cat /etc/sysctl.conf 

fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

[root@localhost ~]# sysctl -p
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

3.2、安装inotify-tools

[root@localhost ~]# tar zxf inotify-tools-3.14.tar.gz 
[root@localhost ~]# cd inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# ./configure 
[root@localhost inotify-tools-3.14]# make
[root@localhost inotify-tools-3.14]# make install

        以监控网站目录/var/www/html为例,可以先执行"inotifywait"命令,然后在另一个终端向/var/www/html目录下添加文件、移动文件,跟踪屏幕输出结果。

rsync远程同步_第3张图片

 3.3、编写触发式同步脚本

        使用inotifywait输出的监控结果中,每行记录中依次包括目录、事件、文件,据此可以识别变动情况。

[root@localhost ~]# cat /opt/inotify_rsync.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /var/www/html"
RSYNC_CMD="rsync -rav /var/www/html [email protected]:/myweb"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
  $RSYNC_CMD
done

[root@localhost ~]# chmod +x /opt/inotify_rsync.sh 
[root@localhost ~]# echo '/opt/inotify_rsync.sh' >> /etc/rc.local 

3.4、测试脚本运行

在源服务器运行脚本,并修改/var/www/html目录中的数据

[root@localhost opt]# ./inotify_rsync.sh 
[root@localhost html]# echo "123" >> test.html 

查看结果

[root@localhost html]# cat test.html 
www.bdqn.com
123
123
123

你可能感兴趣的:(Linux高级管理,linux)