14 .Magento备份 rsyncd +inotify 即时备份

来源:互联网 发布:上海日式拉面 知乎 编辑:程序博客网 时间:2024/05/16 18:09

rsyncd 是linux下面一个不错的备份工具:

远程服务器端安装:(网站服务器,需要备份的服务器)

yum install rsync -y

会有类似的信息出现:
---> Package rsync.x86_64 0:3.0.6-5.el6_0.1 will be updated
---> Package rsync.x86_64 0:3.0.6-9.el6 will be an update
安装完以后可以查看一下rsync安装的相关目录文件:
rpm -ql rsync
/etc/xinetd.d/rsync
/usr/bin/rsync
/usr/share/doc/rsync-3.0.6
/usr/share/man/man1/rsync.1.gz
/usr/share/man/man5/rsyncd.conf.5.gz
为了便于启停rsync,我们可以安装xinetd:
yum install xinetd
---> Package xinetd.x86_64 2:2.3.14-35.el6_3 will be installed
安装完后要修改以下配置文件:默认disable = yes,这里需要把yes改成no,保存退出。
vim /etc/xinetd.d/rsync
service rsync{        disable = no        flags           = IPv6        socket_type     = stream        wait            = no        user            = root        server          = /usr/bin/rsync        server_args     = --daemon --config=/etc/rsyncd/rsyncd.conf        log_on_failure  += USERID}
创建连接文件:

mkdir /etc/rsyncd

ln -s /etc/rsyncd.conf  /etc/rsyncd/rsyncd.conf

然后是配置rsync的主配置文件及密码文件:

touch /var/log/rsync.log
vim /etc/rsyncd.conf
注意:path是您要备份的文件,也就是服务器端的这个文件夹下面的所有文件,备份到远程客户端
pid file = /var/run/rsyncd.pidport = 873uid = rootgid = rootuse chroot = yesread only = yesmax connections = 5log file = /var/log/rsync.log  log format = %t %a %m %f %bsyslog facility = local3timeout = 300   [test]   path = /opt/mongo   list=yes   ignore errors   auth users = root, jeff   secrets file = /etc/rsyncd/rsyncd.pascomment = myself test home   exclude = tmp/
test是链接时候的标签,[test] 下面的path 为远程备份的路径

以上主配置文件有个地方我觉得要特别注意,就是在每个模块(如以上的[test])后指定path路径时,路径后绝对不能留空格,否则全一直报下方的错误一。下面是密码文件:

这个密码文件的作用是,客户端连接服务器的时候,需要验证下面的密码:

vim /etc/rsyncd/rsyncd.pas
root:1q2w3e4rnhy67ujm
上面的代码为/etc/rsyncd/rsyncd.pas文件里面的内容,root为服务端的用户名,冒号后面的是密码。

chmod 600 /etc/rsyncd/rsyncd.pas

将rsync添加为系统服务并在系统3和5级别下启动:

chkconfig --add rsyncchkconfig  rsync on/etc/init.d/xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]
启动好后可以用以下命令查看服务是否开启
netstat -tpln

tcp        0      0 :::873     :::*          LISTEN    17752/xinetd
由上可以看出873端口已经开启了,说明rsync服务已经启动。到此,服务器端的配置已完成。


2:客户端

客户端很简单,把rsync装上就行了,还是用以上的命令:

yum install rsync -y
chkconfig --add rsync  chkconfig  rsync on 


然后安装密码文件:

vi /etc/sery_client.pass

在里面填写的内容要和在服务器段:/etc/sery_client.pass这个文件里面的密码一致,服务器端的密码文件里面的内容为:root:1q2w3e4rnhy67ujm

这里填写:1q2w3e4rnhy67ujm

完成后,我们配置无密码ssh登录:

在服务器端:

mkdir /root/.ssh/

在客户端:

mkdir /root/.ssh/cd /root/.ssh/ssh-keygen -d

一直按回车即可:

Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase): <-- 此处不打passphrase..下次才不会询问password
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
11:22:33:44:55:66:77:88:99:00:11:22:33:44:55:66 root@mondeo.adj.idv.tw

这时会在系统下看到两个档案...id_dsa与id_dsa.pub ,现在要把id_dsa.pub丢到121.199.39.117 并且更名为 authorized_keys

scp id_dsa.pub 192.168.0.1:/root/.ssh/authorized_keys

root@192.168.0.1's password:
id_dsa.pub 100% |*****************************************************| 612 00:00
[root@mondeo .ssh]#

现在您可以执行ssh 121.199.39.117 看看能否登入而不需要输入密码...

如果成功,可以使用下面的命令:

rsync -avzP --delete  root@98.126.196.10::test /backup/onfancy.com --password-file=/etc/sery_client.pass

可能会报错

password file must not be other-accessible 
continuing without password file 
Password: 
说明:这是因为rsyncd.pwd rsyncd.sec的权限不对,应该设置为600。

chmod 600 /etc/sery_client.pass



看看是否备份了、







备注:


