Bug fix; the "month of the key" is 6 characters from index 0
[rrq/buckap.git] / restore.sh
1 #!/bin/bash
2 #
3 # Support script for restoring individual directories and files
4 #
5
6 CONFIG=/etc/duplicity-daily.conf
7 eval $(grep ^TARGET= $CONFIG)
8 eval $(grep ^OPTIONS= $CONFIG)
9 : ${RESTORE:=/restore}
10
11 SRCS=( $(grep ^/ $CONFIG) )
12 if [ -z "$SRCS" ] ; then
13     cat <<EOF
14 ** There are no backup sources.
15 ** Check /etc/duplicity-daily.conf
16 EOF
17     exit 1
18 fi
19 echo "= Select backup set ==="
20 select SRC in ${SRCS[@]} ; do [ -n "$SRC" ] && break ; done
21
22 BASE="${SRC%=*}"
23 if [ "$BASE" = "$SRC" ] ; then
24     URI="$TARGET$SRC"
25 else
26     URI="${SRC#*=}"
27 fi
28 REMOTE="${URI%%/*}"
29 STORE="${URI#*/}"
30
31 ## Check for existing backups
32 TIMES=( $(ssh $REMOTE ls $STORE |grep sigtar |awk -F. '{ print $(NF-2); }') )
33 if [ -z "$TIMES" ] ; then
34     cat <<EOF
35 ** No backups found for $SRC
36 EOF
37     exit 0
38 fi
39 echo "= Select backup timestamp ==="
40 select TIME in ${TIMES[@]} ; do [ -n "$TIME" ] && break ; done
41
42 LIST=/tmp/restore-$$
43 cleanup() {
44     rm -f $LIST 
45 }
46
47 duplicity list-current-files $OPTIONS --time $TIME pexpect+scp://$URI |\
48     sed '1d;2d;3d;s/.\{25\}//' > $LIST
49
50 echo 
51
52 # Restore pathname $1
53 restore() {
54     local X
55     cat <<EOF
56 Pathname   = $1
57 Restore at = $RESTORE/$TIME$BASE/$1
58 Souce      = $URI
59 Options    = $OPTIONS
60 EOF
61     read -p "Go ahead [Yn]?" X
62     [ "$X" = n ] && return 0
63     mkdir -p "$RESTORE/$TIME$BASE" || return 1
64     duplicity restore $OPTIONS --file-to-restore "$1" --time "$TIME" \
65               "pexpect+scp://$URI" "$RESTORE/$TIME$BASE/$1"
66 }
67
68 # $1 root
69 browse() {
70     local X 
71     while true ; do
72         X="$(grep -oE "^$1[^/]*/?" < $LIST | uniq | iselect -fa)"
73         [ -z "$X" ] && break
74         if [ -z "${X##*/}" ] ; then
75             browse "$X"
76         else
77             restore "$X" || read -p "push enter" X
78         fi
79     done
80 }
81
82 browse
83 cleanup