Forgot to note in the changelog
[matthijs/upstream/backupninja-vserver.git] / handlers / rdiff
1 #
2 # rdiff-backup handler script for backupninja
3 # requires rdiff-backup
4 #
5
6 getconf options
7 getconf testconnect yes
8 getconf nicelevel 0
9
10 setsection source
11 getconf type; sourcetype=$type
12 getconf label
13 getconf keep 60
14 getconf include
15 getconf exclude
16
17 ### DESTINATION ###
18
19 setsection dest
20 getconf directory; destdir=$directory
21 # strip trailing /
22 destdir=${destdir%/}
23 getconf type; desttype=$type
24 getconf user; destuser=$user
25 getconf host; desthost=$host
26
27 [ "$destdir" != "" ] || fatal "Destination directory not set"
28
29 if [ "$desttype" == "remote" ]; then
30         # see if we can login
31         if [ "$testconnect" == "yes" ]; then
32                 hostalive=0
33             debug "ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
34                 ret=`ssh -o PasswordAuthentication=no $desthost -l $destuser 'echo -n host is alive'`
35                 if echo $ret | grep "host is alive"; then
36                         debug "Connected to $desthost as $destuser successfully"
37                 else
38                         fatal "Can't connect to $desthost as $destuser."
39                 fi
40         fi
41         # see that rdiff-backup has the same version as here
42         debug "ssh $desthost -l $destuser '$RDIFFBACKUP -V'\""
43         remoteversion=`ssh $desthost -l $destuser "$RDIFFBACKUP -V | grep rdiff-backup"`
44         localversion=`$RDIFFBACKUP -V`
45         if [ "$remoteversion" != "$localversion" ]; then
46                 fatal "rdiff-backup does not have the same version on this computer and the backup server."
47         fi
48         execstr_serverpart="$destuser@$desthost::$destdir/$label"
49 else
50         execstr_serverpart="$destdir/$label"
51 fi
52
53 ### SOURCE ###
54
55 [ "$label" != "" ] || fatal "Source missing label"
56 [ "$sourcetype" == "local" ] || fatal "Only local source type supported"
57 [ "$include" != "" ] || fatal "No source includes specified"
58
59 execstr_clientpart="/"
60         
61 ## REMOVE OLD BACKUPS
62
63 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
64         keep="${keep}D"
65 fi
66
67 removestr="rdiff-backup --force --remove-older-than $keep "
68 if [ "$desttype" == "remote" ]; then
69         removestr="${removestr}${destuser}@${desthost}::"
70 fi
71 removestr="${removestr}${destdir}/${label}";
72
73 debug "$removestr"
74 if [ ! $test ]; then
75         output=`$removestr 2>&1`
76         code=$?
77         if [ "$code" == "0" ]; then
78                 debug $output
79                 info "Removing backups older than $keep days succeeded."
80         else
81                 warning $output
82                 warning "Failed removing backups older than $keep."
83         fi
84 fi
85
86 ## EXECUTE ##
87
88 execstr="$RDIFFBACKUP $options --print-statistics "
89
90 # TODO: order the includes and excludes
91
92 # excludes
93 for i in $exclude; do
94         str="${i//__star__/*}"
95         execstr="${execstr}--exclude '$str' "
96 done
97         
98 # includes 
99 for i in $include; do
100         str="${i//__star__/*}"
101         execstr="${execstr}--include '$str' "
102 done
103
104 # exclude everything else
105 execstr="${execstr}--exclude '/*' "
106                 
107 # include client-part and server-part
108 execstr="${execstr}$execstr_clientpart $execstr_serverpart"
109
110 debug "$execstr"
111 if [ ! $test ]; then
112         output=`nice -n $nicelevel su -c "$execstr" 2>&1`
113         code=$?
114         if [ "$code" == "0" ]; then
115                 debug $output
116                 info "Successfully finished backing up source '$label'"
117         else
118                 warning $output
119                 warning "Failed backup up source '$label'"
120         fi
121 fi      
122
123 return 0