]> git.treefish.org Git - backmeupscotty.git/blob - backmeupscotty
initial commit
[backmeupscotty.git] / backmeupscotty
1 #!/bin/bash
2
3 REMOTE_HOST=localhost
4 REMOTE_BASE=/tmp/backmeupscotty
5 ARCHIVE_KEEPNBACKUPS=30
6 ARCHIVE_KEEPNDAYS=30
7
8 while getopts ":q" opt; do
9     case $opt in
10         q)
11             exec > /dev/null
12             ;;
13     esac
14 done
15
16 function grepbackups {
17     ssh $REMOTE_HOST "ls $REMOTE_BASE/$1" | grep -E '[0-9]+-[0-9]+'
18 }
19
20 function scottysync {
21     timestamp=$(date +%s)
22
23     echo BACKMEUPSCOTTY: Syncing $1 to $REMOTE_HOST:$REMOTE_BASE/$2 @$timestamp
24
25     dir_current=$REMOTE_BASE/$2/current
26     dir_incomplete=$REMOTE_BASE/$2/incomplete
27     dir_timestamped=$REMOTE_BASE/$2/$timestamp-$(date -d @$timestamp +%Y%m%d%H%M%S)
28
29     if [ -z $3 ]; then
30         rsync_exclude=""
31     else
32         rsync_exclude=$(eval echo --exclude={$3} | tr -d {})
33     fi
34
35     echo BACKMEUPSCOTTY: Starting rsync
36     rsync -e ssh \
37         -v -aHAX --numeric-ids --delete --delete-excluded \
38         --link-dest=$dir_current \
39         $rsync_exclude \
40         $1/ $REMOTE_HOST:$dir_incomplete/
41     
42     if [ $? -eq 0 ]; then
43         echo BACKMEUPSCOTTY: Timestamping completed backup and linking to current backup
44         ssh $REMOTE_HOST \
45             "mv $dir_incomplete $dir_timestamped && rm -f $dir_current && ln -s $dir_timestamped $dir_current"
46     fi
47
48     while [ $(grepbackups $2 | wc -l) -gt $ARCHIVE_KEEPNBACKUPS ]; do
49         oldestbackup=$(grepbackups $2 | head -1)
50         oldestbackuptstamp=$(echo $oldestbackup | cut -d'-' -f1)
51
52         if [ $oldestbackuptstamp -lt $(( $(date +%s) - $ARCHIVE_KEEPNDAYS*60*60*24 )) ]; then
53             echo BACKMEUPSCOTTY: Removing old backup $oldestbackup
54             ssh $REMOTE_HOST rm -r "$REMOTE_BASE/$2/$oldestbackup"
55         else
56             break
57         fi
58     done
59 }