下面是该怎么用了,rsync的参数选项比较多,下面就稍微例举一下常见的用法:
rsync -参数 用户名@同步服务器的IP::rsyncd.conf中那个方括号里的内容 本地存放路径 如:   
rsync -avzP root@121.199.39.117::test /backup
   
说明:   
-a 参数,相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;   
-z 传输时压缩;   
-P 传输进度;   
-v 传输时的进度等信息,和-P有点关系.
更多的选项大家可以去查看帮助。

如果想要备份服务器端数据而不需要输入密码,那么需要:

在服务器端:

mkdir /root/.ssh/

在客户端:

mkdir /root/.ssh/cd /root/.ssh/ssh-keygen -d

一直按回车即可:

Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase): <-- 此处不打passphrase..下次才不会询问password
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
11:22:33:44:55:66:77:88:99:00:11:22:33:44:55:66 root@mondeo.adj.idv.tw

这时会在系统下看到两个档案...id_dsa与id_dsa.pub ,现在要把id_dsa.pub丢到121.199.39.117 并且更名为 authorized_keys

scp id_dsa.pub 192.168.0.1:/root/.ssh/authorized_keys
上面的原理为:把客户端的生成的key文件放到服务器端的/root/.ssh文件夹下面,那么,客户端就可以直接ssh ip直接登录服务器端了
root@192.168.0.1's password:
id_dsa.pub 100% |*****************************************************| 612 00:00
[root@mondeo .ssh]#

现在您可以执行ssh 121.199.39.117 看看能否登入而不需要输入密码...

如果成功,可以使用下面的命令,把服务器端 /121目录,备份到客户端/ba下面

rsync -avlR --delete  -e  ssh  121.199.39.117:/121 /ba

上面代表的意思是把121.199.39.117下面的/121目录,备份到客户端的/ba下面

另外可以新建文件/ba/sync.sh

touch  /ba/sync.shvim  /ba/sync.shrsync -avlR --delete  -e  ssh  121.199.39.117:/121 /bash /ba/sync.sh

就会看到同步信息。

如果想用cron,那么:

crontab -e* * * * * /bin/bash /ba/sync.sh

其他辅助参考详细参看如下:


参数意义如下﹕

-a, --archive
It is a quick way of saying you want recursion and want to preserve almost everything.
-v, --verbose
This option increases the amount of information you are given during the transfer.
-l, --links
When symlinks are encountered, recreate the symlink on the destination.
-R, --relative
Use relative paths. 保留相对路径...才不会让子目录跟 parent 挤在同一层...
--delete
是指如果Server端删除了一文件,那客户端也相应把这一文件删除,保持真正的一致。
-e ssh
建立起加密的连接。






在使用rsync的时候会遇到一些问题,下面列举了一些常见的,希望到大家有所帮助:
错误一:
@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]
解决:
服务器端的目录不存在或无权限,创建目录并修正权限可解决问题。

错误二:
@ERROR: auth failed on module XXX
rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]
解决:
服务器端该模块(XXX)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。
提供正确的用户名密码解决此问题。可能是密码文件权限不对,必须确保rsyncd.pas的访问权限为600

错误三:
@ERROR: Unknown module ‘XXX’
rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]
解决:
服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题。

错误四:
@ERROR: failed to open lock file
rsync error: error starting client-server protocol (code 5) at main.c(1495) [receiver=3.0.6]
解决:配置文件 rsync.conf 中添加 lock file = rsyncd.lock 即可解决。

错误五:
rsync: failed to connect to XXX: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=3.0.6]
解决:对方没开机、防火墙阻挡、通过的网络上有防火墙阻挡,都有可能。关闭防火墙,其实就是把tcp udp 的873端口打开。



inotify部分:

检测是否可以安装inotify

uname -r 

如果是2.6.13以上,那么就支持inotify

安装inotify

wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz  tar xvf inotify-tools-3.14.tar.gz  cd inotify-tools-3.14  ./configure  make && make install 
vi /root/rsync.sh 

把本机(服务器)的/ee  备份到121.199.39.117目录 /333下面

设置inotify监控的文件最大数量

echo 999990000 > /proc/sys/fs/inotify/max_user_watches

由于上面使用可文件验证,所以,这里ssh不需要密码。

#!/bin/bash     src=/var/www/html/tomtop.com/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f'  -e modify,delete,create,attrib $src  | while read  file            do                rsync -vzrtopg --delete --bwlimit=1000 --progress $src  -e  ssh  121.199.39.117:/333 &&                echo "$file  was rsynced" >> /tmp/rsync.log 2>&1                echo "---------------------------------------------------------------------------"            done

当本地有文件有改动就会发送到121.199.39.117

如果src=/eee/,注意,后面带有反斜杠,备份的是/eee下面的文件,而不备份/eee文件夹

在对一个大磁盘进行inotify监听时,爆出如下错误:
Failed to watch /mnt/;
upper limit on inotify watches reached!
Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches’.

cat一下这个文件,默认值是8192,

echo 8192000 > /proc/sys/fs/inotify/max_user_watches

即可~






0 0
原创粉丝点击