lib/vserver.in: new function: vservers_exist
[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 local usevserver=no
38 # If vservers are configured, check that the ones listed in $vsnames do exist.
39 if [ $vservers_are_available = yes ]; then
40    if [ "$vsnames" = all ]; then
41       vsnames="$found_vservers"
42    else
43       if ! vservers_exist "$vsnames" ; then
44             fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
45       fi
46    fi
47    if [ -n "$vsinclude" ]; then
48       info "Using vservers '$vsnames'"
49       usevserver=yes
50    fi
51 else
52    [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
53    [ -z "$vsnames" ] || warning 'vservers support disabled in backupninja.conf, vsnames configuration line will be ignored'   
54 fi
55
56 ### see if we can login ###
57
58 if [ "$testconnect" == "yes" ]; then
59     debug "ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
60     if [ ! $test ]; then
61         result=`ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'`
62         if [ "$result" != "1" ]; then
63             fatal "Can't connect to $desthost as $destuser."
64         else
65             debug "Connected to $desthost as $destuser successfully"
66         fi
67     fi
68 fi
69
70 ### COMMAND-LINE MANGLING ###
71
72 scpoptions="$sshoptions"
73 [ "$bandwidthlimit" == 0 ] || scpoptions="$scpoptions -l $bandwidthlimit"
74
75 execstr="$options --no-print-statistics --scp-command 'scp $scpoptions' --ssh-command 'ssh $sshoptions' "
76
77 # deal with symmetric or asymmetric (public/private key pair) encryption
78 if [ -n "$encryptkey" ]; then
79     execstr="${execstr}--encrypt-key $encryptkey "
80     debug "Data will be encrypted with the GnuPG key $encryptkey."
81 else
82     [ -n "$password" ] || fatal "The password option must be set when using symmetric encryption."
83     debug "Data will be encrypted using symmetric encryption."
84 fi
85
86 # deal with data signing
87 if [ "$sign" == yes ]; then
88     # duplicity is not able to sign data when using symmetric encryption
89     [ -n "$encryptkey" ] || fatal "The encryptkey option must be set when signing."
90     # if needed, initialize signkey to a value that is not empty (checked above)
91     [ -n "$signkey" ] || signkey="$encryptkey"
92     # check password validity
93     [ -n "$password" ] || fatal "The password option must be set when signing."
94     execstr="${execstr}--sign-key $signkey "
95     debug "Data will be signed will the GnuPG key $signkey."
96 else
97     debug "Data won't be signed."
98 fi
99
100 if [ "$keep" != "yes" ]; then
101     if [ "`echo $keep | tr -d 0-9`" == "" ]; then
102         keep="${keep}D"
103     fi
104     execstr="${execstr}--remove-older-than $keep "
105 fi
106
107 if [ "$incremental" == "no" ]; then
108     execstr="${execstr}--full "
109 fi
110
111 execstr_serverpart="scp://$destuser@$desthost/$destdir"
112 execstr_clientpart="/"
113
114 ### SOURCE ###
115
116 # excludes
117 for i in "$exclude"; do
118         str="${i//__star__/*}"
119         execstr="${execstr}--exclude '$str' "
120 done
121         
122 # includes 
123 for i in "$include"; do
124         str="${i//__star__/*}"
125         execstr="${execstr}--include '$str' "
126 done
127
128 # vsincludes
129 if [ $usevserver = yes ]; then
130     for vserver in $vsnames; do
131         for vi in "$vsinclude"; do
132             str="${vi//__star__/*}"
133             execstr="${execstr}--include '$VROOTDIR/$vserver$str' "
134         done
135     done
136 fi
137
138 ### EXECUTE ###
139
140 # exclude everything else, start with root
141 #execstr="${execstr}--exclude '**' / "
142                 
143 # include client-part and server-part
144 #execstr="$execstr $execstr_serverpart"
145
146 execstr=${execstr//\\*/\\\\\\*}
147
148 debug "duplicity $execstr --exclude '**' / $execstr_serverpart"
149 if [ ! $test ]; then
150         export PASSPHRASE=$password
151         output=`nice -n $nicelevel \
152                   su -c \
153                     "duplicity $execstr --exclude '**' / $execstr_serverpart 2>&1"`
154         code=$?
155         if [ $code -eq 0 ]; then
156                 debug $output
157                 info "Duplicity finished successfully."
158         else
159                 debug $output
160                 fatal "Duplicity failed."
161         fi
162 fi      
163
164 return 0