這裡使用LVM 邏輯磁碟對 Mongodb進行超速備份,mongodb到了幾十G以上,用mongodb工具備份的速度,會讓我們感到相當蛋疼的慢,這時候我們可以進行mongdb的data目錄進行LVM快照備份。
lvm 參考下我的另一個文章 http://rfyiamcool.blog.51cto.com/1030776/959704
650) this.width=650;" border="0" src="http://www.bkjia.com/uploads/allimg/131228/224UMI9-0.png" />
原理:通過lvm快照給lvm真身拍個照片,當lvm真身發送改變時,lvm快照把lvm真身改變之前的內容存放在快照上,這樣在lvm快照有效這段時間內,我們看到的lvm快照上的內容始終是lvm真身在建立lvm快照時內容,通過備份lvm快照即可達到線上備份lvm真身的目的。需要注意的是,當lvm快照比lvm真身小時,若lvm真身發生的改變大於lvm快照,則lvm快照將變得無法讀取而失效; 若lvm快照大於等於lvm真身,則不會發生前面的情況。 |
用法:
-g 是VG組 -v 是具體的lv邏輯盤
./mongolvmbackup.sh -g myvolgroup -v mongodata
地址: http://rfyiamcool.blog.51cto.com
指令碼:
#!/bin/bash -e
# Backup the local MongoDB database by:
#
#
# 1 lvm snapshot mongodb's data volume
# 2 unlock mongodb
# 3 bzip2 the snapshot into tempdir
#在壓縮的備份將暫時分級傳輸
TARGET_DIR=/mnt/mongosnaps
#保留的天數
LOCAL_RETENTION_DAYS=7
#壓縮的等級
COMPRESS_PROG=bzip2
COMPRESS_SUFFIX=tbz2
COMPRESS_LEVEL=6
print_help() {
echo
echo "$0: -g <lvmgroup> -v <lvmvolume> -b <s3 bucket>"
echo
echo "Snapshot & compress MongoDB databases present on this host. Place them in"
echo "$TARGET_DIR and create a 'latest' symlink."
echo
exit 0
}
# Check for some required utilities
command lvcreate --help >/dev/null 2>&1 || { echo "Error: lvcreate is required. Cannot continue."; exit 1; }
command lvremove --help >/dev/null 2>&1 || { echo "Error: lvremove is required. Cannot continue."; exit 1; }
command $COMPRESS_PROG -V >/dev/null 2>&1 || { echo "Error: compression util required. Cannot continue."; exit 1; }
s3bucket=''
vgroup=''
volume=''
while [ $# -gt 0 ]
do
case $1 in
-h) print_help ;;
--help) print_help ;;
-b) s3bucket=$2 ; shift 2 ;;
-g) vgroup=$2 ; shift 2 ;;
-v) volume=$2 ; shift 2 ;;
*) shift 1 ;;
esac
done
#檢查卷
if [ "$vgroup" == "" ]
then
echo "No group set, won't continue"
exit 1
fi
if [ "$volume" == "" ]
then
echo "No volume set, won't continue"
exit 1
fi
# Check volume is a real LVM volume
if ! lvdisplay "/dev/$vgroup/$volume" >/dev/null 2>/dev/null
then
echo "/dev/$vgroup/$volume is not a real LVM volume!"
exit 1
fi
# Figure out where to put it
date=`date +%F_%H%M`
targetfile="${volume}-${date}-snap.${COMPRESS_SUFFIX}"
# =============================================================================
# Print a meaningful banner!
echo "==================== LVM MONGODB SNAPSHOT SCRIPT ====================="
echo
echo " Snapshotting: /dev/${vgroup}/${volume}"
echo " Target: ${TARGET_DIR}/${targetfile}"
echo
#如果目標檔案夾不存在的話,建立
if [ ! -d "$TARGET_DIR" ]
then
echo "Your target dir ${TARGET_DIR} doesn't exist and I'm too cowardly to create it"
exit 1
fi
# 建立快照
snapvol="$volume-snap"
echo "Freezing MongoDB before LVM snapshot"
mongo -eval "db.fsyncLock()"
echo
echo "Taking snapshot $snapvol"
lvcreate --snapshot "/dev/$vgroup/$volume" --name "$snapvol" --extents '90%FREE'
echo
echo "Snapshot OK; unfreezing DB"
mongo -eval "db.fsyncUnlock()"
echo
echo
# 掛載LVM快照
mountpoint=`mktemp -t -d mount.mongolvmbackup_XXX`
mount -v -o ro "/dev/${vgroup}/${snapvol}" "${mountpoint}"
echo
find "$TARGET_DIR" -iname "*-snap.${COMPRESS_SUFFIX}" -mtime +${LOCAL_RETENTION_DAYS} -delete
echo
# 按照我們先前設定的比率進行壓縮
echo "Compressing snapshot into ${TARGET_DIR}/${targetfile}"
cd "${mountpoint}"
tar cv * | $COMPRESS_PROG "-${COMPRESS_LEVEL}" -c > "${TARGET_DIR}/${targetfile}"
echo
cd -
cd "$TARGET_DIR"
rm -vf latest.${COMPRESS_SUFFIX}
ln -v -s ${targetfile} latest.${COMPRESS_SUFFIX}
cd -
echo
echo
# 卸載並刪除用於備份的那個快照
echo "Removing temporary volume..."
umount -v "$mountpoint"
rm -rvf "$mountpoint"
echo
lvremove -f "/dev/${vgroup}/${snapvol}"
echo
echo
本文出自 “峰雲,就她了。” 部落格,請務必保留此出處http://rfyiamcool.blog.51cto.com/1030776/1195201