rsync – 備份資料

既然現在有了一台File server,也有了NAS,那是不是可以做雙從備份呢?當然可以,也一定要可以,這可是我當初的初衷阿~

我的作法是要把fail server的資料固定備份到NAS上面,要怎麼做呢?rsync是一個好幫手。

首先,先照的synology的官網教學(如何將資料從 Linux 電腦備份至 Synology NAS),打開rsync功能,因為官網寫得很詳細,這部份應該是沒有問題的,唯一要注意的就是,只有admin跟rsync這兩個帳號可以進行srync功能。

接下來,就是於file server上面安裝rsync套件

 apt-get install rsync 

然後呢?很簡單的,只要輸入

 rsync -av --delete --password-file=/etc/rsync.password /來源 rsync@192.168.1.x::NetBackup
其中
--delete 是在比對來源與目的地資料後,若來源沒有,但目的地有,就會刪除目的地資料
--password-file=/etc/rsync.password 是將連線密碼直接存在/etc/rsync.password,讓系統自己抓。
目的地的::表示透過rsync daemon傳送;一個:的話表示透過 ssh 或 rsh,那前面要加-e ssh;沒有的話表是本機傳送

部份參數
-v :觀察模式,可以列出更多的資訊,包括鏡像時的檔案檔名等;
-q :與 -v 相反,安靜模式,略過正常資訊,僅顯示錯誤訊息;
-r :遞迴複製!可以針對『目錄』來處理!很重要!
-u :僅更新 (update),若目標檔案較新,則保留新檔案不會覆蓋;
-l :複製連結檔的屬性,而非連結的目標原始檔案內容;
-p :複製時,連同屬性 (permission) 也保存不變!
-g :保存原始檔案的擁有群組;
-o :保存原始檔案的擁有人;
-D :保存原始檔案的裝置屬性 (device)
-t :保存原始檔案的時間參數;
-I :忽略更新時間 (mtime) 的屬性,檔案比對上會比較快速;
-z :在資料傳輸時,加上壓縮的參數!
-e :使用的通道協定,例如使用 ssh 通道,則 -e ssh
-a :相當於 -rlptgoD ,所以這個 -a 是最常用的參數了!

這樣就好了?當然沒有,我可是懶人耶,當然要電腦自己幫我備份阿,那該怎麼做呢?很簡單,就是將他寫到crontab裡面阿。另外,因為我只打算一週做兩次備份,因此我不打算讓NAS長時間開啟,但又不可能自己去手動開,那該怎麼做呢?很簡單,就是WOL,現在的NAS幾乎都有支援此功能了,我只要在備份開始前,請File Server發一個Magic Packet 封包喚醒NAS就好,怎麼做呢?先確定server上有安裝etherwake套件即可,然後輸入

etherwake -i ethx 00:11:22:33:44:55
指定由那個網卡發出Magic Packet

就可以輕鬆的把NAS叫起床摟~

目前我的crontab長這樣子,他可以讓我這懶人利用自動備份的方式保障我的資料安全。

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
30 5,17 * * * root apt-get update && apt-get -y upgrade
30 3 * * * root /home/username/iptables/systembackup.sh
40 3 * * * root /home/username/iptables/mysqlbackup.sh
05 2 * * 2,5 root etherwake 00:11:22:33:44:55
10 2 * * 2,5 root rsync -av --delete --password-file=/etc/rsync.password /來源 rsync@192.168.1.x::NetBackup
#

為什的都放在iptables下面,這…只是因為我懶惰、方便而已。

那..systembackup.sh跟mysqlbackup.sh是什麼呢?這也是備份scripts,主要在備份我的mysql跟系統資料,因為我的系統是安裝在隨身碟上面,我會把他每天把重要資料備份到硬碟上面,讓隨身碟不幸掛掉後,可以較快的還原資料。內容檔是參考網路上的人寫的。

systembackup.sh


#!/bin/sh
#for data backup

#備份資料的位置
backup_dir=填入自己的路徑

#設定要備份的目錄跟設定檔
blog=$backup_dir/www/blog
album=$backup_dir/www/album
iptables=$backup_dir/iptables
apache2=$backup_dir/apache2
sites=$backup_dir/apache2/sites-available
php5=$backup_dir/php5
samba=$backup_dir/samba
setting=$backup_dir/setting

#判斷目錄是否存在,若不存在則予以建立。
for dirs in $blog $album $iptables $apache2 $sites $php5 $samba $setting
do
[ ! -d "$dirs" ] && mkdir -p $dirs
done

#開始備份
cp -au /var/www/blog/* $blog
cp -au /var/www/album/* $album
cp -au /home/username/iptables/* $iptables
cp -au /etc/network/interfaces $setting
cp -au /etc/crontab $setting
cp -au /etc/apache2/apache2.conf $apache2
cp -au /etc/apache2/sites-available/* $sites
cp -au /etc/php5/apache2/php.ini $php5
cp -au /etc/samba/* $samba

 

mysqlbackup.sh


#!/bin/sh

# mysql_backup.sh: backup mysql databases and keep newest 5 days backup.
#
# Last updated: 20 March 2006
# ----------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2006 Sam Tang
# Feedback/comment/suggestions : http://www.real-blog.com/
# ----------------------------------------------------------------------

# your mysql login information
# db_user is mysql username
# db_passwd is mysql password
# db_host is mysql host
# -----------------------------
db_user="root"
db_passwd=""
db_host="localhost"

# the directory for story your backup file.
backup_dir="備份路徑"

# date format for backup file (dd-mm-yyyy)
time="$(date +"%Y.%m.%d %H:%M")" <- 這可以自己改成想看的時間日期格式

# mysql, mysqldump and some other bin's path
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
MKDIR="$(which mkdir)"
RM="$(which rm)"
MV="$(which mv)"
GZIP="$(which gzip)"

# check the directory for store backup is writeable
#test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0

# the directory for story the newest backup
test ! -d "$backup_dir/backup.0/" && $MKDIR -p "$backup_dir/backup.0/"

# get all databases
all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

for db in $all_db
do
$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP -9 > "$backup_dir/backup.0/$db $time.gz"
done

# delete the oldest backup
test -d "$backup_dir/backup.5/" && $RM -rf "$backup_dir/backup.5"

# rotate backup directory
for int in 4 3 2 1 0  <- 要備份多少歷史檔案,可以自行修正
do
if(test -d "$backup_dir"/backup."$int")
then
next_int=`expr $int + 1`
$MV "$backup_dir"/backup."$int" "$backup_dir"/backup."$next_int"
fi
done

exit 0;

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料