rsync 常用的几种备份案例

作者简介:
一只在互联网IDC运维的网络攻城狮,网络技术问题可联系QQ:1656209309

rsync手工备份配置案例:
服务器端:192.168.1.104
客户端:192.168.1.50
1)环境准备说明:
192.168.1.104 rsync server(rsync服务端)
192.168.1.50 (客户端)
2)配置前检查
cat /etc/redhat-release
CentOS release 6.6 (Final)
uname -r
2.6.32-504.el6.x86_64
uname -m
x86_64
3)配置rsync服务端
#yum安装:
yum install -y rsync

rpm -qa rsync
rsync-3.0.6-12.el6.x86_64

#建立配置文件:
vim /etc/rsyncd.conf

uid = rsync
gid = rsync
use chroot = no
max connections = 200
hosts allow = *
timeout = 600
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
ignore errors
read only = false
list = false
host deny = 0.0.0.0/32
auth users = rsyncback
secrets file = /etc/rsync.password
transfer logging = yes
#Module definitions

[data]
comment = data by pjy
path = /data/

[root@rsync ~]# ll /etc/rsync*
-rw-r--r-- 1 root root 378 11月 3 23:12 /etc/rsyncd.conf
-rw------- 1 root root 18 10月 30 23:04 /etc/rsync.password
[root@rsync ~]# cat /etc/rsync.password
rsyncback:123.com

#创建模块监控目录
mkdir -p /data

#创建rsync用户,不创建家目录并且禁止该账户登录
[root@rsync ~]# useradd -s /sbin/nologin -M rsync

将备份服务器目录权限指定给rsync用户
[root@rsync ~]# chown -R rsync.rsync /data/
[root@rsync ~]# ls -ld /data/
drwxr-xr-x 2 rsync rsync 4096 10月 30 23:00 /data/

#建立密码文件,并修改权限
echo "rsyncback:123.com" >/etc/rsync.password
chmod 600 /etc/rsync.password

#启动rsync服务:
rsync --damon
ps -ef|grep rsync|grep -v grep
root 7858 1 0 20:09 ? 00:00:00 rsync --daemon

#确认下rsync服务器侧服务是否启动
netstat -lntp|grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0: LISTEN 7858/rsync
tcp 0 0 :::873 :::
LISTEN 7858/rsync
lsof -i :873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 7858 root 3u IPv4 224418 0t0 TCP :rsync (LISTEN)
rsync 7858 root 5u IPv6 224419 0t0 TCP
:rsync (LISTEN

4)配置客户端
#在客户端建立密码文件,并测试同步是否正常
echo "123.com" >/etc/rsync.password
chmod 600 /etc/rsync.password
[root@client data]# ll /etc/rsync.password
-rw------- 1 root root 8 11月 3 23:16 /etc/rsync.password
[root@client data]# cat /etc/rsync.password
123.com

[root@client data]# useradd -u 513 -M -s /sbin/nologin rsync

#客户端创建data目录,并创建测试文件
[root@client bak]# mkdir -p /data
[root@client bak]# cd /data/
[root@client data]# touch stu{00..10}.txt
[root@client data]# ll
总用量 0
-rw-r--r-- 1 root root 0 11月 3 23:21 stu00.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu01.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu02.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu03.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu04.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu05.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu06.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu07.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu08.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu09.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu10.txt

#客户端将自己需要备份的文件推送到备份服务器上
[root@client data]# rsync -avz /data/ [email protected]::data --password-file=/etc/rsync.password
sending incremental file list
./
stu00.txt
stu01.txt
stu02.txt
stu03.txt
stu04.txt
stu05.txt
stu06.txt
stu07.txt
stu08.txt
stu09.txt
stu10.txt


增量备份方法:(前提是rsync服务已搭建完成,手工备份测试成功)

#客户端新增test.txt文件,通过--delete参数测试增量同步效果:

[root@client data]# touch test.txt
[root@client data]# ll
总用量 0
-rw-r--r-- 1 root root 0 11月 3 23:21 stu00.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu01.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu02.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu03.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu04.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu05.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu06.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu07.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu08.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu09.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu10.txt
-rw-r--r-- 1 root root 0 11月 3 23:25 test.txt
[root@client data]# rsync -avz --delete /data/ [email protected]::data --password-file=/etc/rsync.password
sending incremental file list
./
test.txt

sent 223 bytes received 30 bytes 168.67 bytes/sec
total size is 0 speedup is 0.00

#在备份服务器rsync侧查看增量同步情况
[root@rsync ~]# ll /data/
总用量 0
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu00.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu01.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu02.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu03.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu04.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu05.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu06.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu07.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu08.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu09.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu10.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:25 test.txt

