-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathestimate.sh
215 lines (185 loc) · 4.3 KB
/
estimate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#
# Functions for pov-simple-backup
#
#
# Helpers
#
test -n "$verbose" || verbose=0
exec 3>&1
DATE_GLOB="[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"
info() {
if [ $verbose -ne 0 ]; then
echo "$@"
fi
}
error() {
echo "$@" 1>&2
}
backupdir() {
local suffix=${1:-$BACKUP_SUFFIX}
local dir=$BACKUP_ROOT/$DATE$suffix
echo "$dir"
}
# keep this in sync with the slugify() from functions.sh
slugify() {
printf '%s\n' "$1" | sed -e 's,^/\+,,' -e 's,/\+$,,' -e 's,/\+,-,g' -e 's,^$,root,'
}
size_of() {
local pathname=$1
test -e "$pathname" || return
du -s "$pathname" | awk '{print $1}'
}
size_of_last_backup() {
local where=$1
local suffix=$2
test -d "$where" || return
local last_backup
# shellcheck disable=SC2086,SC2012
last_backup=$(ls -d "${where%/}"/$DATE_GLOB"$suffix" 2>/dev/null | tail -n 1)
test -n "$last_backup" || return
size_of "$last_backup"
}
pretty_size() {
local size=$1
if [ "$size" -lt 10240 ]; then
echo "${size}K"
return
fi
size=$((size / 1024))
if [ $size -lt 10240 ]; then
echo "${size}M"
return
fi
size=$((size / 1024))
echo "${size}G"
}
estimate() {
local filename=$1
if [ $verbose -ne 0 ]; then
local size
size=$(size_of "$filename")
test -n "$size" || return
local pretty_size
pretty_size=$(pretty_size "$size")
echo "$filename is $pretty_size"
fi
}
grand_total=0
backup_root_included=0
sizes_reported=0
estimate_summary() {
if [ $backup_root_included -eq 0 ]; then
local dir
dir=$(backupdir)
local size
size=$(size_of "$dir")
test -n "$size" || size=$(size_of_last_backup "$BACKUP_ROOT")
test -n "$size" || {
error "Backup was not created yet ($dir missing)"
exit 1
}
local pretty_size
pretty_size=$(pretty_size "$size")
echo "$dir is $pretty_size"
grand_total=$((grand_total + size))
sizes_reported=$((sizes_reported + 1))
fi
if [ $sizes_reported -ge 2 ]; then
local pretty_size
pretty_size=$(pretty_size $grand_total)
echo "Total: $pretty_size"
fi
}
#
# Overridden back up functions
#
back_up() {
local pathname=$1
local outfile
outfile=$(backupdir)/$(slugify "$pathname").tar.gz
estimate "$outfile"
}
back_up_to() {
local name=$1
local outfile
outfile=$(backupdir)/$name.tar.gz
estimate "$outfile"
}
back_up_uncompressed() {
local pathname=$1
local outfile
outfile=$(backupdir)/$(slugify "$pathname").tar
estimate "$outfile"
}
back_up_uncompressed_to() {
local name=$1
local outfile
outfile=$(backupdir)/$name.tar
estimate "$outfile"
}
back_up_dpkg_selections() {
local outfile
outfile=$(backupdir)/dpkg--get-selections.gz
estimate "$outfile"
outfile=$(backupdir)/var-lib-apt-extended_states.gz
estimate "$outfile"
}
back_up_postgresql() {
local outfile
outfile=$(backupdir)/postgresql-dump.sql.gz
estimate "$outfile"
}
back_up_mysql() {
local outfile
outfile=$(backupdir)/mysql-dump.sql.gz
estimate "$outfile"
}
back_up_svn() {
local pathname=$1
local outfile
outfile=$(backupdir)/$(slugify "$pathname").svndump.gz
estimate "$outfile"
}
encrypt_dir() {
local suffix=${1:-$BACKUP_SUFFIX}
local gpg_suffix=${2:-$suffix-gpg}
estimate "$(backupdir "$gpg_suffix")"
}
generate_checksums() {
local suffix=${1:-$BACKUP_SUFFIX}
local outfile
outfile=$(backupdir "$suffix")/SHA256SUMS
estimate "$outfile"
}
clean_up_old_backups() {
local keep=$1
local where=${2:-$BACKUP_ROOT}
local suffix=${3:-$BACKUP_SUFFIX}
local size
size=$(size_of_last_backup "$where" "$suffix")
test -n "$size" || return
local pretty_size
pretty_size=$(pretty_size "$size")
local total
total=$((size * keep))
local pretty_total
pretty_total=$(pretty_size $total)
echo "$keep copies of ${where%/}/YYYY-MM-DD$suffix ($pretty_size) is $pretty_total"
grand_total=$((grand_total + total))
sizes_reported=$((sizes_reported + 1))
if [ x"$where" = x"$BACKUP_ROOT" ]; then
backup_root_included=1
fi
}
copy_backup_to() {
:
}
rsync_to() {
:
}
rsync_backup_to() {
:
}
scp_backup_to() {
:
}