really fix the insecure temp file creation
[matthijs/upstream/backupninja.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 vsinclude
16 getconf exclude
17
18 ### DESTINATION ###
19
20 setsection dest
21 getconf directory; destdir=$directory
22 # strip trailing /
23 destdir=${destdir%/}
24 getconf type; desttype=$type
25 getconf user; destuser=$user
26 getconf host; desthost=$host
27
28 # See if vservers are configured
29 if [ "$vservers" = "yes" ]
30 then
31         if [ ! -d $VROOTDIR ]
32         then
33                 fatal "vservers enabled, but $VROOTDIR does not exist!"
34         else
35                 info "vserver method enabled"
36                 usevserver=1
37         fi
38 fi
39
40 [ "$destdir" != "" ] || fatal "Destination directory not set"
41
42 if [ "$desttype" == "remote" ]; then
43         # see if we can login
44         if [ "$testconnect" == "yes" ]; then
45                 hostalive=0
46             debug "ssh -o PreferredAuthentications=publickey $desthost -l $destuser 'echo -n 1'"
47                 ret=`ssh -o PreferredAuthentications=publickey $desthost -l $destuser 'echo -n host is alive'`
48                 if echo $ret | grep "host is alive"; then
49                         debug "Connected to $desthost as $destuser successfully"
50                 else
51                         fatal "Can't connect to $desthost as $destuser."
52                 fi
53         fi
54         # see that rdiff-backup has the same version as here
55         debug "ssh -o PreferredAuthentications=publickey $desthost -l $destuser '$RDIFFBACKUP -V'\""
56         remoteversion=`ssh -o PreferredAuthentications=publickey $desthost -l $destuser "$RDIFFBACKUP -V | grep rdiff-backup"`
57         localversion=`$RDIFFBACKUP -V`
58         if [ "$remoteversion" != "$localversion" ]; then
59                 fatal "rdiff-backup does not have the same version on this computer and the backup server."
60         fi
61         execstr_serverpart="$destuser@$desthost::$destdir/$label"
62 else
63         execstr_serverpart="$destdir/$label"
64 fi
65
66 ### SOURCE ###
67
68 [ "$sourcetype" == "local" ] || fatal "Only local source type supported"
69 [ "$include" != "" -o "$vsinclude" != "" ] || fatal "No source includes specified"
70 #TODO should I test for vsinclude if usevservers=1?
71
72 execstr_clientpart="/"
73         
74 ## REMOVE OLD BACKUPS
75
76 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
77         keep="${keep}D"
78 fi
79
80 removestr="rdiff-backup --force --remove-older-than $keep "
81 if [ "$desttype" == "remote" ]; then
82         removestr="${removestr}${destuser}@${desthost}::"
83 fi
84 removestr="${removestr}${destdir}/${label}";
85
86 debug "$removestr"
87 if [ ! $test ]; then
88         output=`$removestr 2>&1`
89         code=$?
90         if [ "$code" == "0" ]; then
91                 debug $output
92                 info "Removing backups older than $keep days succeeded."
93         else
94                 warning $output
95                 warning "Failed removing backups older than $keep."
96         fi
97 fi
98
99 ## EXECUTE ##
100
101 execstr="$RDIFFBACKUP $options --print-statistics "
102
103 # TODO: order the includes and excludes
104
105 # excludes
106 for i in $exclude; do
107         str="${i//__star__/*}"
108         execstr="${execstr}--exclude '$str' "
109 done
110         
111 # includes 
112 for i in $include; do
113         str="${i//__star__/*}"
114         execstr="${execstr}--include '$str' "
115 done
116
117 # vsinclude
118 if [ $usevserver ] 
119 then
120         for vserver in `ls $VROOTDIR | grep -E -v "lost+found|ARCHIVES"`
121         do
122                 for vi in $vsinclude
123                 do
124                         str="${vi//__star__/*}"
125                         execstr="${execstr}--include '$VROOTDIR/$vserver$str' "
126                 done
127         done
128 fi
129
130 # exclude everything else
131 execstr="${execstr}--exclude '/*' "
132                 
133 # include client-part and server-part
134 execstr="${execstr}$execstr_clientpart $execstr_serverpart"
135
136 debug "$execstr"
137 if [ ! $test ]; then
138         output=`nice -n $nicelevel su -c "$execstr" 2>&1`
139         code=$?
140         if [ "$code" == "0" ]; then
141                 debug $output
142                 info "Successfully finished backing up source $label"
143         else
144                 warning $output
145                 warning "Failed backup up source $label"
146         fi
147 fi      
148
149 return 0