Initial version of backup-manager.
[clinton/scripts.git] / backup-manager
CommitLineData
719a1db9 1#!/bin/bash
2#
3# List and fetch backups.
4
5CONNSTR="9248@usw-s009.rsync.net"
6RESTORE_LOC=/vicepa/hcoop-backups/restored
7
8function 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
29function 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
50function 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
84function 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
93function 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 " list [DATE] List dates available, or files with given date"
98 echo " quota Display info about the quota"
99 echo " restore DATE FILE VOLUME-NAME"
100 echo " Take a fetched file (referenced by DATE and" \
101 "FILE) and turn"
102 echo " it into a new AFS volume with the name" \
103 "VOLUME-NAME"
104 exit 1
105}
106
107# Dispatch
108if test -z "$1"; then
109 echo "Error: Incorrect number of arguments."
110 echo
111 usage
112fi
113
114cmd=$1
115shift
116case $cmd in
117 get) get $@ ;;
118 list) list $@ ;;
119 quota) quota $@ ;;
120 restore) restore $@ ;;
121 *)
122 echo "Error: Invalid command."
123 echo
124 usage
125 ;;
126esac