scripts/snapshot-rootfs.sh

37 lines
885 B
Bash
Executable File

#!/usr/bin/bash
# snapshot-rootfs.sh
#
# In your /etc/profile, export the following variables
# LVM_VG_ROOTFS=vg0
# LVM_LG_ROOTFS_NAME=rootfs-arch
# LVM_ROOTFS_SNAPSHOT_SZ=10G
source /etc/profile
crash() {
echo $1;
exit 1;
}
[ \
-n "$LVM_VG_ROOTFS" \
-a -n "$LVM_LG_ROOTFS_NAME" \
-a -n "$LVM_ROOTFS_SNAPSHOT_SZ" \
] || crash "Missing one of the LVM variables for snapshot-rootfs.sh";
DATE=`date +%Y%m%dT%H%M`
LAST_SNAPSHOT_NAME=`lvs --select "origin = $LVM_LG_ROOTFS_NAME" \
--noheadings -o "data_percent,lv_name" 2>&1 \
|sort -n|grep -o '[^[:space:]]\+$'`
echo "The last snapshot's name is : $LAST_SNAPSHOT_NAME. Remove it? (y/n)"
read REMOVE_LAST_SNAPSHOT
[ $REMOVE_LAST_SNAPSHOT = "y" ] && lvremove $LVM_VG_ROOTFS/$LAST_SNAPSHOT_NAME
lvcreate -n $LVM_LG_ROOTFS_NAME-$DATE \
--snapshot \
-L $LVM_ROOTFS_SNAPSHOT_SZ $LVM_VG_ROOTFS/$LVM_LG_ROOTFS_NAME