CentOS5 + rsync 同步2台服务器的文件
Always use rsync over ssh
Since rsync does not provide any security while transferring data it is recommended that you use rsync over ssh . This allows a secure remote connection. Now let us see some examples of rsync.
rsync command common options
- --delete : delete files that don't exist on sender (system)
- -v : Verbose (try -vv for more detailed information)
- -e "ssh options" : specify the ssh as remote shell
- -a : archive mode
- -r : recurse into directories
- -z : compress file data
Task : Copy file from a local computer to a remote server
Copy file from /www/backup.tar.gz to a remote server called openbsd.nixcraft.in
$ rsync -v -e ssh /www/backup.tar.gz jerry@openbsd.nixcraft.in:~
Output:
Password:
sent 19099 bytes received 36 bytes 1093.43 bytes/sec
total size is 19014 speedup is 0.99
Please note that symbol ~ indicate the users home directory (/home/jerry).
Task : Copy file from a remote server to a local computer
Copy file /home/jerry/webroot.txt from a remote server openbsd.nixcraft.in to a local computer /tmp directory:
$ rsync -v -e ssh jerry@openbsd.nixcraft.in:~/webroot.txt /tmp
Password
Task: Synchronize a local directory with a remote directory
$ rsync -r -a -v -e "ssh -l jerry" --delete openbsd.nixcraft.in:/webroot/ /local/webroot
Task: Synchronize a remote directory with a local directory
$ rsync -r -a -v -e "ssh -l jerry" --delete /local/webroot openbsd.nixcraft.in:/webroot
Task: Synchronize a local directory with a remote rsync server
$ rsync -r -a -v --delete rsync://rsync.nixcraft.in/cvs /home/cvs
Task: Mirror a directory between my "old" and "new" web server/ftp
You can mirror a directory between my "old" (my.old.server.com) and "new" web server with the command (assuming that ssh keys are set for password less authentication)
$ rsync -zavrR --delete --links --rsh="ssh -l vivek" my.old.server.com:/home/lighttpd /home/lighttpd
===================================================
当需要把服务器上的文件复制到另外的机器上,可用rsync来同步文件。
一、服务器端配置:
# yum -y install xinetd
# vi /etc/xinetd.d/rsync
将如下代码
service rsync
{
disable = yes
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = –daemon
log_on_failure += USERID
}
中的 disable = yes 改成 disable = no
# /etc/init.d/xinetd start
注意:如果服务器上装有防火墙记得要打开端口,默认端口是873
# telnet 127.0.0.1 873
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
# iptables -A INPUT -s 192.168.0.0/255.255.255.0 -p tcp -m tcp --dport 873 -j ACCEPT
# iptables -A INPUT -p tcp -m tcp --dport 873 -j DROP
# vi /etc/rsyncd.conf
[backup]
path = /www
auth users = admin
uid = root
gid = root
secrets file = /etc/rsyncd.secrets
read only = no
[服务器代号]
path = 备份文件路径
auth users = 授权帐号
uid = 执行时的uid
gid = 执行时的gid
secrets file = 密码文件位置
read only = 是否只读
# vi /etc/rsyncd.secrets
admin:1234 #用户名:密碼
给文件正确的权限
# chown root:root /etc/rsyncd.secrets
# chmod 600 /etc/rsyncd.secrets
二、client 端进行同步
客户端默认好像已经装了rsync,没有的话装下:
# yum -y install rsync
执行异步同步操作:
/usr/bin/rsync -avz –progress admin@192.168.1.105::backup /www
同步命令说明:
1 显示目录内容
命令
——
a) rsync
b) rsync -r
c) rsync jack@192.168.0.1::
d) rsync ssh_user@192.168.0.1:
命令说明
———
a) 显示目录内容(第一层)
b) 递归显示目录内容
c) 显示远程主机目录内容
*注1:端口模式, 基于rsync用户的身份验证
*注2:rsync server上的目录必须具有xx7的权限.
d) 查看远程主机目录内容
*注1:remote shell模式, 通过ssh连接的基于系统本地用户的身份验证
*注2:这里只使用了一个冒号(:),同时用户名是远程主机的ssh 用户,密码也是ssh用户对应的密码。
*注3:使用””,则列出文件夹本身的信息。若要列出文件夹内容,应使用”/”。
参数说明
———
-r 对目录进行递归操作
2 本地目录之间同步
命令
——
a) rsync -av –progress / *** 注意(/) ***
b) rsync -av –progress
c) rsync -avu –progress –delete /
d) rsync -av –progress –temp-dir=/tmp /
命令说明
———
a) 同步src-dir目录下所有文件到dst-dir目录下
b) 同步src-dir目录下所有文件到dst-dir/src-dir目录下
c) 对src-dir目录内容向dst-dir目录下进行差异更新,有增加/更新则添加替换,有减少则对其删减
d) 比a)多了–temp-dir=/tmp,即指定/tmp为临时交换区,这样可以避免因目标目录空间不够引起的无法同步文件的错误。
参数说明
———
-a 相当于 -rlptgoD 的集合
-u 等同于 –update,在目标文件比源文件新的情况下不更新
-v 显示同步的文件
–progress 显示文件同步时的百分比进度、传输速率
–delete 删除目标目录中多于源目录的文件
3 异地主机之间同步
命令
——
a) rsync -avz –progress jack@192.168.0.1::/
b) rsync -avz –progress jack@192.168.0.1::/ –password-file=/home/jack/rsync.jack
c) rsync -avuz –progress –delete jack@192.168.0.1::/ –password-file=/home/jack/rsync.jack
d) rsync -avz –progress jack@192.168.0.1::/
命令说明
———
a) 同步本地目录的内容到远程主机192.168.0.1的目录下,jack是rsync数据库用户(参见3. /etc/rsync.secrets)
b) 通过自动读取用户密码而实现非交互登录文件同步
c) 较b)多了-u和–delete
d) 同步远程主机内容到本地目录