.
[rbackup.git] / rbackup
blob3aec613e57fdb9a4b0670ae2870fb4338433cf97
1 #!/bin/sh
2 . /opt/corvix/etc/rbackup.conf
3 [[ -e $HOME/.rbackup.conf ]] && . $HOME/.rbackup.conf
5 while getopts "l" opt; do
6 case $opt in
7 l) OPTS="--bwlimit=66";;
8 esac
9 done
10 shift $(($OPTIND -1))
12 ERUNNING=4
14 NICE="nice -n 19 "
15 RM="$NICE rm"
16 MV="$NICE mv"
17 RSYNC="$NICE /usr/bin/rsync"
18 CP="$NICE cp"
19 SSH="ssh"
20 LOGFILE=/opt/corvix/var/log/rbackup
21 RUNFILE=/opt/corvix/var/run/rbackup
23 _set_running() {
24 echo $TASK > $RUNFILE
27 _unset_running() {
28 rm -f $RUNFILE
31 _is_running() {
32 test -e $RUNFILE
35 _log() {
36 echo $@
37 echo $@ >> $LOGFILE
40 _rotate() {
41 name=$1
42 interval=$2
43 path=$3
44 host=$4
45 _log "Rotate $name in $path on $host:"
46 $SSH $host "[[ -d $path ]] || mkdir -p $path"
47 if $SSH $host "[[ -d $path/$name.$interval ]]" ; then
48 _log "Delete expired snapshot $interval on $host"
49 $SSH $host "$RM -rfv $path/$name.$interval" 2>&1 1>>$LOGFILE
51 for i in $(seq $(($interval-1)) -1 0); do
52 $SSH $host "[[ -d $path/$name.$i ]]" || continue;
53 _log "Rotate snapshot $i on $host"
54 $SSH $host "$MV -v $path/$name.$i $path/$name.\$(($i+1))" 2>&1 1>>$LOGFILE
55 done
58 _backup() {
59 source=$1
60 name=$2
61 target=$3
62 host=$4
63 _log "Backup $name to $target on $host:"
64 $RSYNC -v -a --delete-excluded --exclude-from=$HOME/.rbackup.exclude --delete $OPTS --link-dest=../$name.1 $source/ $host:$target/$name.0/ 2>&1 1>>$LOGFILE
67 TASK=$1
68 case $TASK in
69 hourly) INTERVAL=$HOURLY_INTERVAL;;
70 daily) INTERVAL=$DAILY_INTERVAL;;
71 weekly) INTERVAL=$WEEKLY_INTERVAL;;
72 monthly) INTERVAL=$MONTHLY_INTERVAL;;
73 *) exit 0 ;;
74 esac
76 _is_running && exit $ERUNNING
77 _set_running
79 _rotate $TASK $INTERVAL $TARGET/$SOURCE $HOST
80 _backup $SOURCE $TASK $TARGET/$SOURCE $HOST
82 _unset_running