Improved duplicity handler: option to disable remote files cleaning, optionnal backup...
[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 exclude
18
19 setsection dest
20 getconf keep 60
21 getconf sshoptions
22 getconf desthost
23 getconf destdir
24 getconf destuser
25 destdir=${destdir%/}
26
27 [ "$destdir" != "" ] || fatal "Destination directory not set"
28 [ "$include" != "" ] || fatal "No source includes specified"
29 [ "$password" != "" ] || fatal "No password specified"
30
31 # see if we can login
32 if [ "$testconnect" == "yes" ]; then
33     debug "ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
34     if [ ! $test ]; then
35         result=`ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1' 2>&1`
36         if [ "$result" != "1" ]; then
37             fatal "Can't connect to $desthost as $destuser."
38         else
39             debug "Connected to $desthost as $destuser successfully"
40         fi
41     fi
42 fi
43
44 ### COMMAND-LINE MANGLING ###
45
46 execstr="$options --no-print-statistics --scp-command 'scp $sshoptions' --ssh-command 'ssh $sshoptions' "
47
48 if [ "$encryptkey" == "" ]; then
49     [ "$sign" != "yes" ] || fatal "encryptkey option must be set when signing."
50 else
51     execstr="${execstr}--encrypt-key $encryptkey "
52     [ "$sign" != "yes" ] || execstr="${execstr}--sign-key $encryptkey "
53 fi
54
55 if [ "$keep" != "yes" ]; then
56     if [ "`echo $keep | tr -d 0-9`" == "" ]; then
57         keep="${keep}D"
58     fi
59     execstr="${execstr}--remove-older-than $keep "
60 fi
61
62 execstr_serverpart="scp://$destuser@$desthost/$destdir"
63 execstr_clientpart="/"
64
65 ### SOURCE ###
66
67 # excludes
68 for i in $exclude; do
69         str="${i//__star__/*}"
70         execstr="${execstr}--exclude $str "
71 done
72         
73 # includes 
74 for i in $include; do
75         str="${i//__star__/*}"
76         execstr="${execstr}--include $str "
77 done
78
79 ### EXECUTE ###
80
81 # exclude everything else, start with root
82 #execstr="${execstr}--exclude '**' / "
83                 
84 # include client-part and server-part
85 #execstr="$execstr $execstr_serverpart"
86
87 execstr=${execstr//\\*/\\\\\\*}
88
89 debug "duplicity $execstr --exclude '**' / $execstr_serverpart"
90 if [ ! $test ]; then
91         output=`nice -n $nicelevel \
92                   su -c \
93                     "export PASSPHRASE=$password \
94                      && duplicity $execstr --exclude '**' / $execstr_serverpart 2>&1"`
95         code=$?
96         if [ "$code" == "0" ]; then
97                 debug $output
98                 info "Duplicity finished successfully."
99         else
100                 warning $output
101                 warning "Duplicity failed."
102         fi
103 fi      
104
105 return 0