Advertisement
Guest User

nbolte

a guest
Nov 12th, 2012
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.48 KB | None | 0 0
  1. #!/bin/sh
  2. VG_NAME="vg_virtualbox"
  3. LV_NAME="lv_vms"
  4. LV_BACKUP_NAME="${LV_NAME}_backup"
  5.  
  6. MOUNT_LOCATION="/mnt/${LV_BACKUP_NAME}"
  7.  
  8. LVCREATE=$(which lvcreate)
  9. if [[ $? != 0 ]]; then
  10.   echo "lvcreate not found!"
  11.   exit 1
  12. fi
  13. LVREMOVE=$(which lvremove)
  14. if [[ $? != 0 ]]; then
  15.   echo "lvremove not found!"
  16.   exit 1
  17. fi
  18.  
  19. function pre_backup()
  20.   {
  21.   if [[ -L /dev/$VG_NAME/$LV_BACKUP_NAME ]]; then
  22.     echo "Logical Volume: ${LV_BACKUP_NAME} already exists"
  23.     exit 1
  24.   fi
  25.  
  26.   $LVCREATE -l100%FREE -s -n $LV_BACKUP_NAME /dev/$VG_NAME/$LV_NAME
  27.   if [[ $? != 0 ]]; then
  28.     echo "lvcreate failed."
  29.     exit 1
  30.   fi
  31.  
  32.   if [[ ! -d $MOUNT_LOCATION ]]; then
  33.     mkdir -p $MOUNT_LOCATION
  34.     if [[ $? != 0 ]]; then
  35.       echo "Could not create mountpoint: ${MOUNT_LOCATION}"
  36.       exit 1
  37.     fi
  38.   fi
  39.   mount -o ro /dev/$VG_NAME/$LV_BACKUP_NAME $MOUNT_LOCATION
  40.   }
  41.  
  42. function post_backup()
  43.   {
  44.   # Do not care about the exit state from umount.
  45.   umount $MOUNT_LOCATION
  46.   if [[ -L /dev/$VG_NAME/$LV_BACKUP_NAME ]]; then
  47.     lvremove -f /dev/$VG_NAME/$LV_BACKUP_NAME
  48.   else
  49.     echo "Logical Volume: ${LV_BACKUP_NAME} not found"
  50.     exit 1
  51.   fi
  52.   }
  53.  
  54.  
  55.  
  56. if [[ "$1" == "pre" ]]; then
  57.  pre_backup
  58. elif [[ "$1" == "post" ]]; then
  59.  post_backup
  60. else
  61.   echo "Usage: $0 <command>"
  62.   echo "Commands available:"
  63.   echo "    pre             Runs pre backup tasks (create LVM snapshot)"
  64.   echo "    post            Runs post backup tasks (Delete LVM snapshot)"
  65.   exit -1
  66. fi
  67.  
  68. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement