Changes by docelic.
[clinton/scripts.git] / backup-manager
1 #!/bin/bash
2 #
3 # List and fetch backups.
4
5 CONNSTR="9248@usw-s009.rsync.net"
6 RESTORE_LOC=/vicepa/hcoop-backups/restored
7
8 function get() {
9 if test -n "$3" || test -z "$2"; then
10 echo "Error: Invalid number of arguments for 'get' command."
11 echo
12 usage
13 fi
14 local date=$1
15 local file=$2
16 scp "$CONNSTR:$date/$file" "$RESTORE_LOC/$date-$file"
17 local stat=$?
18 echo
19 if test $stat -eq 0; then
20 echo "Get succeeded. You may now run:"
21 echo " backup-manager restore $date '$file' VOLUME-NAME"
22 echo "to restore the file into an AFS volume."
23 else
24 echo "Error: Get failed with status $stat."
25 exit 1
26 fi
27 }
28
29 function list() {
30 if test -n "$2"; then
31 echo "Error: Invalid number of arguments for 'list' command."
32 echo
33 usage
34 fi
35 local OUT=$(ssh $CONNSTR ls -l -h $@)
36 local view=
37 if test $(<<< "$OUT" wc -l) -ge $(tput lines); then
38 # if output takes up all the lines on the screen, use a pager
39 if test -n "$PAGER"; then
40 view=$PAGER
41 else
42 view=less
43 fi
44 else
45 view=cat
46 fi
47 <<< "$OUT" $view
48 }
49
50 function restore() {
51 if test -n "$4" || test -z "$3"; then
52 echo "Error: Invalid number of arguments for 'get' command."
53 echo
54 usage
55 fi
56 local date=$1
57 local file=$2
58 local volname=$3
59 file="$RESTORE_LOC/$date-$file"
60 if test ! -f "$file"; then
61 echo "Error: File '$file' does not exist."
62 exit 1
63 fi
64 vos examine "$volname" >/dev/null 2>&1
65 local stat=$?
66 if test $stat -eq 0; then
67 echo "Error: Volume '$volname' already exists."
68 exit 1
69 fi
70 < "$file" ccrypt -cdk /etc/backup-encryption-key \
71 | gunzip | vos restore deleuze /vicepa "$volname"
72 stat=$?
73 echo
74 if test $stat -eq 0; then
75 echo "Restore succeeded. You may now run:"
76 echo " fs mkm '/afs/hcoop.net/.old/$volname' '$volname'"
77 echo "to mount the restored AFS volume."
78 else
79 echo "Error: Restore failed with status $stat."
80 exit 1
81 fi
82 }
83
84 function quota() {
85 if test -n "$1"; then
86 echo "Error: Invalid number of arguments for 'quota' command."
87 echo
88 usage
89 fi
90 ssh $CONNSTR quota
91 }
92
93 function usage() {
94 echo "Usage: backup-manager [get|list|quota] [args...]"
95 echo "Commands:"
96 echo " get DATE FILE Fetch a file into $RESTORE_LOC"
97 echo " help Display this message"
98 echo " list [DATE] List dates available, or files with given date"
99 echo " quota Display info about the quota"
100 echo " restore DATE FILE VOLUME-NAME"
101 echo " Take a fetched file (referenced by DATE and" \
102 "FILE) and turn"
103 echo " it into a new AFS volume with the name" \
104 "VOLUME-NAME"
105 exit 1
106 }
107
108 # Dispatch
109 if test -z "$1"; then
110 echo "Error: Incorrect number of arguments."
111 echo
112 usage
113 fi
114
115 cmd=$1
116 shift
117 case $cmd in
118 get) get $@ ;;
119 help) usage ;;
120 list) list $@ ;;
121 quota) quota $@ ;;
122 restore) restore $@ ;;
123 *)
124 echo "Error: Invalid command."
125 echo
126 usage
127 ;;
128 esac