You are not logged in.

#1 2011-01-21 03:47:07

vendion
Member
From: Tennessee, USA
Registered: 2010-03-10
Posts: 204
Website

Scripting LVM snapshots

I am working on a bash script to automate the task of making LVM snapshots doing a backup then removing the snapshot volume.  The question I have is how can I get around lvremove asking if I'm sure I want to remove the logical volume, it seems that even with -f (force) it still asks.  Here is what I have at the moment

#!/bin/bash 
# Backup the system to /mnt  (Will later handle LVM snapshots) 
 
if [[ $EUID -ne 0 ]]; then 
        echo "You must be root!" 
        exit 1 
fi 
 
now=$(date +%Y%m%d) 
echo "Doing backup..." 
echo "Backing up /dev/system/root" 
lvcreate -L 20G -s -n snap /dev/system/root 
mount /dev/system/snap /mnt/backup 
tar -pczf /backups/root-$now.tar.gz /mnt/backup 
umount /mnt/backup 
lvremove /dev/system/snap 
echo "Backing up /dev/system/home" 
lvcreate -L 20G -s -n snap /dev/system/home 
mount /dev/system/snap /mnt/backup 
tar -pczf /backups/home-$now.tar.gz /mnt/backup 
umount /mnt/backup 
lvremove /dev/system/snap 
echo "Backup Has Finished" 
exit 0

The idea is to have the run as a weekly cronjob

Offline

#2 2011-01-21 10:48:56

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Re: Scripting LVM snapshots

-f works for me. Here's part of my backup script:

echo "Creating LVM snapshot..."
if ! sudo lvcreate --permission r --size 1G --snapshot --name lvm_backup_snapshot /dev/VGSSD1/ArchRoot ; then
    echo "Couldn't create snapshot."
    exit 1
fi
[ -e ${source_dir} ] || sudo mkdir ${source_dir}
if ! sudo mount -o ro /dev/VGSSD1/lvm_backup_snapshot ${source_dir} ; then
    echo "Couldn't mount snapshot."
    sudo lvremove -f /dev/VGSSD1/lvm_backup_snapshot
    exit 1
fi

...

echo "Removing LVM snapshot..."
sleep 3
sudo umount ${source_dir} && sudo rm -r ${source_dir}
sudo lvremove -f /dev/VGSSD1/lvm_backup_snapshot

I don't remember why I put the sleep 3 in there; I think umount was telling me the device was in use otherwise...

Edit: I'm not running this via cron.

Last edited by stqn (2011-01-21 10:50:36)

Offline

#3 2011-01-21 22:59:13

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: Scripting LVM snapshots

Try the 'yes' command (man yes).

yes | lvremove /dev/system/snap

Offline

Board footer

Powered by FluxBB