HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   HOWTO-Related Questions (http://www.howtoforge.com/forums/forumdisplay.php?f=2)
-   -   Scripting LVM Snapshot Backups (http://www.howtoforge.com/forums/showthread.php?t=31919)

atjensen11 28th February 2009 19:59

Scripting LVM Snapshot Backups
 
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?

falko 1st March 2009 14:40

Code:

lvremove -f /dev/server1/rootsnapshot
(see man lvremove)

atjensen11 7th March 2009 18:47

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.

Thanks.

atjensen11 8th March 2009 01:55

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

Quote:

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

Code:

# Query size of Logical Volume
LVM_SIZE="`lvdisplay -c $LVM_NAME_PATH | cut -d':' -f7`K"

# Create Snapshot Logical Volume
lvcreate -L $LVM_SIZE -s -n $LVM_SNAPSHOT_NAME $LVM_NAME_PATH

Quote:

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

Code:

# 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
fi


Hans 12th April 2009 13:44

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.


All times are GMT +2. The time now is 11:51.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.