Scripting LVM Snapshot Backups

Discussion in 'HOWTO-Related Questions' started by atjensen11, Feb 28, 2009.

  1. atjensen11

    atjensen11 New Member

    I have a pretty simple, but tested and validated (unfortunately) backup script that I wrote. I am currenlty running the same script on each physical machine. Each machine backing up to a different external HD.

    However, now that the new server I built is using Xen with LVM, I am rebuilding most of my physical machines as Xen virtual machines. So I believe I can simplify my backups by just taking snapshots of each LV for the virtual machines on the Dom0. I can then use my existing script to TAR that LV snapshot.

    So I found this tutorial which is exactly what I want to do. I followed the tutorial and it works great for my situation. I would now like to automate it through a bash script so that I can create a cron job that runs in the middle of the night to perform the backups.

    My question becomes, when I delete a LV (i.e. the snapshot volume was I am done with it), the user must enter y or n at the command line to confirm. How would I write my script to handle this extra confirmation step?
  2. falko

    falko Super Moderator ISPConfig Developer

    lvremove -f /dev/server1/rootsnapshot
    (see man lvremove)
  3. atjensen11

    atjensen11 New Member

    Thanks Falko. The -f command line parameter helped get past the confirmation dialog that would appear when issuing the lvremove command.

    My script works really good right now. I am moving all of my physical servers to a Xen setup using LVM. I plan on using the script to iterate through the logical volumes and perform the backup on each one.

    These are the steps right now that the backup script performs:

    1. Create a snapshot logical volume
    2. Verify the directory where the TAR file will be stored exists
    3. Mount the snapshot logical volume to a temp location
    4. Perform the backup using TAR
    5. Unmount the snapshot logical volume at the temp location
    6. Remove (delete) the snapshot logical volume
    7. Send a confirmation email

    I know how to test if directories exist in my bash script (Step 2 above). How can I test if the logical volume exists before performing the backup?

    Can I simplify the script by not mounting/unmounting the snapshot logical volume (Steps 3 & 5 above)?

    Is there a way to determine the size of the logical volume that a snapshot volume will be created from? Currently, the size of the snapshot volume is hard coded in my script. I would rather test for the size of the logical volume prior to creating the snapshot volume and then pass the size to my lvcreate command.

    Last edited: Mar 7, 2009
  4. atjensen11

    atjensen11 New Member

    With some help from Google, I think I answered a few of my own questions.

    Reading the MAN pages a bit, I found that I could use the lvdisplay command with the -c parameter which would output all of the details about the logical volume in one string with colons between the fields. The size is the seventh field and is reported in KB. I then found that I could use the cut command to parse the fields by the colon delimeter. So I came up with the solution below.

    $LVM_NAME_PATH is the name of the logical volume for which a snapshot volume will be created.

    $LVM_SNAPSHOT_NAME is the name of the new snapshot volume that is to be created.

    # Query size of Logical Volume
    LVM_SIZE="`lvdisplay -c $LVM_NAME_PATH | cut -d':' -f7`K"
    # Create Snapshot Logical Volume
    I wasn't so sure about this one, so I copied a previous example where I had scripted a check to see if a directory existed and then tested it on the logical volume name. The following appears to work.

    # Verify Logical Volume Snapshot Exits
    if [ ! -d $LVM_SNAPSHOT_NAME_PATH ]; then
            echo "Error - The specified snapshot volume $LVM_SNAPSHOT_NAME_PATH could not be found or does not exist.  Operation canceled."
            exit 1
  5. Hans

    Hans Moderator ISPConfig Developer

    xenbackup script

    I've found i nice article and script as well to create backups of xen domains here.

    The final xenbackup script can be downloaded here.

    The final version of the xenbackup has also:
    - Added basic functionality to use LVM snapshots
    - Added NTFS mounting support

    xenbackup has been tested on Debian, with some small modifications, it should work on other Linux distro's as well.

Share This Page