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