Linux rsync同步檔案指令碼分享

來源:互聯網
上載者:User

使用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還有更多的使用情境。

tar exclude排除某個目錄

記錄一下tar壓縮命令排除某個目錄文法:

Vim

 代碼如下 複製代碼

tar -zcvf daili.tar.gz --exclude=./inc/log --exclude=./api/tmp ./

以上命令是打包目前的目錄下除了兩個exlude註明的目錄。

壓縮結束有一句提示:tar: .: file changed as we read it,不過壓縮包解壓出來沒發現遺漏。先無視。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.