fix mistake from recent commit
[distro-setup] / small-backup
1 #!/bin/bash
2
3 # for duplicity source build
4 PATH="$PATH:/usr/local/bin"
5
6 # [--retry] interval_name [max_age]
7 # takes 2 arguments specifying the name of the subfolder,
8 # and optionally, the max age of the backup
9
10 # this script setup by adding a user crontab. see t.org for the script
11 # it's also setup to email me only when it fails, and only for daily or weekly runs
12
13 # uncomment for debugging, prints all commands to stdout
14 #set -x
15
16 set -E
17 #trap 'echo trapped error from \"$BASH_COMMAND\" returned $? line $LINENO; accumulated_errors=true' ERR
18 trap 'echo trapped err: $?; accumulated_errors=true' ERR
19
20 exec 3>&1 4>&2
21 exec &>> /tmp/small-backup.log
22
23 echo "BEGIN: $(date): args $*"
24
25
26 # only works with a single letter, ie 2D, not 2D12h
27 half-time() {
28 local time_word
29 local letter=${1##*[0-9]}
30 case $letter in
31 s) time_wrod=second ;;
32 m) time_word=minute ;;
33 h) time_word=hour ;;
34 D) time_word=day ;;
35 W) time_word=week ;;
36 M) time_word=month ;;
37 Y) time_word=year ;;
38 esac
39 echo "${1%%$letter} $time_word"
40 local x=$(date +%s -d "${1%%$letter} $time_word")
41 local y=$(date +%s)
42 }
43
44
45 if [[ $1 == --retry ]]; then
46 shift
47 x=0
48 while pid=( $(pidof -o %PPID -x ${0##*/}) ) && (( ${#pid[@]} > 1 )) && (( x < 20 )); do
49 x=$(( x + 1 ))
50 sleep 30
51 done
52 if [[ $x == 20 ]]; then
53 ps -F ${pid[@]}
54 echo timeout error: existing ${0##*/} running for over 5 minutes >&2
55 exit 1
56 fi
57 else
58 if pid=( $(pidof -o %PPID -x ${0##*/}) ) && (( ${#pid[@]} > 1 )); then
59 echo ps -F ${pid[@]}
60 ps -F ${pid[@]}
61 echo error: existing ${0##*/} running >&2
62 exit 1
63 fi
64 fi
65
66 interval=$1
67 max_age=$2
68 full_backup_arg=""
69 if [[ $max_age ]]; then
70 full_backup_arg="--full-if-older-than $(half-time $max_age)"
71 fi
72
73 rbackup () {
74
75 local d=$1
76 shift
77 local dest=root@li::/root/rdiff-backups/${d##*/}/${interval}
78
79 c="rdiff-backup $* --create-full-path $d $dest"
80 echo "$c"; $c
81
82 if [[ $max_age ]]; then
83 c="rdiff-backup --force --remove-older-than $max_age $dest"
84 echo "$c"; $c
85
86 fi
87 }
88
89
90 rbackup /a/bin --exclude /a/bin/fai-basefiles
91 rbackup /a/c
92
93 # this is populated after input_setup.sh is run on login
94
95 ssh root@li mkdir -p /root/duplicity-backups/p/$interval
96 source /p/duplicity/gpg_agent_env
97 duplicity_dest=rsync://root@li//root/duplicity-backups/p/$interval
98
99 x=(/p/*)
100 if ((${#x[@]} > 1)); then
101 set -x
102 # archive-dir is sort of a persistent cache
103 duplicity --use-agent \
104 --encrypt-sign-key E969C67B \
105 --include-globbing-filelist /p/duplicity/filelist \
106 --archive-dir /p/duplicity/archive \
107 --tempdir /p/tmp \
108 $full_backup_arg /p $duplicity_dest
109 if [[ $max_age ]]; then
110 duplicity --use-agent \
111 remove-all-but-n-full 2 --force $duplicity_dest
112 fi
113 set +x
114 fi
115 # example restore command. We only need to make the first argument be a url for it to know it to do restore
116 # the archive-dir and tempdir args are not needed
117 # duplicity --use-agent --encrypt-sign-key E969C67B --archive-dir /p/duplicity/archive --tempdir /p/tmp ssh://root@li//root/duplicity-backups/p/weekly /p/duptest
118
119
120 echo END
121
122 # to restore duplicity. see man for additional options
123 # duplicity --use-agent restore ...
124 if [[ $accumulated_error ]]; then
125 eccho "tail -n 50 of /tmp/small-backup.log:"
126 tail -n 50 /tmp/small-backup.log
127 exit 1
128 fi