使用rsync可以实现文件备份同步,现实的一个使用场景是,在后台上传的模板文件,需要同步到另一台服务器,通过别的域名地址访问,这就需要把后台上传的文件自动同步到对外可访问的服务器。
记录一个rsync同步文件的sh脚本,作为例子:
#!/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
再来一个:
#!/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脚本文件,实现文件同步,如下:exec ( "/usr/local/bin/rsync_act_vip_xunlei_com_to_twin14419.sh" );
这样当在后台上传文件完成后,即可使用exec执行同步脚本把文件同步到另一台服务器。
当然rsync还有更多的使用场景。
留言与评论(共有 0 条评论) |