dup: new tmpdir config option, useful when duplicity fills up /tmp
[matthijs/upstream/backupninja.git] / handlers / dup.in
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 getconf tmpdir
11
12 setsection gpg
13 getconf password
14 getconf sign no
15 getconf encryptkey
16 getconf signkey
17
18 setsection source
19 getconf include
20 getconf vsnames all
21 getconf vsinclude
22 getconf exclude
23
24 setsection dest
25 getconf incremental yes
26 getconf keep 60
27 getconf sshoptions
28 getconf bandwidthlimit 0
29 getconf desthost
30 getconf destdir
31 getconf destuser
32 destdir=${destdir%/}
33
34 [ "$destdir" != "" ] || fatal "Destination directory not set"
35 [ "$include" != "" ] || fatal "No source includes specified"
36
37 ### vservers stuff ###
38
39 # If vservers are configured, check that the ones listed in $vsnames do exist.
40 local usevserver=no
41 if [ $vservers_are_available = yes ]; then
42    if [ "$vsnames" = all ]; then
43       vsnames="$found_vservers"
44    else
45       if ! vservers_exist "$vsnames" ; then
46             fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
47       fi
48    fi
49    if [ -n "$vsinclude" ]; then
50       info "Using vservers '$vsnames'"
51       usevserver=yes
52    fi
53 else
54    [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
55 fi
56
57
58 ### see if we can login ###
59
60 if [ "$testconnect" == "yes" ]; then
61     debug "ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
62     if [ ! $test ]; then
63         result=`ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'`
64         if [ "$result" != "1" ]; then
65             fatal "Can't connect to $desthost as $destuser."
66         else
67             debug "Connected to $desthost as $destuser successfully"
68         fi
69     fi
70 fi
71
72 ### COMMAND-LINE MANGLING ###
73
74 duplicity_version="`duplicity --version | @AWK@ '{print $2}'`"
75 duplicity_major="`echo $duplicity_version | @AWK@ -F '.' '{print $1}'`"
76 duplicity_minor="`echo $duplicity_version | @AWK@ -F '.' '{print $2}'`"
77 duplicity_sub="`echo $duplicity_version | @AWK@ -F '.' '{print $3}'`"
78
79 # 1. duplicity >= 0.4.2 needs --sftp-command (NB: sftp does not support the -l option)
80 # 2. duplicity >= 0.4.3 replaces --ssh-command with --ssh-options, which:
81 #      - is passed to scp and sftp commands by duplicity
82 #      - has a special syntax we can not feed the command line with
83 #    so we don't use it: since this version does not use the ssh command anymore,
84 #    we keep compatibility with our previous config files by passing $sshoptions to
85 #    --scp-command and --sftp-command ourselves
86
87 scpoptions="$sshoptions"
88 [ "$bandwidthlimit" == 0 ] || scpoptions="$scpoptions -l $bandwidthlimit"
89
90 execstr="$options --no-print-statistics "
91
92 # < 0.4.2 : only uses ssh and scp
93 if [ "$duplicity_major" -le 0 -a "$duplicity_minor" -le 4 -a "$duplicity_sub" -lt 2 ]; then
94    execstr="$execstr --scp-command 'scp $scpoptions' --ssh-command 'ssh $sshoptions' "
95 # >= 0.4.2 : also uses sftp, --sftp-command option is now supported
96 else
97    sftpoptions="$sshoptions"
98    # == 0.4.2 : uses ssh, scp and sftp
99    if [ "$duplicity_major" -eq 0 -a "$duplicity_minor" -eq 4 -a "$duplicity_sub" -eq 2 ]; then
100       execstr="$execstr --scp-command 'scp $scpoptions' --sftp-command 'sftp $sftpoptions' --ssh-command 'ssh $sshoptions' "
101    # >= 0.4.3 : uses only scp and sftp, --ssh-command option is not supported anymore
102    else
103       execstr="$execstr --scp-command 'scp $scpoptions' --sftp-command 'sftp $sftpoptions' "
104    fi
105 fi
106
107 # deal with symmetric or asymmetric (public/private key pair) encryption
108 if [ -n "$encryptkey" ]; then
109     execstr="${execstr}--encrypt-key $encryptkey "
110     debug "Data will be encrypted with the GnuPG key $encryptkey."
111 else
112     debug "Data will be encrypted using symmetric encryption."
113 fi
114
115 # deal with data signing
116 if [ "$sign" == yes ]; then
117     # duplicity is not able to sign data when using symmetric encryption
118     [ -n "$encryptkey" ] || fatal "The encryptkey option must be set when signing."
119     # if needed, initialize signkey to a value that is not empty (checked above)
120     [ -n "$signkey" ] || signkey="$encryptkey"
121     execstr="${execstr}--sign-key $signkey "
122     debug "Data will be signed will the GnuPG key $signkey."
123 else
124     debug "Data won't be signed."
125 fi
126
127 # deal with GnuPG passphrase
128 [ -n "$password" ] || fatal "The password option must be set."
129
130 if [ "$keep" != "yes" ]; then
131     if [ "`echo $keep | tr -d 0-9`" == "" ]; then
132         keep="${keep}D"
133     fi
134     execstr="${execstr}--remove-older-than $keep "
135 fi
136
137 if [ "$incremental" == "no" ]; then
138     execstr="${execstr}--full "
139 fi
140
141 execstr_serverpart="scp://$destuser@$desthost/$destdir"
142 execstr_clientpart="/"
143
144 ### SOURCE ###
145
146 set -o noglob
147
148 # excludes
149 for i in $exclude; do
150    str="${i//__star__/*}"
151    execstr="${execstr}--exclude '$str' "
152 done
153         
154 # includes 
155 for i in $include; do
156    [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
157    str="${i//__star__/*}"
158    execstr="${execstr}--include '$str' "
159 done
160
161 # vsincludes
162 if [ $usevserver = yes ]; then
163    for vserver in $vsnames; do
164       for vi in $vsinclude; do
165          str="${vi//__star__/*}"
166          str="$VROOTDIR/$vserver$str"
167          execstr="${execstr}--include '$str' "
168       done
169    done
170 fi
171
172 set +o noglob
173
174 ### deal with tmpdir ###
175 precmd=
176 if [ -n "$tmpdir" ]; then
177    if [ ! -d "$tmpdir" ]; then
178       info "Temporary directory ($tmpdir) does not exist, creating it."
179       mkdir -p "$tmpdir"
180       [ $? -eq 0 ] || fatal "Could not create temporary directory ($tmpdir)."
181    fi
182    info "Using $tmpdir as TMPDIR"
183    precmd="${precmd}TMPDIR=$tmpdir "
184 fi
185
186 ### EXECUTE ###
187
188 execstr=${execstr//\\*/\\\\\\*}
189
190 debug "$precmd duplicity $execstr --exclude '**' / $execstr_serverpart"
191 if [ ! $test ]; then
192         export PASSPHRASE=$password
193         output=`nice -n $nicelevel \
194                   su -c \
195                     "$precmd duplicity $execstr --exclude '**' / $execstr_serverpart 2>&1"`
196         code=$?
197         if [ $code -eq 0 ]; then
198                 debug $output
199                 info "Duplicity finished successfully."
200         else
201                 debug $output
202                 fatal "Duplicity failed."
203         fi
204 fi      
205
206 return 0