Fixed the include=/exclude= improper dereference problem in the dup handler
[matthijs/upstream/backupninja.git] / handlers / dup
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 #
3 # duplicity script for backupninja
4 # requires duplicity
5 #
6
7 getconf options
8 getconf testconnect yes
9 getconf nicelevel 0
10
11 setsection gpg
12 getconf password
13 getconf sign no
14 getconf encryptkey
15 getconf signkey
16
17 setsection source
18 getconf include
19 getconf vsnames all
20 getconf vsinclude
21 getconf exclude
22
23 setsection dest
24 getconf incremental yes
25 getconf keep 60
26 getconf sshoptions
27 getconf bandwidthlimit 0
28 getconf desthost
29 getconf destdir
30 getconf destuser
31 destdir=${destdir%/}
32
33 [ "$destdir" != "" ] || fatal "Destination directory not set"
34 [ "$include" != "" ] || fatal "No source includes specified"
35
36 ### vservers stuff ###
37
38 # If vservers are configured, check that the ones listed in $vsnames do exist.
39 local usevserver=no
40 if [ $vservers_are_available = yes ]; then
41    if [ "$vsnames" = all ]; then
42       vsnames="$found_vservers"
43    else
44       if ! vservers_exist "$vsnames" ; then
45             fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
46       fi
47    fi
48    if [ -n "$vsinclude" ]; then
49       info "Using vservers '$vsnames'"
50       usevserver=yes
51    fi
52 else
53    [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
54    [ -z "$vsnames" ] || warning 'vservers support disabled in backupninja.conf, vsnames configuration line will be ignored'   
55 fi
56
57 ### see if we can login ###
58
59 if [ "$testconnect" == "yes" ]; then
60     debug "ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
61     if [ ! $test ]; then
62         result=`ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'`
63         if [ "$result" != "1" ]; then
64             fatal "Can't connect to $desthost as $destuser."
65         else
66             debug "Connected to $desthost as $destuser successfully"
67         fi
68     fi
69 fi
70
71 ### COMMAND-LINE MANGLING ###
72
73 scpoptions="$sshoptions"
74 [ "$bandwidthlimit" == 0 ] || scpoptions="$scpoptions -l $bandwidthlimit"
75
76 execstr="$options --no-print-statistics --scp-command 'scp $scpoptions' --ssh-command 'ssh $sshoptions' "
77
78 # deal with symmetric or asymmetric (public/private key pair) encryption
79 if [ -n "$encryptkey" ]; then
80     execstr="${execstr}--encrypt-key $encryptkey "
81     debug "Data will be encrypted with the GnuPG key $encryptkey."
82 else
83     [ -n "$password" ] || fatal "The password option must be set when using symmetric encryption."
84     debug "Data will be encrypted using symmetric encryption."
85 fi
86
87 # deal with data signing
88 if [ "$sign" == yes ]; then
89     # duplicity is not able to sign data when using symmetric encryption
90     [ -n "$encryptkey" ] || fatal "The encryptkey option must be set when signing."
91     # if needed, initialize signkey to a value that is not empty (checked above)
92     [ -n "$signkey" ] || signkey="$encryptkey"
93     # check password validity
94     [ -n "$password" ] || fatal "The password option must be set when signing."
95     execstr="${execstr}--sign-key $signkey "
96     debug "Data will be signed will the GnuPG key $signkey."
97 else
98     debug "Data won't be signed."
99 fi
100
101 if [ "$keep" != "yes" ]; then
102     if [ "`echo $keep | tr -d 0-9`" == "" ]; then
103         keep="${keep}D"
104     fi
105     execstr="${execstr}--remove-older-than $keep "
106 fi
107
108 if [ "$incremental" == "no" ]; then
109     execstr="${execstr}--full "
110 fi
111
112 execstr_serverpart="scp://$destuser@$desthost/$destdir"
113 execstr_clientpart="/"
114
115 ### SOURCE ###
116
117 set -o noglob
118
119 # excludes
120 for i in $exclude; do
121         i=`readlink -f $i`
122         str="${i//__star__/*}"
123         execstr="${execstr}--exclude '$str' "
124 done
125         
126 # includes 
127 for i in $include; do
128         i=`readlink -f $i`
129         str="${i//__star__/*}"
130         execstr="${execstr}--include '$str' "
131 done
132
133 # vsincludes
134 if [ $usevserver = yes ]; then
135     for vserver in $vsnames; do
136         for vi in $vsinclude; do
137             i=`readlink -f $VROOTDIR/$vserver$vi`
138             str="${i//__star__/*}"
139             execstr="${execstr}--include '$VROOTDIR/$vserver$str' "
140         done
141     done
142 fi
143
144 set +o noglob
145
146 ### EXECUTE ###
147
148 # exclude everything else, start with root
149 #execstr="${execstr}--exclude '**' / "
150                 
151 # include client-part and server-part
152 #execstr="$execstr $execstr_serverpart"
153
154 execstr=${execstr//\\*/\\\\\\*}
155
156 debug "duplicity $execstr --exclude '**' / $execstr_serverpart"
157 if [ ! $test ]; then
158         export PASSPHRASE=$password
159         output=`nice -n $nicelevel \
160                   su -c \
161                     "duplicity $execstr --exclude '**' / $execstr_serverpart 2>&1"`
162         code=$?
163         if [ $code -eq 0 ]; then
164                 debug $output
165                 info "Duplicity finished successfully."
166         else
167                 debug $output
168                 fatal "Duplicity failed."
169         fi
170 fi      
171
172 return 0