Linux操作文档——rsync远程同步


一、配置rsync源服务器

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

[root@localhost ~]# vim /etc/rsyncd.conf
 #运行进程的用户
 uid = nobody
 #运行进程的组
 gid = nobody
 #禁锢在源目录
 use chroot = yes
 #模块的最大并发连接数量
 max connections = 10
 #监听地址
 address = 192.168.1.10
 #监听端口
 port 873
 #日志文件位置
 log file = /var/log/rsyncd.log
 #存放进程ID的文件位置
 pid file = /var/run/rsyncd.pid
 #允许访问的客户机地址
 hosts allow = 192.168.1.0/24
 #共享模块名称
[wwwroot]
		uid = root
        gid = root
        #源目录的实际位置
        path = /var/www/html
        comment = Document Root of www.admin.com
        #忽略一些无关的IO错误
        #ignore errors
        #是否为只读,当inotify所在的发起端要上传数据时设置为no
        read only = yes
        #同步时不再压缩的文件类型
        dont compress = *.gz *.bz2 *.ygz *.zip *.rar *.z
        #授权账户
        auth users = backuper
        #存放账户信息的数据文件
        secrets file = /etc/rsyncd_users.db

同步可以采用匿名的方式,只要将其中的"auth users"和"secrets file”配置记录去掉就可以了

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

[root@localhost ~]# vim /etc/rsyncd_users.db
backuper:pwd123               //无须建立同名系统用户
[root@localhost ~]# chmod 600 /etc/rsyncd_users.db 
[root@localhost ~]# mkdir -p /var/www/html

3、启动rsync服务程序,运行参数为"–daemon"

[root@localhost ~]# rsync --daemon
[root@localhost ~]# netstat -anpt | grep rsync
tcp        0      0 192.168.1.10:873        0.0.0.0:*               LISTEN      23296/rsync  
[root@localhost ~]# kill $(cat /var/run/rsyncd.pid)               //杀死进程
[root@localhost ~]# rsync --daemon --config=/etc/rsyncd.conf               //重启

二、使用rsync备份工具

1、rsync命令的基本用法

[root@localhost ~]# rsync /etc/fstab /opt/
[root@localhost ~]# rsync -rl /etc/fstab /boot/grub /opt/               //将文件/etc/fstab、目录/boot/grub同步备份到/opt目录下
常用备份选项说明常用备份选项说明
-r递归模式,包含目录及子目录中的所有文件-I对于符号链接文件仍然复制为符号链接文件
-v显示同步过程的详细信息-a归档模式,保留文件的权限、属性等信息
-z在传输文件时进行压缩-p保留文件的权限标记
-t保留文件的时间标记-g保留文件的属组标记(仅超级用户使用)
-o保留文件的属主标记(仅超级用户使用)-H保留硬连接文件
-A保留ACL属性信息-D保留设备文件及其他特殊文件
–delete删除目标位置有而原始位置没有的文件–checksum根据校验和(而不是文件大小、修改时间)来决定是否跳过文件

2、配置源的表示方法
将访问rsync同步源,将指定的资源下载到本地/root目录下进行备份

[root@localhost ~]# rsync -avz backuper@192.168.1.10::wwwroot /root
Password: 
receiving incremental file list
./

sent 64 bytes  received 97 bytes  10.39 bytes/sec
total size is 0  speedup is 0.00

[root@localhost ~]# rsync -avz rsync://backuper@192.168.1.10/wwwroot /root
Password: 
receiving incremental file list

sent 61 bytes  received 94 bytes  28.18 bytes/sec
total size is 0  speedup is 0.00

2、rsync备份操作

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

[root@localhost ~]# yum -y install httpd
[root@client ~]# mkdir /myweb
[root@client ~]# rsync -avzH --delete backuper@192.168.1.10::wwwroot /myweb/
Password: 
receiving incremental file list
./
index.html
index.php

sent 102 bytes  received 205 bytes  47.23 bytes/sec
total size is 0  speedup is 0.00

2、每天晚上22: 30对服务器的网站目录做一次同步

[root@client ~]# vim /etc/server.pass               //创建密码文件,保存backuper用户的密码
pwd123
[root@client ~]# chmod 600 /etc/server.pass
[root@client ~]# crontab -e
30 22 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass backuper@192.168.1.10::wwwroot /myweb
[root@client ~]# systemctl restart crond
[root@client ~]# systemctl enable crond

三、配置inotify+rsync架构

