Linux rsync同步文件脚本

使用rsync能够实现文件备份同步,现实的一个使用场景是,在后台上传的模板文件,须要同步到另外一台服务器,经过别的域名地址访问,这就须要把后台上传的文件自动同步到对外可访问的服务器。bash

记录一个rsync同步文件的sh脚本,做为例子:服务器

Vimssh

#!/bin/bash
rsync -avz --password-file=/etc/rsync.msg.key /usr/local/zeus/htdocs/act.example.com  vip@t11.example.net::act.vip1  --port=873
rsync -avz --password-file=/etc/rsync.msg.key /usr/local/zeus/htdocs/act.example.com  vip@t12.example.net::act.vip1  --port=873
servers='t16b21 c16b181'
src='/usr/local/zeus/htdocs/example.com'
for server in $servers
do
        dst="vip@${server}.sandai.net::act.vip1"
        rsync -avz --password-file=/etc/rsync.msg.key $src $dst  --port=8873
#	rsync -avz -e ssh /usr/local/zeus/htdocs/act.example.com root@$server.sandai.net:/usr/local/zeus/htdocs/
done

再来一个:函数

Vim.net

#!/bin/bash
server="t16b21 c16b181"
for i in $server
do
rsync -avz /usr/local/zeus/htdocs/images.client.example.com/ vip@$i.example.net::images.client/ --port=8873 --password-file=/etc/client.secret
rsync -avz /usr/local/zeus/htdocs/images.client.example.com/ vip@$i.example.net::images.client/ --port=873 --password-file=/etc/client.secret
done

在PHP中使用exec函数能够执行sh脚本文件,实现文件同步,以下:code

PHPserver

exec ( "/usr/local/bin/rsync_act_vip_xunlei_com_to_twin14419.sh" );

这样当在后台上传文件完成后,便可使用exec执行同步脚本把文件同步到另外一台服务器。ip

固然rsync还有更多的使用场景。同步

相关文章
相关标签/搜索