rsync实时备份

实验环境redhat或者centos

Ip192.168.18.67

Ip192.168.18.167

了解rsync

[root@localhost~]#mkdir-pv/tmp/a

mkdir:已创建目录/tmp/a

[root@localhost~]#mkdir-pv/tmp/b

mkdir:已创建目录/tmp/b

[root@localhost~]#cd/tmp/

[root@localhosttmp]#cda

[root@localhosta]#touch123

[root@localhosta]#rsync/tmp/a/tmp/b

skippingdirectory/tmp/a

[root@localhosta]#rsync/tmp/a/tmp/b-r

[root@localhosta]#cd/tmp/b/

[root@localhostb]#ls

a

[root@localhostb]#cda

[root@localhosta]#ls

123

实验演示过程

mkdir-pv/etc/rsyncd这个目录起初是没有的,需要手动建立

ls-l

总计0

vim/etc/rsyncd/rsyncd.conf

uid=root

gid=root

port=873

maxconnections=0#limitclientconection

logfile=/var/log/rsyncd.log

pidfile=/var/run/rsyncd.pid

lockfile=/var/run/rsyncd.lock

motdfile=/etc/rsyncd/rsyncd.motd

readonly=yes

####limituserconn######

hostsallow=192.168.18.0/255.255.255.0

hostsdeny=*

#transferlogging=yes

#logformat=%t%a%m%f%b

#syslogfacility=local3

#timeout=300

[www]

path=/var/www/html/

list=yes

ignoreerrors

authusers=zhang

###username

secretsfile=/etc/rsyncd/rsyncd.secrets

comment=wwwdirectory

exclude=a/b/

~

[root@localhostrsyncd]#mkdir-pv/var/www/html/{a,b,c}

mkdir:已创建目录/var/www/html/a

mkdir:已创建目录/var/www/html/b

mkdir:已创建目录/var/www/html/c

/etc/rsyncd

vimrsyncd.motd创建广告文件

################################

zhangzhanlan

################################

vimrsyncd.secrets

zhang:123

chmod600rsyncd.secrets

rsync--daemon--config=/etc/rsyncd/rsyncd.conf启动服务

lsof-i:873

COMMANDPIDUSERFDTYPEDEVICESIZENODENAME

rsync5766root4uIPv620761TCP*:rsync(LISTEN)

rsync5766root5uIPv420762TCP*:rsync(LISTEN)

tail-f/var/log/rsyncd.log

2013/10/0922:41:50[5766]rsyncdversion2.6.8starting,listeningonport873

客户端

echo123>/etc/rsync.password

chmod600/etc/rsync.password

[root@localhost~]#mkdir-pv/tmp/bak

mkdir:已创建目录/tmp/bak

rsync-avzP--delete--password-file=/etc/[email protected]::www/tmp/bak

实验效果测试

67touch/var/www/html/{1,2,3}创建文件及数据文件

167上执行上述的命令

rsync-avzP--delete--password-file=/etc/[email protected]::www/tmp/bak

################################

zhangzhanlan

################################

receivingfilelist...

5filestoconsider

./

1

