Advertisement
Guest User

Untitled

a guest
Apr 5th, 2018
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.02 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Backup large virtual hard disks stored on an LV, while in use by running VMs
  4.  
  5. # This script quiesces the guest filesystems, takes an LVM Snapshot, and backs up the contents of that snapshot to the target directory with cp.
  6. # It is designed to be used with large virtual disk images, which are impractical to incrementally backup or keep many copies of.
  7.  
  8. # Settings used by the below, customize this
  9. # No trailing slash on target directory
  10. TARGET_DIR=
  11. VG_PATH=
  12. LV_NAME=
  13. # Be sure to leave adequate room for disk writes during the backup
  14. SNAPSHOT_SIZE=10G
  15. SNAPSHOT_NAME=kvmbackup-`date +%s`
  16. MOUNT_DIR=/mnt/$SNAPSHOT_NAME
  17.  
  18. # Ensure there is nothing mounted to the mount directory
  19. umount $MOUNT_DIR > /dev/null 2>&1
  20.  
  21. # Ensure there is no LV with the same name
  22. lvremove -f $VG_PATH/$SNAPSHOT_NAME > /dev/null 2>&1
  23.  
  24. # Keep the most recent backup before the current run
  25. # If the target directory exists
  26. if [ -d "$TARGET_DIR" ]
  27. then
  28.     # And if there are files in the directory
  29.     TARGET_FILES=$TARGET_DIR/*
  30.     if [ ${#TARGET_FILES[@]} -gt 0 ]
  31.     then
  32.         if [ -d "$TARGET_DIR.prev" ]
  33.         then
  34.             # Overwrite the second-latest backup with the latest backup
  35.             mv -f $TARGET_FILES "$TARGET_DIR.prev/"
  36.             echo "Moved most recent backup out of the way."
  37.         else
  38.             # Create the directory for the previous backup and then move the latest backup into it
  39.             mkdir -p "$TARGET_DIR.prev"
  40.             mv -f $TARGET_FILES "$TARGET_DIR.prev/"
  41.             echo "Moved most recent backup out of the way."
  42.         fi
  43.     fi
  44. fi
  45.  
  46. # Freeze and quiesce the guest filesystems
  47. for name in $(
  48.     # Get the names of all running VMs
  49.     for vm in $(sudo virsh list | tail -n +3)
  50.     do
  51.         echo $vm
  52.     done | sed '/running/d' | awk 'NR%2==0' )
  53. do
  54.     # Quiesce the file system of each VM
  55.     sudo virsh domfsfreeze $name > /dev/null
  56.     echo "Froze filesystem on $name"
  57. done
  58.  
  59. # Take the snapshot
  60. lvcreate --size $SNAPSHOT_SIZE --snapshot --name $SNAPSHOT_NAME $VG_PATH/$LV_NAME || exit 1
  61.  
  62. # Thaw the guest filesystems
  63. for name in $(
  64.     # Get the names of all running VMs
  65.     for vm in $(sudo virsh list | tail -n +3)
  66.     do
  67.         echo $vm
  68.     done | sed '/running/d' | awk 'NR%2==0' )
  69. do
  70.     # Quiesce the file system of each VM
  71.     sudo virsh domfsthaw $name > /dev/null
  72.     echo "Thawed filsystem on $name."
  73. done
  74.  
  75. # Make the mount directory
  76. mkdir $MOUNT_DIR || exit 1
  77. echo "Created mount point."
  78. # Mount the snapshot
  79. # Add the 'nouuid' option if backing up an XFS formatted LV
  80. mount -o ro $VG_PATH/$SNAPSHOT_NAME $MOUNT_DIR || exit 1
  81. echo "Mounted snapshot."
  82.  
  83. # Copy the contents of the snapshotted volume to the target
  84. echo "Starting copy..."
  85. cp -r $MOUNT_DIR/* $TARGET_DIR/
  86. echo "Copy complete."
  87.  
  88. # Unmount and remove the LV snapshot
  89. umount $MOUNT_DIR
  90. echo "Unmounted snapshot."
  91. lvremove -f $VG_PATH/$SNAPSHOT_NAME
  92. echo "Removed snapshot."
  93.  
  94. # Remove the mount directory
  95. rmdir $MOUNT_DIR
  96. echo "Removed mount point."
  97. echo "Backup complete."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement