]> git.treefish.org Git - backmeupscotty.git/blobdiff - backmeupscotty
Abort sync if source directory does not exist or is empty
[backmeupscotty.git] / backmeupscotty
index 965da294be8c489d9617ffba8a388b53586a3cce..e9bab430abb4c03726c44a4338255499d266a000 100644 (file)
@@ -5,6 +5,11 @@ REMOTE_BASE=/tmp/backmeupscotty
 ARCHIVE_KEEPNBACKUPS=30
 ARCHIVE_KEEPNDAYS=30
 
+if [ $(pidof -x $(basename $0) | wc -w) -gt 2 ]; then 
+    echo BACKMEUPSCOTTY: Another instance of $(basename $0) is already running! >&2
+    exit 1
+fi
+
 while getopts ":q" opt; do
     case $opt in
        q)
@@ -22,6 +27,16 @@ function scottysync {
 
     echo BACKMEUPSCOTTY: Syncing $1 to $REMOTE_HOST:$REMOTE_BASE/$2 @$timestamp
 
+    if [ ! -d "$1" ]; then
+       echo BACKMEUPSCOTTY: Source dir $1 does not exist. Not syncing! >&2
+       return 1
+    fi
+
+    if [ $(ls -A "$1" | wc -l) -eq 0 ]; then
+       echo BACKMEUPSCOTTY: Source dir $1 is empty. Not syncing! >&2
+       return 1
+    fi
+
     dir_current=$REMOTE_BASE/$2/current
     dir_incomplete=$REMOTE_BASE/$2/incomplete
     dir_timestamped=$REMOTE_BASE/$2/$timestamp-$(date -d @$timestamp +%Y%m%d%H%M%S)
@@ -32,6 +47,15 @@ function scottysync {
        rsync_exclude=$(eval echo --exclude={$3} | tr -d {})
     fi
 
+    if (ssh $REMOTE_HOST '[ ! -d '$REMOTE_BASE/$2' ]'); then
+       echo BACKMEUPSCOTTY: Creating destination directory $REMOTE_HOST:$REMOTE_BASE/$2
+       ssh $REMOTE_HOST "mkdir $REMOTE_BASE/$2"
+    fi
+
+    if (ssh $REMOTE_HOST '[ -d '$REMOTE_BASE/$2/incomplete' ]'); then
+       echo BACKMEUPSCOTTY: Continuing old incomplete backup >&2
+    fi
+
     echo BACKMEUPSCOTTY: Starting rsync
     rsync -e ssh \
        -v -aHAX --numeric-ids --delete --delete-excluded \