4db412ef3479230c592226b04c1210c7408b6b86
[distro-setup] / disabled / small-backup
1 #!/bin/bash
2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
7 # its license to GPL.
8
9 # Copyright 2024 Ian Kelling
10
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14
15 # http://www.apache.org/licenses/LICENSE-2.0
16
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22
23
24 # for duplicity source build
25 PATH="$PATH:/usr/local/bin"
26
27 # [--retry] interval_name [max_age]
28 # takes 2 arguments specifying the name of the subfolder,
29 # and optionally, the max age of the backup
30
31 # this script setup by adding a user crontab. see t.org for the script
32 # it's also setup to email me only when it fails, and only for daily or weekly runs
33
34 # uncomment for debugging, prints all commands to stdout
35 #set -x
36
37 set -E
38 #trap 'echo trapped error from \"$BASH_COMMAND\" returned $? line $LINENO; accumulated_errors=true' ERR
39 trap 'echo trapped err: $?; accumulated_errors=true' ERR
40
41 exec 3>&1 4>&2
42 exec &>> /tmp/small-backup.log
43
44 echo "BEGIN: $(date): args $*"
45
46
47 # only works with a single letter, ie 2D, not 2D12h
48 half-time() {
49 local time_word
50 local letter=${1##*[0-9]}
51 case $letter in
52 s) time_wrod=second ;;
53 m) time_word=minute ;;
54 h) time_word=hour ;;
55 D) time_word=day ;;
56 W) time_word=week ;;
57 M) time_word=month ;;
58 Y) time_word=year ;;
59 esac
60 echo "${1%%$letter} $time_word"
61 local x=$(date +%s -d "${1%%$letter} $time_word")
62 local y=$(date +%s)
63 }
64
65
66 if [[ $1 == --retry ]]; then
67 shift
68 x=0
69 while pid=( $(pidof -o %PPID -x ${0##*/}) ) && (( ${#pid[@]} > 1 )) && (( x < 20 )); do
70 x=$(( x + 1 ))
71 sleep 30
72 done
73 if [[ $x == 20 ]]; then
74 ps -F ${pid[@]}
75 echo timeout error: existing ${0##*/} running for over 5 minutes >&2
76 exit 1
77 fi
78 else
79 if pid=( $(pidof -o %PPID -x ${0##*/}) ) && (( ${#pid[@]} > 1 )); then
80 echo ps -F ${pid[@]}
81 ps -F ${pid[@]}
82 echo error: existing ${0##*/} running >&2
83 exit 1
84 fi
85 fi
86
87 interval=$1
88 max_age=$2
89 full_backup_arg=""
90 if [[ $max_age ]]; then
91 full_backup_arg="--full-if-older-than $(half-time $max_age)"
92 fi
93
94 rbackup () {
95
96 local d=$1
97 shift
98 local dest=root@li::/root/rdiff-backups/${d##*/}/${interval}
99
100 c="rdiff-backup $* --create-full-path $d $dest"
101 echo "$c"; $c
102
103 if [[ $max_age ]]; then
104 c="rdiff-backup --force --remove-older-than $max_age $dest"
105 echo "$c"; $c
106
107 fi
108 }
109
110
111 rbackup /a/bin --exclude /a/bin/fai-basefiles
112 rbackup /a/c
113
114 # this is populated after input_setup.sh is run on login
115
116 ssh root@li mkdir -p /root/duplicity-backups/p/$interval
117 source /p/duplicity/gpg_agent_env
118 duplicity_dest=rsync://root@li//root/duplicity-backups/p/$interval
119
120 x=(/p/*)
121 if ((${#x[@]} > 1)); then
122 set -x
123 # archive-dir is sort of a persistent cache
124 duplicity --use-agent \
125 --encrypt-sign-key E969C67B \
126 --include-globbing-filelist /p/duplicity/filelist \
127 --archive-dir /p/duplicity/archive \
128 --tempdir /p/tmp \
129 $full_backup_arg /p $duplicity_dest
130 if [[ $max_age ]]; then
131 duplicity --use-agent \
132 remove-all-but-n-full 2 --force $duplicity_dest
133 fi
134 set +x
135 fi
136 # example restore command. We only need to make the first argument be a url for it to know it to do restore
137 # the archive-dir and tempdir args are not needed
138 # 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
139
140
141 echo END
142
143 # to restore duplicity. see man for additional options
144 # duplicity --use-agent restore ...
145 if [[ $accumulated_error ]]; then
146 eccho "tail -n 50 of /tmp/small-backup.log:"
147 tail -n 50 /tmp/small-backup.log
148 exit 1
149 fi