dup: now possible to use different keys to sign and encrypt
[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 [ "$password" != "" ] || fatal "No password 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 # if encryptkey is set, add --encrypt-key to the command-line
84 [ -z "$encryptkey" ] || execstr="${execstr}--encrypt-key $encryptkey "
85 # if signkey is not set, set it to encryptkey
86 [ -n "$signkey" ] || signkey="$encryptkey"
87 # if needed, add --sign-key to command-line
88 if [ "$sign" == "yes" ]; then
89     if [ -n "$signkey" ]; then
90         execstr="${execstr}--sign-key $signkey "
91     else
92         fatal "Either encryptkey or signkey option must be set when signing."
93     fi
94 fi
95
96 if [ "$keep" != "yes" ]; then
97     if [ "`echo $keep | tr -d 0-9`" == "" ]; then
98         keep="${keep}D"
99     fi
100     execstr="${execstr}--remove-older-than $keep "
101 fi
102
103 if [ "$incremental" == "no" ]; then
104     execstr="${execstr}--full "
105 fi
106
107 execstr_serverpart="scp://$destuser@$desthost/$destdir"
108 execstr_clientpart="/"
109
110 ### SOURCE ###
111
112 # excludes
113 for i in $exclude; do
114         str="${i//__star__/*}"
115         execstr="${execstr}--exclude '$str' "
116 done
117         
118 # includes 
119 for i in $include; do
120         str="${i//__star__/*}"
121         execstr="${execstr}--include '$str' "
122 done
123
124 # vsincludes
125 if [ $usevserver ]; then
126     for vserver in $vsnames; do
127         for vi in $vsinclude; do
128             str="${vi//__star__/*}"
129             execstr="${execstr}--include '$VROOTDIR/$vserver$str' "
130         done
131     done
132 fi
133
134 ### EXECUTE ###
135
136 # exclude everything else, start with root
137 #execstr="${execstr}--exclude '**' / "
138                 
139 # include client-part and server-part
140 #execstr="$execstr $execstr_serverpart"
141
142 execstr=${execstr//\\*/\\\\\\*}
143
144 debug "duplicity $execstr --exclude '**' / $execstr_serverpart"
145 if [ ! $test ]; then
146         export PASSPHRASE=$password
147         output=`nice -n $nicelevel \
148                   su -c \
149                     "duplicity $execstr --exclude '**' / $execstr_serverpart 2>&1"`
150         code=$?
151         if [ $code -eq 0 ]; then
152                 debug $output
153                 info "Duplicity finished successfully."
154         else
155                 debug $output
156                 fatal "Duplicity failed."
157         fi
158 fi      
159
160 return 0