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