summarylogtreecommitdiffstats
path: root/lvm_create_filesystem_snapshot
blob: 3c919743f7260a75e2ae21da5d45d29be6806cc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/sh

set -e

#For 20GB
#SNAP_SIZE="-L20G"
SNAP_SIZE="-l50%FREE"

mkdir -p /mnt/urbackup_snaps

SNAP_ID=$1
SNAP_MOUNTPOINT="$2"
SNAP_DEST=/mnt/urbackup_snaps/$SNAP_ID

if lsblk -r --output "NAME,MOUNTPOINT" --paths > /dev/null 2>&1
then
	VOLNAME=`lsblk -r --output "NAME,MOUNTPOINT" --paths | egrep " ${SNAP_MOUNTPOINT}\$" | head -n 1 | tr -s " " | cut -d" " -f1`
else
	VOLNAME=`lsblk -r --output "NAME,MOUNTPOINT" | egrep " ${SNAP_MOUNTPOINT}\$" | head -n 1 | tr -s " " | cut -d" " -f1`
	VOLNAME="/dev/mapper/$VOLNAME"
fi

TYPE=`df -T -P | egrep " ${SNAP_MOUNTPOINT}\$" | head -n 1 | tr -s " " | cut -d" " -f2`

if [ "x$VOLNAME" = x ]
then
    echo "Could not find LVM volume for mountpoint ${SNAP_MOUNTPOINT}"
    exit 1
fi

export LVM_SUPPRESS_FD_WARNINGS=1

VGNAME=`lvdisplay "$VOLNAME" | grep "VG Name" | tr -s " " | cut -d" " -f4`
if [ "x$VGNAME" = x ]
then
	echo "Could not find LVM volume group of volume $VOLNAME"
    exit 1
fi

if [ "x$SNAP_ID" = x ]
then
    echo "No snapshot id specified"
    exit 1
fi

lvcreate $SNAP_SIZE -s -n urbackup_snap_$SNAP_ID "$VOLNAME"

mkdir -p /mnt/urbackup_snaps/$SNAP_ID

MOUNTOPTS="ro"

if [ $TYPE = "xfs" ]
then
    MOUNTOPTS="ro,nouuid"
fi

if ! mount -o $MOUNTOPTS /dev/$VGNAME/urbackup_snap_$SNAP_ID /mnt/urbackup_snaps/$SNAP_ID
then
	echo "Mounting filesystem failed"
	rmdir /mnt/urbackup_snaps/$SNAP_ID
	lvremove -f /dev/$VGNAME/urbackup_snap_$SNAP_ID
	exit 1
fi

echo "SNAPSHOT=/mnt/urbackup_snaps/$SNAP_ID"

exit 0