使用LVM對Mongodb進行速度快照和壓縮備份

來源:互聯網
上載者:User

這裡使用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邏輯盤

 

  1. ./mongolvmbackup.sh -g myvolgroup -v mongodata 

 地址: http://rfyiamcool.blog.51cto.com

指令碼:

  1. #!/bin/bash -e 

  2. # Backup the local MongoDB database by: 

  3. # 

  4. #  

  5. #  1  lvm snapshot mongodb's data volume 

  6. #  2  unlock mongodb 

  7. #  3   bzip2 the snapshot into tempdir 

  8. #在壓縮的備份將暫時分級傳輸 

  9. TARGET_DIR=/mnt/mongosnaps 

  10. #保留的天數 

  11. LOCAL_RETENTION_DAYS=7

  12. #壓縮的等級 

  13. COMPRESS_PROG=bzip2

  14. COMPRESS_SUFFIX=tbz2

  15. COMPRESS_LEVEL=6

  16. print_help() { 

  17.   echo 

  18.   echo "$0: -g <lvmgroup> -v <lvmvolume> -b <s3 bucket>" 

  19.   echo 

  20.   echo "Snapshot & compress MongoDB databases present on this host.  Place them in" 

  21.   echo "$TARGET_DIR and create a 'latest' symlink." 

  22.   echo 

  23.   exit 0 

  24. } 

  25. # Check for some required utilities 

  26. command lvcreate --help >/dev/null 2>&1 || { echo "Error: lvcreate is required.  Cannot continue."; exit 1; } 

  27. command lvremove --help >/dev/null 2>&1 || { echo "Error: lvremove is required.  Cannot continue."; exit 1; } 

  28. command $COMPRESS_PROG -V >/dev/null 2>&1 || { echo "Error: compression util required.  Cannot continue."; exit 1; } 

  29. s3bucket=''

  30. vgroup=''

  31. volume=''

  32. while [ $# -gt 0 ] 

  33. do 

  34.   case $1 in 

  35.     -h) print_help ;; 

  36.     --help) print_help ;; 

  37.     -b) s3bucket=$2 ; shift 2 ;; 

  38.     -g) vgroup=$2 ; shift 2 ;; 

  39.     -v) volume=$2 ; shift 2 ;; 

  40.     *) shift 1 ;; 

  41.   esac 

  42. done 

  43. #檢查卷 

  44. if [ "$vgroup" == "" ] 

  45. then 

  46.   echo "No group set, won't continue" 

  47.   exit 1 

  48. fi 

  49. if [ "$volume" == "" ] 

  50. then 

  51.   echo "No volume set, won't continue" 

  52.   exit 1 

  53. fi 

  54. # Check volume is a real LVM volume 

  55. if ! lvdisplay "/dev/$vgroup/$volume" >/dev/null 2>/dev/null 

  56. then 

  57.   echo "/dev/$vgroup/$volume is not a real LVM volume!" 

  58.   exit 1 

  59. fi 

  60. # Figure out where to put it 

  61. date=`date +%F_%H%M` 

  62. targetfile="${volume}-${date}-snap.${COMPRESS_SUFFIX}"

  63. # ============================================================================= 

  64. # Print a meaningful banner! 

  65. echo "==================== LVM MONGODB SNAPSHOT SCRIPT =====================" 

  66. echo 

  67. echo "  Snapshotting: /dev/${vgroup}/${volume}" 

  68. echo "  Target:       ${TARGET_DIR}/${targetfile}" 

  69. echo 

  70. #如果目標檔案夾不存在的話,建立 

  71. if [ ! -d "$TARGET_DIR" ] 

  72. then 

  73.   echo "Your target dir ${TARGET_DIR} doesn't exist and I'm too cowardly to create it" 

  74.   exit 1 

  75. fi 

  76. # 建立快照 

  77. snapvol="$volume-snap"

  78. echo "Freezing MongoDB before LVM snapshot" 

  79. mongo -eval "db.fsyncLock()" 

  80. echo 

  81. echo "Taking snapshot $snapvol" 

  82. lvcreate --snapshot "/dev/$vgroup/$volume" --name "$snapvol" --extents '90%FREE' 

  83. echo 

  84. echo "Snapshot OK; unfreezing DB" 

  85. mongo -eval "db.fsyncUnlock()" 

  86. echo 

  87. echo 

  88. # 掛載LVM快照 

  89. mountpoint=`mktemp -t -d mount.mongolvmbackup_XXX` 

  90. mount -v -o ro "/dev/${vgroup}/${snapvol}" "${mountpoint}" 

  91. echo 

  92. find "$TARGET_DIR" -iname "*-snap.${COMPRESS_SUFFIX}" -mtime +${LOCAL_RETENTION_DAYS} -delete 

  93. echo 

  94. # 按照我們先前設定的比率進行壓縮 

  95. echo "Compressing snapshot into ${TARGET_DIR}/${targetfile}" 

  96. cd "${mountpoint}" 

  97. tar cv * | $COMPRESS_PROG "-${COMPRESS_LEVEL}" -c > "${TARGET_DIR}/${targetfile}" 

  98. echo 

  99. cd - 

  100. cd "$TARGET_DIR" 

  101. rm -vf latest.${COMPRESS_SUFFIX} 

  102. ln -v -s ${targetfile} latest.${COMPRESS_SUFFIX} 

  103. cd - 

  104. echo 

  105. echo 

  106. # 卸載並刪除用於備份的那個快照 

  107. echo "Removing temporary volume..." 

  108. umount -v "$mountpoint" 

  109. rm -rvf "$mountpoint" 

  110. echo 

  111. lvremove -f "/dev/${vgroup}/${snapvol}" 

  112. echo 

  113. echo 

 

 

本文出自 “峰雲,就她了。” 部落格,請務必保留此出處http://rfyiamcool.blog.51cto.com/1030776/1195201

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.