0100%0.00kB/s0:00:00(xfer#1,to-check=3/5)

2

0100%0.00kB/s0:00:00(xfer#2,to-check=2/5)

3

0100%0.00kB/s0:00:00(xfer#3,to-check=1/5)

sent169bytesreceived360bytes1058.00bytes/sec

totalsizeis0speedupis0.00

Youhavemailin/var/spool/mail/root

进入到指定的目录里查看就可以查看到备份的数据及文件

要想让167自动检测到67上的数据集文件可以自动备份,就需要将67167的服务器和客户端的角色互换,

首先将

167上的/tmp/bak目录下的数据及文件清空

rm-fr./*

在查看67上的rsync的进程是否还存在,如果存在先将进程结束

[root@localhosthtml]#lsof-i:873

COMMANDPIDUSERFDTYPEDEVICESIZENODENAME

rsync5766root4uIPv620761TCP*:rsync(LISTEN)

rsync5766root5uIPv420762TCP*:rsync(LISTEN)

[root@localhosthtml]#killallrsync

167上创建rsync目录

mkdir-pv/etc/rsync

mkdir:已创建目录/etc/rsyncd

[root@localhost~]#mkdir-pv/etc/rsyncd

mkdir:已创建目录/etc/rsyncd

[root@localhost~]#cd/etc/rsyncd

[root@localhostrsync]#pwd

/etc/rsync

vim/etc/rsyncd/rsyncd.secrets

zhang:123

chmod600/etc/rsyncd/rsyncd.secrets

vim/etc/rsyncd/rsyncd.conf编辑配置文件

uid=root

gid=root

port=873

maxconnections=0#limitclientconection

usechroot=no

logfile=/var/log/rsyncd.log

pidfile=/var/run/rsyncd.pid

lockfile=/var/run/rsyncd.lock

motdfile=/etc/rsyncd/rsyncd.motd

readonly=no###############!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

####limituserconn######

hostsallow=192.168.18.0/255.255.255.0

hostsdeny=*

#transferlogging=yes

#logformat=%t%a%m%f%b

#syslogfacility=local3

#timeout=300

[www]

path=/tmp/bak

list=yes

ignoreerrors

authusers=zhang

###username!!

secretsfile=/etc/rsyncd/rsyncd.secrets

comment=wwwdirectory

readonly=no

开启服务

[root@localhost~]#rsync--daemon--config=/etc/rsyncd/rsyncd.conf

[root@localhost~]#lsof-i:873

COMMANDPIDUSERFDTYPEDEVICESIZENODENAME

rsync14364root3uIPv647367TCP*:rsync(LISTEN)

rsync14364root5uIPv447368TCP*:rsync(LISTEN)

67上安装这个工具。主要是监听内核的,会出发rsyncclient端。

[root@localhosttmp]#tarfvxzsersync.tar.gz

GNU-Linux-x86/

GNU-Linux-x86/sersync2

GNU-Linux-x86/confxml.xml

[root@localhosttmp]#cdGNU-Linux-x86/

[root@localhostGNU-Linux-x86]#ls

confxml.xmlsersync2

[root@localhostGNU-Linux-x86]#pwd

/tmp/GNU-Linux-x86

vimconfxml.xml修改监听条件

主要修改的地方是

<sersync>

<localpathwatch="/var/www/html">#本地的需要备份文件的路径

<remoteip="192.168.18.167"name="www"/>#备份机的ip及共享名

<!--<remoteip="192.168.8.39"name="tongbu"/>-->

<!--<remoteip="192.168.8.40"name="tongbu"/>-->

</localpath>

<rsync>

<commonParamsparams="-artuz"/>

<authstart="true"users="zhang"passwordfile="/etc/rsyncd/rsyncd.secrets"/>

#密码文件

<userDefinedPortstart="false"port="874"/><!--port=874-->

<timeoutstart="false"time="100"/><!--timeout=100-->

<sshstart="false"/>

</rsync>

<failLogpath="/tmp/rsync_log"timeToExecute="60"/><!--defaultevery60minsexecuteonce-->#错误日志存放路径(自定义)

在整个配置中的true,全部是监听,false是不监听。以上的这些信息都是可以自己修改的

67上开启监听

[root@localhostGNU-Linux-x86]#./sersync2-r

setthesystemparam

executeecho50000000>/proc/sys/fs/inotify/max_user_watches

executeecho327679>/proc/sys/fs/inotify/max_queued_events

parsethecommandparam

option:-rrsyncallthelocalfilestotheremoteserversbeforethesersyncwork

daemonthreadnum:10

parsexmlconfigfile

hostip:localhosthostport:8008

usersyncpassword-file:

useriszhang

passwordfileis/etc/rsyncd/rsyncd.secrets

configxmlparsesuccess

pleaseset/etc/rsyncd.confmaxconnections=0Manually

sersyncworkingthread12=1(primarythread)+1(failretrythread)+10(daemonsubthreads)

Maxthreadsnumbersis:22=12(Threadpoolnums)+10(Subthreads)

pleaseaccordingyourcpuuse-nparamtoadjustthecpurate

------------------------------------------

rsyncthedirectoryrecursivlytotheremoteserversonce

workingpleasewait...

executecommand:cd/var/www/html&&rsync-artuz-R--delete./[email protected]::www--password-file=/etc/rsyncd/rsyncd.secrets>/dev/null2>&1

runthesersync:

watchpathis:/var/www/html

这样就可以成功了

167上查看

[root@localhostbak]#ls

123abc

错误日志路径

tail-f/tmp/rsync_log

如果不成功,说明是在67上的/etc/rsyncd/rsyncd.secrets中多写用户名,将用户名删掉就可以

如:zhang123写成123保存即可

当目录层级比较多的时候这个备份不是很好用

你可能感兴趣的:(touch)