1、调整inotify内核参数

[root@client ~]# vim /etc/sysctl.conf
#监控事件队列
fs.inotify.max_queued_events = 16384
#实例数
fs.inotify.max_user_instances = 1024
#监控数
fs.inotify.max_user_watches = 1048576
[root@client ~]# sysctl -p

2、安装 inotify-tools

[root@localhost ~]# wget --no-check-certificate https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@localhost ~]# tar zxf inotify-tools-3.14.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# ./configure && make && make install

选项"-e"用来指定要监控哪些事件,选项"-m"表示持续监控,选项"-r"表示递归整个目录,选项"-q"简化输出信息

[root@localhost ~]# inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/               

3、编写触发式同步脚本

用来检测本机192.168.1.20/var/www/html目录的变动情况,一旦有更新触发rsync同步操作,上传备份至服务器192.168.1.10的/var/www/html目录下

[root@localhost ~]# vim /etc/server.pass
pwd123
[root@localhost ~]# chmod 600 /etc/server.pass 
[root@localhost ~]# vim /opt/inotify_rsync.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.1.10::wwwroot"
$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               //设置开机自启动

或者

#!/bin/bash
#需要同步的源路径
src=/var/www/html
#发布的模块名称
des=wwwroot
#密码文件
rsync_passwd_file=/etc/server.pass
#目标服务器1
ip1=192.168.1.10
#目标服务器2
ip2=192.168.1.30
#验证用户名
user=backuper
cd ${src}
inotifywait -mrq --format  '%Xe %w%f' -e modify,create,delete,attrib,close_write,move ./ | while read file    
do
        INO_EVENT=$(echo $file | awk '{print $1}')
        INO_FILE=$(echo $file | awk '{print $2}')
        echo "-------------------------------$(date)------------------------------------"
        echo $file
        if [[ $INO_EVENT =~ 'CREATE' ]] || [[ $INO_EVENT =~ 'MODIFY' ]] || [[ $INO_EVENT =~ 'CLOSE_WRITE' ]] || [[ $INO_EVENT =~ 'MOVED_TO' ]]
        then
                echo 'CREATE or MODIFY or CLOSE_WRITE or MOVED_TO'
                rsync -avzcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des} &&
                rsync -avzcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip2}::${des}
        fi
        if [[ $INO_EVENT =~ 'DELETE' ]] || [[ $INO_EVENT =~ 'MOVED_FROM' ]]
        then
                echo 'DELETE or MOVED_FROM'
                rsync -avzR --delete --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des} &&
                rsync -avzR --delete --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip2}::${des}
        fi
        if [[ $INO_EVENT =~ 'ATTRIB' ]]
        then
                echo 'ATTRIB'
                if [ ! -d "$INO_FILE" ]
                then
                        rsync -avzcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des} &&
                        rsync -avzcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip2}::${des}
                fi
		fi
done

四、配置rsync+sersync架构

[root@localhost ~]# wget https://files.cnblogs.com/files/hypj/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@localhost ~]# tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/
[root@localhost src]# mv GNU-Linux-x86/ /usr/src/sersync
[root@localhost src]# cd sersync/
[root@localhost sersync]# cp confxml.xml confxml.xml.bak
[root@localhost sersync]# vim confxml.xml
    <sersync>
        <localpath watch="/var/www/html">
            <remote ip="192.168.1.10" name="wwwroot"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="backuper" passwordfile="/etc/server.pass"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
[root@localhost sersync]# ./sersync2 -d -r -o ./confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d 	run as a daemon
option: -r 	rsync all the local files to the remote servers before the sersync work
option: -o 	config xml name:  ./confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost	host port: 8008
daemon start,sersync run behind the console 
use rsync password-file :
user is	backuper
passwordfile is 	/etc/server.pass
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /var/www/html && rsync -artuz -R --delete ./ backuper@192.168.1.10::wwwroot --password-file=/etc/server.pass >/dev/null 2>&1 
run the sersync: 
watch path is: /var/www/html
[root@localhost ~]# echo '/usr/src/sersync/sersync2 -d -r -o  /usr/src/sersync/confxml.xml' >> /etc/rc.local               //设置开机自启动
[root@localhost ~]# killall sersync2 && sersync2 -dro /usr/src/sersync/confxml.xml               //重启

rsync 常见错误与解决方法整理

posted @ 2020-03-03 21:31  高中僧  阅读(79)  评论(0)    收藏  举报