转自:http://www.mike.org.cn/blog/index.php?load=read&id=639###pp=0
[rsync实现网站的备份,文件的同步,不同系统的文件的同步,如果是windows的话,需要windows版本cwrsync]
一、什么是rsync
rsync,remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。 rsync是用 “rsync 算法”提供了一个客户机和远程文件服务器的文件同步的快速方法,而且可以通过ssh方式来传输文件,这样其保密性也非常好,另外它还是免费的软件。
rsync 包括如下的一些特性:
能更新整个目录和树和文件系统;
有选择性的保持符号链链、硬链接、文件属于、权限、设备以及时间等;
对于安装来说,无任何特殊权限要求;
对于多个文件来说,内部流水线减少文件等待的延时;
能用rsh、ssh 或直接端口做为传输入端口;
支持匿名rsync 同步文件,是理想的镜像工具;
二、架设rsync服务器
架设rsync 服务器比较简单,写一个配置文件rsyncd.conf 。文件的书写也是有规则的,我们可以参照rsync.samba.org 上的文档来做。当然我们首先要安装好rsync这个软件才行;
A、rsync的安装;
获取rsync
rysnc的官方网站:http://rsync.samba.org/可以从上面得到最新的版本。目前最新版是3.05。当然,因为rsync是一款如此有用的软件,所以很多Linux的发行版本都将它收录在内了。
软件包安装
# sudo apt-get install rsync 注:在debian、ubuntu 等在线安装方法;
# yum install rsync 注:Fedora、Redhat 等在线安装方法;
# rpm -ivh rsync 注:Fedora、Redhat 等rpm包安装方法;
其它Linux发行版,请用相应的软件包管理方法来安装。
源码包安装
tar xvf rsync-xxx.tar.gzcd rsync-xxx
./configure --prefix=/usr ;make ;make install 注:在用源码包编译安装之前,您得安装gcc等编译开具才行;
B、配置文件
rsync的主要有以下三个配置文件rsyncd.conf(主配置文件)、rsyncd.secrets(密码文件)、rsyncd.motd(rysnc服务器信息)
服务器配置文件(/etc/rsyncd.conf),该文件默认不存在,请创建它。
具体步骤如下:
#touch /etc/rsyncd.conf #创建rsyncd.conf,这是rsync服务器的配置文件。
#touch /etc/rsyncd.secrets #创建rsyncd.secrets ,这是用户密码文件。
#touch /etc/rsyncd.motd
#chown root.root rsyncd.secrets #修改属主
#chmod 600 rsyncd.secrets #修改权限
下一就是我们修改rsyncd.conf和rsyncd.secrets和rsyncd.motd文件的时候了。
这个配置的主机是需要备份的主机,也就是把这个主机的文件备份到别的主机上,我们配置这个需要备份的主机:
rsyncd.conf
# Distributed under the terms of the GNU General Public License v2
# Minimal configuration file for rsync daemon
# See rsync(1) and rsyncd.conf(5) man pages for help
# This line is required by the /etc/init.d/rsyncd script
pid file = /var/run/rsyncd.pid
port = 873
address = 192.168.1.214 #本机ip 需要备份的主机ip
#uid = nobody
#gid = nobody
uid = root
gid = root
use chroot = yes
read only = yes
#limit access to private LANs
hosts allow=192.168.1.217/255.255.255.0 10.0.1.1/255.255.255.0 #远程备份主机
hosts deny=*
max connections = 5
motd file = /etc/rsyncd.motd
#This will give you a separate log file
#log file = /var/log/rsync.log
#This will log every file transferred - up to 85,000+ per user, per sync
#transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
[rhel4home]
path = /www/web #备份文件
list=yes
ignore errors
auth users = root #账户
secrets file = /etc/rsyncd.secrets
comment = This is RHEL 4 data
exclude = b/ magento/
rsyncd.secrets
root:admin123
rsyncd.motd
++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use the Terry rsync services!
2002------2009
++++++++++++++++++++++++++++++++++++++++++++++
配置完成后,关闭防火墙:
service iptables stop
启动rsync:
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
对于配置文件中的选项参看:http://www.cnblogs.com/feiling/archive/2012/04/02/2430273.html
重启rsync。
cat /var/run/rsyncd.pid | xargs kill -9
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
如果报错:failed to create pid file /var/run/rsyncd.pid: File exists
rm -rf /var/run/rsyncd.pid
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
远程备份主机:
rsync -avzP root@192.168.1.214::rhel4home rhel4home
root@192.168.1.214是远程ip和用户名,rhel4home是配置文件/etc/rsyncd.conf下面的[rhel4home]部分。
后面的[rhel4home]代表保存到本地的文件夹名字。
查看连接是否成功:
rsync --list-only root@192.168.1.214::rhel4home
查看这个文件夹下面的其他文件。
如果想关掉密码验证,那么把文件 rsyncd.conf 下的
auth users = root
这行去掉,然后执行 rsync -avzP root@192.168.1.214::rhel4home rhel4home 就不需要密码验证了就可以直接使用rsyncd.secrets里面的密码了
自动备份:
[root@backup etc]# cat /root/script/rsync.sh
#!/bin/sh
/usr/bin/rsync -auv --progress --delete root@192.168.1.216::rhel4home /www/rhel4home
[root@backup etc]# chmod 755 /root/script/rsync.sh
vim crontab
添加:
0 1 * * * root sh /root/script/rsync.sh