]> git.treefish.org Git - backmeupscotty.git/commitdiff
initial commit
authorAlexander Schmidt <alex@treefish.org>
Tue, 10 Jun 2014 12:57:09 +0000 (14:57 +0200)
committerAlexander Schmidt <alex@treefish.org>
Tue, 10 Jun 2014 12:57:09 +0000 (14:57 +0200)
backmeupscotty [new file with mode: 0644]
backmeupscotty-example [new file with mode: 0755]

diff --git a/backmeupscotty b/backmeupscotty
new file mode 100644 (file)
index 0000000..965da29
--- /dev/null
@@ -0,0 +1,59 @@
+#!/bin/bash
+
+REMOTE_HOST=localhost
+REMOTE_BASE=/tmp/backmeupscotty
+ARCHIVE_KEEPNBACKUPS=30
+ARCHIVE_KEEPNDAYS=30
+
+while getopts ":q" opt; do
+    case $opt in
+       q)
+           exec > /dev/null
+           ;;
+    esac
+done
+
+function grepbackups {
+    ssh $REMOTE_HOST "ls $REMOTE_BASE/$1" | grep -E '[0-9]+-[0-9]+'
+}
+
+function scottysync {
+    timestamp=$(date +%s)
+
+    echo BACKMEUPSCOTTY: Syncing $1 to $REMOTE_HOST:$REMOTE_BASE/$2 @$timestamp
+
+    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)
+
+    if [ -z $3 ]; then
+       rsync_exclude=""
+    else
+       rsync_exclude=$(eval echo --exclude={$3} | tr -d {})
+    fi
+
+    echo BACKMEUPSCOTTY: Starting rsync
+    rsync -e ssh \
+       -v -aHAX --numeric-ids --delete --delete-excluded \
+       --link-dest=$dir_current \
+       $rsync_exclude \
+       $1/ $REMOTE_HOST:$dir_incomplete/
+    
+    if [ $? -eq 0 ]; then
+       echo BACKMEUPSCOTTY: Timestamping completed backup and linking to current backup
+       ssh $REMOTE_HOST \
+           "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && ln -s $dir_timestamped $dir_current"
+    fi
+
+    while [ $(grepbackups $2 | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
+       oldestbackup=$(grepbackups $2 | head -1)
+       oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)
+
+       if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then
+           echo BACKMEUPSCOTTY: Removing old backup $oldestbackup
+           ssh $REMOTE_HOST rm -r "$REMOTE_BASE/$2/$oldestbackup"
+       else
+           break
+       fi
+    done
+}
diff --git a/backmeupscotty-example b/backmeupscotty-example
new file mode 100755 (executable)
index 0000000..8ac2100
--- /dev/null
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+. ./backmeupscotty
+
+REMOTE_HOST=localhost
+REMOTE_BASE=/home/alex/backtest
+ARCHIVE_KEEPNBACKUPS=30
+ARCHIVE_KEEPNDAYS=1
+
+scottysync /home/alex/backtest/source dest "abab"
+scottysync /home/alex/backtest/source2 dest2 "abab"