源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

CentOS5 + rsync 同步2台服务器的文件

  • 时间:2022-08-19 12:29 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:CentOS5 + rsync 同步2台服务器的文件
[h2]Always use rsync over ssh[/h2] 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. [h2]rsync command common options[/h2] [list] [*][b]--delete[/b] : delete files that don't exist on sender (system) [/*][*][b]-v[/b] : Verbose (try[b]-vv[/b] for more detailed information) [/*][*][b]-e "ssh options"[/b] : specify the ssh as remote shell [/*][*][b]-a[/b] : archive mode [/*][*][b]-r[/b] : recurse into directories [/*][*][b]-z[/b] : compress file data [/*][/list] [h2]Task : Copy file from a local computer to a remote server[/h2] 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[b]~[/b] indicate the users home directory (/home/jerry). [h2]Task : Copy file from a remote server to a local computer[/h2] 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 [h2]Task: Synchronize a local directory with a remote directory[/h2]
$ rsync -r -a -v -e "ssh -l jerry" --delete openbsd.nixcraft.in:/webroot/ /local/webroot
[h2]Task: Synchronize a remote directory with a local directory[/h2]
$ rsync -r -a -v -e "ssh -l jerry" --delete /local/webroot openbsd.nixcraft.in:/webroot
[h2]Task: Synchronize a local directory with a remote rsync server[/h2]
$ rsync -r -a -v --delete rsync://rsync.nixcraft.in/cvs /home/cvs
[h2]Task: Mirror a directory between my "old" and "new" web server/ftp[/h2] 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来同步文件。 一、服务器端配置: [b]# yum -y install xinetd[/b] [b]# vi /etc/xinetd.d/rsync[/b] 将如下代码
[u]复制代码[/u] 代码如下:
service rsync { disable = yes socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = –daemon log_on_failure += USERID }
中的[b]disable = yes [/b]改成[b]disable = no[/b]
然后启动 xinetd
[u]复制代码[/u] 代码如下:
# /etc/init.d/xinetd start
注意:如果服务器上装有防火墙记得要打开端口,默认端口是873
[u]复制代码[/u] 代码如下:
# 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
[b]# vi /etc/rsyncd.conf[/b]
[u]复制代码[/u] 代码如下:
[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 = 是否只读
[b]# vi /etc/rsyncd.secrets[/b]
[u]复制代码[/u] 代码如下:
admin:1234 #用户名:密碼
给文件正确的权限 [b]# chown root:root /etc/rsyncd.secrets # chmod 600 /etc/rsyncd.secrets[/b]
[b]二、client 端进行同步 [/b]客户端默认好像已经装了rsync,没有的话装下: [b]# yum -y install rsync[/b] 执行异步同步操作:
/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) 同步远程主机内容到本地目录
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部