#!/bin/sh

#####################################################################
# (c) The Institute for Open Systems Technologies Pty Ltd, 2014
# This script is designed to be a post-exec for a Data Protector filesystem
# object.
# It is designed to tidy up after snapshot-preexec.sh

################################################################

# The parent process' command-line shows which filesystem we are backing up.
# It will look like   hostname:/file/system/path
# We need to find the right line of PS output, get rid of everything up to
# the :/ and get rid of anything afterwards.
REQUESTED_TO_BACKUP=$(ps -fp $PPID | grep vbda | sed -e 's/.*-volume //' -e 's/ .*//')
SNAPSHOT_DEVICE=$(mount | grep "on $REQUESTED_TO_BACKUP type" | awk '{print $1}')

echo Unmounting $REQUESTED_TO_BACKUP
umount $REQUESTED_TO_BACKUP
echo Removing $REQUESTED_TO_BACKUP directory
rmdir --ignore-fail-on-non-empty -p $REQUESTED_TO_BACKUP
echo Removing snapshot
lvremove -f $SNAPSHOT_DEVICE
