XEN SERVER BACKUP
- Category: 電腦相關
- Last Updated: Wednesday, 03 June 2015 09:12
- Published: Monday, 18 May 2015 11:36
- Written by sam
公司大部份的機器都架在Debian(Ubuntu) Xen Server上
目前已進貨新NAS一臺
該寫一下備份了
採用SNAPSHOT的方式來備份
最基本的就是先弄好NFS作為備份地
192.168.1.17:/volume1/root on /blvm type nfs (rw,tcp,addr=192.168.1.17)
目前這臺只有四臺主機,兩臺採用LVM,兩臺是FILE方式
root@pm-team-acer:~# xen list Name ID Mem VCPUs State Time(s) Domain-0 0 3249 8 r----- 409531.6 samtest 21 1024 2 -b---- 2165.5 keenhigh-git 24 384 2 -b---- 3028.7 ag-server 36 1024 1 -b---- 2524.3 artifactory-server 37 1024 1 -b---- 825.4
詳細見SH的相異之處
#!/bin/bash #sam141113 v0 #sam150515 v1 #sam150516 v2 #sam150603 add timer #for acer-192.168.1.21 #for lv in `lvdisplay /dev/pm-team-acer-vg | grep 'LV Path' | awk '{print $3}'` #{ cp -rf /etc/xen/* /blvm/acer DEST="/blvm/acer" SAM=`date "+%Y%m%d%H%M"` startTimer() { START_TIME=$(date) S_TIME=$(date +%s) } endTimer() { END_TIME=$(date) E_TIME=$(date +%s) DURATION=$(echo $((E_TIME - S_TIME))) if [[ ${DURATION} -le 60 ]] ; then echo "Backup Duration: ${DURATION} Seconds" else echo "Backup Duration: $(awk 'BEGIN{ printf "%.2f\n", '${DURATION}'/60}') Minutes" fi } for lv in $(</home/samlin/list); do startTimer echo "lv: $lv" LV_NAME="`basename $lv`" LV_SIZE="`lvs --units m |grep $LV_NAME |awk '{print $4}'`" LV_UUID="`lvdisplay $lv | grep 'LV UUID' | awk -F'LV UUID' '{print $2}' | sed 's/^ *//g'`" LV_SNAPNAME="SNAP_$LV_NAME" SAM1="`echo "$LV_NAME" |sed 's/-disk//g'`" echo "shutdown $SAM1" echo "LV NAME: $LV_NAME" echo "LVM Logical Volume: $lv" echo "Size: $LV_SIZE MB" echo "UUID: $LV_UUID" echo "Snapshot name: $LV_SNAPNAME" echo "Removing old snapshot (if any)..." lvremove -f "/dev/pm-team-acer-vg/$LV_SNAPNAME" echo "Creating snapshot..." lvcreate -L+$LV_SIZE --snapshot -n"/dev/pm-team-acer-vg/$LV_SNAPNAME" "$lv" sleep 5 echo "Backing up snapshot..." dd if="/dev/pm-team-acer-vg/$LV_SNAPNAME" bs=512k of=/dev/stdout |/usr/bin/pigz -9 -b 256 > "$DEST/$LV_SNAPNAME.$SAM.gz" echo "Removing snapshot..." lvremove -f "/dev/pm-team-acer-vg/$LV_SNAPNAME" echo "start $SAM1" echo "--------------Del more than 3--------------------" (ls -t $DEST/$LV_SNAPNAME* |head -n 3;ls -t $DEST/$LV_SNAPNAME*) |sort |uniq -u |xargs rm endTimer echo "------------------------------------------------" done echo "start backup Fix lvm" for fix in $(</home/samlin/list1); do startTimer NN=`echo $fix |awk -F',' '{print $1}'` LL=`echo $fix |awk -F',' '{print $2}'` echo stop ${NN} xl shutdown ${NN} echo Backing up ${NN} dd if=${LL} bs=512k of=/dev/stdout |/usr/bin/pigz -9 -b 256 > $DEST/${NN}.$SAM.gz echo start ${NN} xl create /etc/xen/${NN}.cfg echo "--------------Del more than 3--------------------" (ls -t $DEST/${NN}*.gz |head -n 3;ls -t $DEST/${NN}*.gz) |sort |uniq -u |xargs rm endTimer done #} > /blvm/backup-lvm-acer-sam.log #mutt -s "backup-lvm-acer" mis at fuhu.tw < /blvm/backup-lvm-acer-sam.log
以上是測試正常腳本
分隔兩部份
一區為使用lvcreate的方式,取得大小_建立snapshot_壓縮_至目的地_移除snapshot
一區為使用先停xen guest os的方式,先DD出來_壓縮_至目的地_啟動
snapshot的方式沒有停機,採直接DD到目的地
當然原本的方式是由LVM指令取得相關資訊
但由於許多臺機器並無開機及備份需求
所以改採用 cat 由檔案讀入方式
以下一份附上格式、內容
oot@pm-team-acer:~# cat list /dev/pm-team-acer-vg/keenhigh-git-disk /dev/pm-team-acer-vg/testgerrit-disk
root@pm-team-acer:~# cat list1 ag-server,/home/xen/domains/ag-server/disk.img artifactory-server,/home/xen/domains/artifactory-server/disk.img
##############################################
另一臺環境相同,差別為都是由LVM所建立
所以相對腳本少了許多
#!/bin/bash #sam141113 v0 #sam150515 v1 #sam150516 v2 #sam150603 add timer #for asus-192.168.1.20 #for lv in `lvdisplay /dev/pm-team | grep 'LV Path' | awk '{print $3}'` #{ cp -rf /etc/xen/* /blvm/asus DEST=/blvm/asus SAM=`date "+%Y%m%d%H%M"` startTimer() { START_TIME=$(date) S_TIME=$(date +%s) } endTimer() { END_TIME=$(date) E_TIME=$(date +%s) DURATION=$(echo $((E_TIME - S_TIME))) if [[ ${DURATION} -le 60 ]] ; then echo "Backup Duration: ${DURATION} Seconds" else echo "Backup Duration: $(awk 'BEGIN{ printf "%.2f\n", '${DURATION}'/60}') Minutes" fi } for lv in $(</home/samlin/list) do startTimer LV_NAME="`basename $lv`" LV_SIZE="`lvs --units m |grep $LV_NAME |awk '{print $4}'`" LV_UUID="`lvdisplay $lv | grep 'LV UUID' | awk -F'LV UUID' '{print $2}' | sed 's/^ *//g'`" LV_SNAPNAME="SNAP_$LV_NAME" echo "LV NAME: $LV_NAME" echo "LVM Logical Volume: $lv" echo "Size: $LV_SIZE MB" echo "UUID: $LV_UUID" echo "Snapshot name: $LV_SNAPNAME" echo "Removing old snapshot (if any)..." lvremove -f "/dev/pm-team/$LV_SNAPNAME" echo "Creating snapshot..." lvcreate -L+$LV_SIZE --snapshot -n"/dev/pm-team/$LV_SNAPNAME" "$lv" sleep 10 echo "Backing up snapshot..." dd if="/dev/pm-team/$LV_SNAPNAME" bs=512k of=/dev/stdout |/usr/bin/pigz -9 -b 256 > "$DEST/$LV_SNAPNAME.$SAM.gz" echo "Removing snapshot..." lvremove -f "/dev/pm-team/$LV_SNAPNAME" echo "--------------Del Old than 3--------------------" (ls -t $DEST/$LV_SNAPNAME* |head -n 3;ls -t $DEST/$LV_SNAPNAME*) |sort |uniq -u |xargs rm endTimer echo "-------------------------------------------------------" done #} > /blvm/backup-lvm-asus-sam.log #mutt -s "backup-lvm-asus" mis at fuhu.tw < /blvm/backup-lvm-asus-sam.log
##############################################
備份出來格式
目前還欠版本保留個數及發信通知功能待加入
要使用lvm的snapshot 記得查看是否有以下指令
##########################################
檔案保留三個
(ls -t $LV_SNAPNAME |head -n 3;ls -t $LV_SNAPNAME) |sort |uniq -u |xargs rm
使用mutt寄信
mutt -s "backup-lvm-asus" mis fuhu.tw < /blvm/backup-lvm-asus-sam.log
因為直接用crontab 排程寄信,所以直接mark掉shell 裡的mutt.