使用LVM对Mongodb进行速度快照和压缩备份

这里使用LVM 逻辑磁盘对 Mongodb进行超速备份,mongodb到了几十G以上,用mongodb工具备份的速度,会让我们感到相当蛋疼的慢,这时候我们可以进行mongdb的data目录进行LVM快照备份。

lvm 参考下我的另一个文章 http://rfyiamcool.blog.51cto.com/1030776/959704


000927115.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



你可能感兴趣的:(mongodb,mongodb,mongodb,mongodb,复制,lvm,集群管理,mongos)