Added vservers support to duplicity handler.
[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
15 setsection source
16 getconf include
17 getconf vsnames all
18 getconf vsinclude
19 getconf exclude
20
21 setsection dest
22 getconf keep 60
23 getconf sshoptions
24 getconf bandwidthlimit 0
25 getconf desthost
26 getconf destdir
27 getconf destuser
28 destdir=${destdir%/}
29
30 [ "$destdir" != "" ] || fatal "Destination directory not set"
31 [ "$include" != "" ] || fatal "No source includes specified"
32 [ "$password" != "" ] || fatal "No password specified"
33
34 ### vservers stuff ###
35
36 # See if vservers are configured.
37 # If so, check that the ones listed in $vsnames do exist.
38 if [ "$vservers" == "yes" ]; then
39     [ -d "$VROOTDIR" ] || fatal "vservers enabled, but $VROOTDIR does not exist!"
40     if [ "$vsnames" == "all" ]; then
41         vsnames=""
42         for vserver in `ls $VROOTDIR | grep -v lost+found | grep -v ARCHIVES`; do
43             vsnames="$vserver $vsnames"
44         done
45     else
46         for vserver in "$vsnames"; do
47             [ -d "$VROOTDIR/$vserver" ] || fatal "vserver '$vserver' does not exist."
48         done
49     fi
50     if [ -n "$vsnames" ]; then
51         if [ -n "$vsinclude" ]; then
52             info "Using vservers '$vsnames'"
53             usevserver=1
54         fi
55     else
56         [ -z "$vsinclude" ] || warning 'vsnames is empty, vsinclude configuration lines will be ignored'
57     fi
58 fi
59
60 ### see if we can login ###
61
62 if [ "$testconnect" == "yes" ]; then
63     debug "ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
64     if [ ! $test ]; then
65         result=`ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1' 2>&1`
66         if [ "$result" != "1" ]; then
67             fatal "Can't connect to $desthost as $destuser."
68         else
69             debug "Connected to $desthost as $destuser successfully"
70         fi
71     fi
72 fi
73
74 ### COMMAND-LINE MANGLING ###
75
76 scpoptions="$sshoptions"
77 [ "$bandwidthlimit" == 0 ] || scpoptions="$scpoptions -l $bandwidthlimit"
78
79 execstr="$options --no-print-statistics --scp-command 'scp $scpoptions' --ssh-command 'ssh $sshoptions' "
80
81 if [ "$encryptkey" == "" ]; then
82     [ "$sign" != "yes" ] || fatal "encryptkey option must be set when signing."
83 else
84     execstr="${execstr}--encrypt-key $encryptkey "
85     [ "$sign" != "yes" ] || execstr="${execstr}--sign-key $encryptkey "
86 fi
87
88 if [ "$keep" != "yes" ]; then
89     if [ "`echo $keep | tr -d 0-9`" == "" ]; then
90         keep="${keep}D"
91     fi
92     execstr="${execstr}--remove-older-than $keep "
93 fi
94
95 execstr_serverpart="scp://$destuser@$desthost/$destdir"
96 execstr_clientpart="/"
97
98 ### SOURCE ###
99
100 # excludes
101 for i in $exclude; do
102         str="${i//__star__/*}"
103         execstr="${execstr}--exclude $str "
104 done
105         
106 # includes 
107 for i in $include; do
108         str="${i//__star__/*}"
109         execstr="${execstr}--include $str "
110 done
111
112 # vsincludes
113 if [ $usevserver ]; then
114     for vserver in $vsnames; do
115         for vi in $vsinclude; do
116             str="${vi//__star__/*}"
117             execstr="${execstr}--include '$VROOTDIR/$vserver$str' "
118         done
119     done
120 fi
121
122 ### EXECUTE ###
123
124 # exclude everything else, start with root
125 #execstr="${execstr}--exclude '**' / "
126                 
127 # include client-part and server-part
128 #execstr="$execstr $execstr_serverpart"
129
130 execstr=${execstr//\\*/\\\\\\*}
131
132 debug "duplicity $execstr --exclude '**' / $execstr_serverpart"
133 if [ ! $test ]; then
134         output=`nice -n $nicelevel \
135                   su -c \
136                     "export PASSPHRASE=$password \
137                      && duplicity $execstr --exclude '**' / $execstr_serverpart 2>&1"`
138         code=$?
139         if [ "$code" == "0" ]; then
140                 debug $output
141                 info "Duplicity finished successfully."
142         else
143                 warning $output
144                 warning "Duplicity failed."
145         fi
146 fi      
147
148 return 0