VPS伺服器備份網站到七牛雲端儲存的shell指令碼(第二版)
上一篇文章釋出了備份網站到七牛的第一版,這次應回覆要求先公佈同步原理及 shell 原始碼,讓喜歡折騰的人有力可使。其實,這個備份工具的原理非常簡單,就是借用了官方的同步工具,配合簡單的 shell 語句即可完成。
一、備份原理
簡介
qrsync 是一個根據七牛雲端儲存 API 實現的簡易命令列輔助上傳同步工具,支援斷點續上傳,增量同步,它可將使用者本地的某目錄的檔案同步到七牛雲端儲存中,同步或上傳幾百 GB 甚至上 TB 的檔案毫無鴨梨。
注意:被同步的檔名和路徑必須是 utf8 編碼,非 utf8 的檔名和路徑將會同步失敗
下載
qrsync 命令列輔助同步工具下載地址:
Shell123456 | Mac OS:http://devtools.qiniu.io/qiniu-devtools-darwin_amd64-current.tar.gzLinux64bits:http://devtools.qiniu.io/qiniu-devtools-linux_amd64-current.tar.gzLinux32bits:http://devtools.qiniu.io/qiniu-devtools-linux_386-current.tar.gzLinux ARMv6:http://devtools.qiniu |
用法
先建立一個配置檔案(JSON 格式),比如叫 conf.json,內容如下:
Shell1234567891011 | {"src":"/home/your/sync_dir","dest":"qiniu:access_key=<AccessKey>&secret_key=<SecretKey>&bucket=<Bucket>&key_prefix=<KeyPrefix>&threshold=<Threshold>","deletable":0,"debug_level":1} |
簡單說明:
①、src 表示需要同步的本地目錄,比如網站檔案打包存放路徑:/www/web_backup,或是你網站檔案路徑:/home/domain/zhangge.net/public_html
②、2 個 Key:access_key和secret_key,進入七牛賬號設定介面可以獲取到。
③、bucket 表示存放備份檔案的七牛空間,建議新建一個。
④、key_prefix 表示存放在七牛空間的上一級目錄名稱,比如key_prefix=backup/ 則表示備份檔案將存放到目標空間下的 backup 資料夾中。引數留空則表示存放到改空間的根目錄。
⑤、threshold表示檔案分塊上傳的塊大小,單位為位元組(Byte),可根據你本地的網路頻寬來確定。
⑥、deletable 是否同步刪除七牛雲上的檔案,通常設定 0,當本地檔案刪除時並不刪除儲存在七牛的對應檔案。如果你想刪除本地檔案的同時也刪除儲存在儲存在七牛的檔案,則設定為 1。
Ps:以上即為此工具必備的引數,更多引數請至七牛官方文件檢視。
在建立完 conf.json 配置檔案後,就可以執行 qrsync 程式進行同步。
Unix/Linux/MacOS 系統可以用如下命令列:
Shell1 | ./qrsync/path/of/your-conf.json |
Windows 系統使用者在 [開始] 選單欄選擇 [執行] 輸入 cmd 回車即可開啟 DOS 命令列視窗。
假設你的 qrsync.exe 和配置檔案都存放在 d:/tools/,則如下執行:
Shell1 | D:\tools\qrsync.exe D:\tools\your-conf.json |
需要注意的是,qrsync 是增量同步的,如果你上一次同步成功後修改了部分檔案,那麼再次執行 qrsync 時只同步新增的和被修改的檔案。當然,如果上一次同步過程出錯了,也可以重新執行 qrsync 程式繼續同步。
以上即為各種系統將檔案同步到七牛雲端儲存的原理方法,我們換個想法:當我們同步的是網站打包的檔案及資料庫,那不就變相的實現了將網站備份到七牛雲端儲存了嗎?!
二、第二版工具 shell 原始碼(未完待續...)
仔細看了備份原理,相信有 shell 基礎的朋友很容易就可以製作一個自動備份的工具了。
下面公佈下張戈寫的第二版工具原始碼:
相比第一版:
①、增加了資料庫和網站檔案分離機制,可分開設定各自的備份間隔,比如資料庫我需要每天備份一次,而網站檔案我打算 7 天備份一次;
②、資料庫備份檔案先壓縮後上傳,減小檔案體積,節省空間流量。
工具檔案結構:
Shell12345678 | [root@zhangge.net~]# tree qiniuqiniu|--backup#本地默認同步目錄|--conf#配置檔案存放目錄|--main.sh#初始化shell指令碼|--QN_backup.sh#備份及同步shell指令碼`--qrsync2directories,3files |
初始化指令碼原始碼:
Shell123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 | [root@zhangge.net~]#vim main.sh#!/bin/bash#Author:ZhangGe#Des:Backup you website to qiniu CDN.#Date:2014.7.29#config Em.filepath(){read-p"Please input the webroot like /domain/zhangge.net/public_html:"filepathif[-z$filepath];thenechoNothing input!Please retry..filepathelif[!-d$filepath];thenecho$filepathnotfound!Press Enter_key retry!filepathfi}access_key(){read-p"Please input the access_key:"access_keyif[-z$access_key];thenechoNothing input!Please retry..access_keyfi}secret_key(){read-p"Please input the secret_key:"secret_keyif[-z$secret_key];thenechoNothing input!Please retry..secret_keyfi}bucket(){read-p"Please input the bucket name:"bucketif[-z$bucket];thenechoNothing input!Please retry..bucketfi}mysqluser(){read-p"Please input the mysql username:"mysqluserif[-z$mysqluser];thenechoNothing input!Please retry..mysqluserfi}mysqlpd(){read-p"Please input the mysql password:"mysqlpdif[-z$mysqlpd];thenechoNothing input!Please retry..mysqlpdfi}backpath(){read-p"Please input the backup path:"backpathif[-z$backpath];thenbackpath=`pwd`/backupelif[!-d$backpath];thenecho$backpathNotexist!!Please retry..backpathfi}dbname(){read-p"Please input the database name:"dbnameif[-z$dbname];thenechoNothing input!Please retry..dbnamefi}d_time(){read-p"Please input the db backup rate time(like 1/2/3..30):"d_timeif[-z$d_time];thend_time=1fi}w_time(){read-p"Please input the web backup rate time(like 1/2/3..30):"w_timeif[-z$w_time];thenw_time=7fi}input(){clearfilepathaccess_keysecret_keybucketmysqlusermysqlpddbnamebackpathd_timew_time}inputcd$(cd$(dirname$BASH_SOURCE)&&pwd)echo\{\"src\":\"$backpath\",\"dest\":\"qiniu:access_key=$access_key\&secret_key=$secret_key\&bucket=$bucket\&key_prefix=backup\/\&threshold=512000\",\"deletable\":0,\"debug_level\":1\}>./conf/qrsync.conf#crontabtest-f/var/spool/cron/root&&cp-f/var/spool/cron/root/var/spool/cron/root_bakgrep-v"QN_backup"/var/spool/cron/root>/var/spool/cron/rootecho"#Add by QN_backup Scripts">>/var/spool/cron/rootecho"0 0 */$d_time * * sh `pwd`/QN_backup.sh db $mysqluser $mysqlpd $dbname $backpath">>/var/spool/cron/rootecho"0 0 */$w_time * * sh `pwd`/QN_backup.sh file $filepath $backpath ">>/var/spool/cron/root#reload the crontab/sbin/service crond reload2>/dev/nullecho=========================The crontablist===========================< |