#在备份服务器rsync上新增test1.txt文件, 测试同步情况
[root@rsync ~]# touch /data/test1.txt

#在客户端上通过命令测试,发现服务器侧文件被test1.txt被删除
[root@client data]# rsync -avz --delete /data/ [email protected]::data --password-file=/etc/rsync.password
sending incremental file list
./
deleting test1.txt

sent 187 bytes received 11 bytes 396.00 bytes/sec
total size is 0 speedup is 0.00
[root@client data]# ll /data/
总用量 0
-rw-r--r-- 1 root root 0 11月 3 23:21 stu00.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu01.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu02.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu03.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu04.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu05.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu06.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu07.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu08.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu09.txt
-rw-r--r-- 1 root root 0 11月 3 23:21 stu10.txt
-rw-r--r-- 1 root root 0 11月 3 23:25 test.txt

#备份服务器rsync上test1.txt被删除

[root@rsync ~]# ll /data/
总用量 0
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu00.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu01.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu02.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu03.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu04.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu05.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu06.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu07.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu08.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu09.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:21 stu10.txt
-rw-r--r-- 1 rsync rsync 0 11月 3 23:25 test.txt

注意:增量同步--delete参数使用时,如果客户端本地数据推向备份服务器,客户端新增数据时,采用delete参数,可以实现将客户端本地数据备份到服务器,如果备份服务器侧新增文件,则在客户端采用推的方式将本地数据推向服务器时,服务器侧之前新增的文件会被删除掉。


inotify+rsync实现实时同步
配置思路:
1、服务器端配置rsync服务,客户端测试能否实现增量同步功能(省略)
2、客户端安装inotify-tools工具(需要epel源)

1)查看客户端是否支持inotify功能(Linux内核2.6.13后支持)
[root@client data]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 11月 4 00:18 max_queued_events
-rw-r--r-- 1 root root 0 11月 4 00:18 max_user_instances
-rw-r--r-- 1 root root 0 11月 4 00:18 max_user_watches
2)安装epel源及inotify-tools
epel阿里源
[root@client data]# wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo
[root@client data]# yum install -y inotify-tools
3、编写脚本并测试,保证客户端创建的文件实时同步到服务器端
客户端脚本:
[root@client data]# less /server/script/rsync.sh
#!/bin/bash
src=/data/
des_ip=192.168.1.104
user=rsyncback
/usr/bin/inotifywait -mrq --timefmt %y/%m'%d %H:/%M' --format '%T %w%f' -e modify,delete,create,attrib $src \
| while read file
do
/usr/bin/rsync -avzP --delete $src $user@$des_ip::data --password-file=/etc/rsync.password
echo "${file} was rsynced" >> /var/log/rsync.log 2>&1
done

测试:
#客户端创建50个文件
[root@client data]# touch stu{50..100}

#服务器查看文件是否同步
[root@rsync data]# ll stu
stu00.txt stu10.txt stu50 stu61 stu72 stu83 stu94
stu01.txt stu11 stu51 stu62 stu73 stu84 stu95
stu02.txt stu12 stu52 stu63 stu74 stu85 stu96
stu03.txt stu13 stu53 stu64 stu75 stu86 stu97
stu04.txt stu14 stu54 stu65 stu76 stu87 stu98
stu05.txt stu15 stu55 stu66 stu77 stu88 stu99
stu06.txt stu16 stu56 stu67 stu78 stu89
stu07.txt stu17 stu57 stu68 stu79 stu90
stu08.txt stu18 stu58 stu69 stu80 stu91
stu09.txt stu19 stu59 stu70 stu81 stu92
stu100 stu20 stu60 stu71 stu82 stu93

4、将脚本放置后台运行,并加入到/etc/rc.local中,保证开机可自启动
[root@client script]# ./rsync.sh &
[root@client script]# ps aux | grep inotify
root 26294 0.0 0.0 6284 752 pts/0 S 03:04 0:00 /usr/bin/inotifywait -mrq --timefmt %y/%m%d %H:/%M --format %T %w%f -e modify,delete,create,attrib /data/
root 26297 0.0 0.0 103300 884 pts/0 S+ 03:04 0:00 grep inotify
[root@client data]# echo "/server/script/rsync.sh" >> /etc/rc.local


sersync+rsync实时备份
环境:
客户端:192.168.1.50
备份服务器:192.168.1.104

配置思路:
1、配置备份服务器rysnc,并测试客户端手工备份
备份服务器端配置:
[root@rsync data]# cat /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 200
hosts allow = *
timeout = 600
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
ignore errors
read only = false
list = false
host deny = 0.0.0.0/32
auth users = rsyncback
secrets file = /etc/rsync.password
transfer logging = yes
#Module definitions

[data]
path = /data/

[root@rsync data]# cat /etc/rsync.password
rsyncback:123.com

