Skip to content

Instantly share code, notes, and snippets.

@dedeibel
Created September 18, 2014 08:01
Show Gist options
  • Save dedeibel/a250ac089696b9845e80 to your computer and use it in GitHub Desktop.
Save dedeibel/a250ac089696b9845e80 to your computer and use it in GitHub Desktop.
mount a network share before doing a specified action and umount it afterwards (see also backup and archive)
#!/bin/bash
#set -x
function failure() {
echo "backup failed " `date` >> /var/log/backup-mount-`date +%Y-%m-%d`
exit
}
function kill_childs() {
CPIDS=`pgrep -P $$`
echo "childs running: $CPIDS"
if [ -n "$CPIDS" ]; then
echo "terminating childs"
kill -s TERM ${CPIDS} &> /dev/null
wait ${CPIDS}
fi
}
function exit_trap() {
kill_childs
echo "unounting share"
cd /
sync
sleep 1
umount /backup
}
function prepare() {
echo "preparing backup share"
if (mount | grep '/backup'); then
echo "warn: umounting backup share"
umount /backup
fi
}
function mount_share() {
echo "mounting backup share"
echo "SECRET" | sshfs USER@DUPLICITYHOST:USER /backup -o idmap=user,password_stdin || failure
trap exit_trap EXIT
}
prepare
mount_share
echo "starting action"
$*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment