diff --git a/bare.sh b/bare.sh index a538ff0..8fbe365 100755 --- a/bare.sh +++ b/bare.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash VALIDCOMMANDS="go ls init repos stats diff forget" RCFILE=${HOME}/.backup/config @@ -63,7 +63,11 @@ function usage-repo() { echo " usmb Run 'usmb reponame' to mount \$USMB_PREFIX/ before running." echo " Sharename determined by stripping \$NFS_PREFIX from start of ." echo " localmount Run 'mount reponame' to mount before running." + echo + echo "Pre-backup commands to access repo:" + echo " premount=x Run 'mount ' before command and 'umount x' afterwards." } + function usage-rc() { echo " export B2_ACCOUNT_ID=xxx # Backblaze B2 credentials" echo " export B2_ACCOUNT_KEY=xxx # Backblaze B2 credentials" @@ -231,6 +235,30 @@ function getrepodef() { echo "$repodef" } +function gettagval() { # return 0 if tag matches + local reponame lookfor justtags x t val + + reponame="$1" + lookfor="$2" + + justtags=`getrepotags "$reponame"` + + val="" + IFS=',' read -ra t <<< "$justtags" + for x in ${t[@]}; do + if [[ $x == ${lookfor}=* ]]; then + val=`echo "$x" | cut -d= -f2` + break + fi + done + + if [[ ! -z ${val} ]]; then + echo "$val" + return 0 + fi + return 1 +} + function checktag() { # return 0 if tag matches local reponame lookfor justtags @@ -246,6 +274,65 @@ function checktag() { # return 0 if tag matches return 1 } + +function do_mount() { + local msg + if [[ $USEUSMB -eq 1 ]]; then + [[ $VERBOSE -eq 1 ]] && log "debug: mounting $DATAPATH via smb" + mount | grep -q ${DATAPATH} + if [[ $? -ne 0 ]]; then + # try mounting it. + ${USMB} ${USMBNAME} >/dev/null + if [[ $? -eq 0 ]]; then + log "Mount usmb volume '$USMBNAME': success" + else + log "Mount usmb volume '$USMBNAME': FAILED" + return 1 + fi + fi + elif [[ $USENFS -eq 1 ]]; then + [[ $VERBOSE -eq 1 ]] && log "debug: mounting $DATAPATH via nfs" + mount | grep -q ${DATAPATH} + if [[ $? -ne 0 ]]; then + # try mounting it. + mkdir -p $DATAPATH + mount_nfs -R 1 ${NFSPATH} ${DATAPATH} >/dev/null + if [[ $? -eq 0 ]]; then + log "Mount nfs volume '$NFSPATH' to $DATAPATH: success" + else + log "Mount nfs volume '$NFSPATH' to $DATAPATH: FAILED" + return 1 + fi + fi + elif [[ $USELOCALMOUNT -eq 1 ]]; then + [[ $VERBOSE -eq 1 ]] && log "debug: mounting $DATAPATH via fstab" + mount | grep -q ${DATAPATH} + if [[ $? -ne 0 ]]; then + # try mounting it. + mkdir -p $DATAPATH + mount ${DATAPATH} >/dev/null + if [[ $? -eq 0 ]]; then + log "Mount $DATAPATH: success" + else + log "Mount $DATAPATH: FAILED" + return 1 + fi + fi + fi + + if ! [[ -e ${DATAPATH} ]]; then + log "Error: ${DATAPATH} doesn't exist" + return 1 + fi + + count=`ls ${DATAPATH} | wc -l` + if [ $count -le 2 ]; then + log "Error: ${DATAPATH} exists but appears to be empty" + return 1 + fi + return 0 +} + function do_umount() { if [[ $USEUSMB -eq 1 ]]; then log "Unmounting usmb volume '$USMBNAME'" @@ -331,7 +418,7 @@ fi # Get list of defined repos idx=0 -for f in `cat $REPOFILE`; do +for f in `cat $REPOFILE | grep ";" | grep -v "^#"`; do REPODEFS[$idx]="$f" mode=`getmode "$f"` @@ -403,6 +490,7 @@ GOODREPOS="" for f in $REPOSTOBACKUP; do REPO=${f} DATAPATH=$(getdatapath $f) + REPOPATH=$(getrepopath $f) mode=`getmode "$REPO"` checktag "$f" usmb @@ -443,9 +531,24 @@ for f in $REPOSTOBACKUP; do NFSNAME="" USENFS=0 fi + + path=`gettagval "$f" premount` + if [[ $? -eq 0 ]]; then + grep -q $path /etc/fstab + if [[ $? -ne 0 ]]; then + log "Error: repo '$REPO' has premount tag but $path not in fstab." + continue + fi + + MOUNTREPO=1 + else + MOUNTREPO=0 + fi + checktag "$f" localmount if [[ $? -eq 0 ]]; then - grep -q $DTAPATH /etc/fstab + dir=`echo "$DATAPATH" | sed -e 's,/$,,'` + grep -q $dir /etc/fstab if [[ $? -ne 0 ]]; then log "Error: repo '$REPO' has localmount tag but $DATAPATH not in fstab." continue @@ -469,54 +572,8 @@ for f in $REPOSTOBACKUP; do fi if [[ $NEEDMOUNT -eq 1 ]]; then - if [[ $USEUSMB -eq 1 ]]; then - mount | grep -q ${DATAPATH} - if [[ $? -ne 0 ]]; then - # try mounting it. - ${USMB} ${USMBNAME} >/dev/null - if [[ $? -eq 0 ]]; then - log "Mount usmb volume '$USMBNAME': success" - else - log "Mount usmb volume '$USMBNAME': FAILED" - continue - fi - fi - elif [[ $USENFS -eq 1 ]]; then - mount | grep -q ${DATAPATH} - if [[ $? -ne 0 ]]; then - # try mounting it. - mkdir -p $DATAPATH - mount_nfs -R 1 ${NFSPATH} ${DATAPATH} >/dev/null - if [[ $? -eq 0 ]]; then - log "Mount nfs volume '$NFSPATH' to $DATAPATH: success" - else - log "Mount nfs volume '$NFSPATH' to $DATAPATH: FAILED" - continue - fi - fi - elif [[ $USELOCALMOUNT -eq 1 ]]; then - mount | grep -q ${DATAPATH} - if [[ $? -ne 0 ]]; then - # try mounting it. - mkdir -p $DATAPATH - mount ${DATAPATH} >/dev/null - if [[ $? -eq 0 ]]; then - log "Mount $DATAPATH: success" - else - log "Mount $DATAPATH: FAILED" - continue - fi - fi - fi - - if ! [[ -e ${DATAPATH} ]]; then - log "Error: ${DATAPATH} doesn't exist" - continue - fi - - count=`ls ${DATAPATH} | wc -l` - if [ $count -le 2 ]; then - log "Error: ${DATAPATH} exists but appears to be empty" + do_mount $DATAPATH + if [[ $? -ne 0 ]]; then continue fi fi @@ -590,6 +647,18 @@ for f in $REPOSTOBACKUP; do USINGDEFAULT=1 fi + if [[ $MOUNTREPO -eq 1 ]]; then + path=`gettagval "$f" premount` + [[ $VERBOSE -eq 1 ]] && log "debug: doing repo premount at $path via fstab" + mount $path + if [[ $? -eq 0 ]]; then + log "Pre-mount of '$path': success" + else + log "Pre-mount of '$path': FAILED" + continue + fi + fi + if [[ $CMD == "go" ]]; then NEEDMOUNT=1 elif [[ $CMD == "diff" ]]; then @@ -859,6 +928,15 @@ for f in $REPOSTOBACKUP; do updatestats $f $CMD $rv fi + if [[ $MOUNTREPO -eq 1 ]]; then + path=`gettagval "$f" premount` + umount $path + if [[ $? -eq 0 ]]; then + log "Un-mount of '$path': success" + else + log "Un-mount of '$path': FAILED" + fi + fi if [[ $rv -eq 0 ]]; then if [[ $NEEDMOUNT -eq 1 ]]; then do_umount