[root@rsync data]# ls -ld /data/
drwxr-xr-x 2 rsync rsync 4096 11月 4 2019 /data/

客户端配置:
[root@client bin]# cat /etc/rsync.password
123.com

2、配置客户端sersync
1)下载sersync,见附件
2)配置sersync
[root@client ~]# tree /sersync/
/sersync/
|-- GNU-Linux-x86
|-- bin
| -- sersync<br/>|-- conf<br/>|-- confxml.xml
-- log<br/>-- rsync_fail_log.sh

[root@client ~]# less /sersync/conf/confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.).svn"></exclude>
<exclude expression="(.
).gz"></exclude>
<exclude expression="^info/"></exclude>
<exclude expression="^static/
"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>

<sersync>
    <localpath watch="/data">
        <remote ip="192.168.1.104" name="data"/>
        <!--<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="rsyncback" passwordfile="/etc/rsync.password"/>
        <userDefinedPort start="false" port="874"/><!-- port=874 -->
        <timeout start="false" time="100"/><!-- timeout=100 -->
        <ssh start="false"/>
    </rsync>
    <failLog path="/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
    <crontab start="false" schedule="600"><!--600mins-->
        <crontabfilter start="false">
            <exclude expression="*.php"></exclude>
            <exclude expression="info/*"></exclude>
        </crontabfilter>
    </crontab>
    <plugin start="false" name="command"/>
</sersync>

<plugin name="command">
    <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /opt/tongbu/mmm.sh suffix-->
    <filter start="false">
        <include expression="(.*)\.php"/>
        <include expression="(.*)\.sh"/>
    </filter>
</plugin>

<plugin name="socket">
    <localpath watch="/opt/tongbu">
        <deshost ip="192.168.138.20" port="8009"/>
    </localpath>
</plugin>
<plugin name="refreshCDN">
    <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
        <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
        <sendurl base="http://pic.xoyo.com/cms"/>
        <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
    </localpath>
</plugin>

</head>
[root@client ~]#

#启动sersync服务:
[root@client bin]# ll
total 1768
-rwxr-xr-x 1 root root 1810128 Oct 26 2011 sersync
[root@client bin]# ./sersync -r -d -o /sersync/conf/confxml.xml
#确认服务运行状态
[root@client bin]# ps aux | grep sersync
ps aux | grep sersync
root 27428 0.0 0.6 211072 7028 ? Ssl 04:56 0:00 ./sersync -r -d -o /sersync/conf/confxml.xml
root 27974 0.0 0.0 103264 788 pts/2 S+ 05:27 0:00 grep sersync

3、测试:客户端手工创建文件,查看备份服务器端备份效果
[root@client ~]#touch /data/stu{00..10}
[root@client bin]# ll /data/
total 0
-rw-r--r-- 1 root root 0 Nov 4 04:58 stu00
-rw-r--r-- 1 root root 0 Nov 4 04:58 stu01
-rw-r--r-- 1 root root 0 Nov 4 04:58 stu02
-rw-r--r-- 1 root root 0 Nov 4 04:58 stu03
-rw-r--r-- 1 root root 0 Nov 4 04:58 stu04
-rw-r--r-- 1 root root 0 Nov 4 04:58 stu05
-rw-r--r-- 1 root root 0 Nov 4 04:58 stu06
-rw-r--r-- 1 root root 0 Nov 4 04:58 stu07
-rw-r--r-- 1 root root 0 Nov 4 04:58 stu08
-rw-r--r-- 1 root root 0 Nov 4 04:58 stu09
-rw-r--r-- 1 root root 0 Nov 4 04:58 stu10

备份服务器端:
[root@rsync yum.repos.d]# ll /data/
总用量 0
-rw-r--r-- 1 rsync rsync 0 11月 4 2019 stu00
-rw-r--r-- 1 rsync rsync 0 11月 4 2019 stu01
-rw-r--r-- 1 rsync rsync 0 11月 4 2019 stu02
-rw-r--r-- 1 rsync rsync 0 11月 4 2019 stu03
-rw-r--r-- 1 rsync rsync 0 11月 4 2019 stu04
-rw-r--r-- 1 rsync rsync 0 11月 4 2019 stu05
-rw-r--r-- 1 rsync rsync 0 11月 4 2019 stu06
-rw-r--r-- 1 rsync rsync 0 11月 4 2019 stu07
-rw-r--r-- 1 rsync rsync 0 11月 4 2019 stu08
-rw-r--r-- 1 rsync rsync 0 11月 4 2019 stu09
-rw-r--r-- 1 rsync rsync 0 11月 4 2019 stu10

sent 551 bytes received 220 bytes 1542.00 bytes/sec
total size is 0 speedup is 0.00

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章