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

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