/
simplebackup
executable file
·238 lines (201 loc) · 5.27 KB
/
simplebackup
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/bash
#
# simplebackup - a simple backup script
#
# Copyright (C) 2004-2009,2011,2019,2021,2022,2023 Christian Garbs <mitch@cgarbs.de>
# licensed under GNU GPL v2 or later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# initial setup
set -e
export PATH=/usr/sbin:/usr/bin:/sbin:/bin
RCFILE=~/.simplebackup.conf
# these defaults can be overridden in the $RCFILE
BACKUP_EXTENSION=tar.bz2
archive_and_compress() {
tar -cjf "$BACKUPFILE" "$TARGET"
}
# helper functions
echo_info() {
if [ "$SILENT" != "1" ] && [ "$SILENT" != "true" ]; then
echo "$@"
fi
}
echo_warn() {
echo "$@"
}
echo_err() {
echo "$@" >&2
}
_dirstats() {
if [ "$DIRSTATS" != "1" ] && [ "$DIRSTATS" != "true" ]; then
return
fi
local DIR="$1" DU_FLAG="$2" FIND_FLAG="$3"
local SIZE
SIZE="$(du $DU_FLAG -BM -d0 "$DIR")"
SIZE="${SIZE%M*}M"
local FILES DIRS
FILES="$(find "$DIR" $FIND_FLAG -type f | wc -l)"
DIRS="$( find "$DIR" $FIND_FLAG -type d | wc -l)"
echo " ($SIZE in $DIRS dirs with $FILES files)"
}
dirstats() {
_dirstats "$1" '' ''
}
dirstats_onefs() {
_dirstats "$1" '-x' '-mount'
}
# help wanted?
if [ "$1" = "-h" ]; then
echo "usage:"
echo -e "\t$0 [-h] [--default-config-to-etc-if-root] [configuration_file]"
exit 0
fi
# change default configuration file location if root?
if [ "$1" = "--default-config-to-etc-if-root" ]; then
if [ "$UID" = 0 ]; then
RCFILE=/etc/simplebackup.conf
fi
shift
fi
# other configuration file given?
if [ "$1" ]; then
RCFILE="$1"
fi
# check permissions on configuration file
if [ $(( 0$(stat -c %a "$RCFILE") & 0022 )) -gt 0 ] ; then
echo_err Configuration file is writeable by group or others.
echo_err As this file is executed under your userid, this is a
echo_err security risk. A malicious user could add a command to
echo_err remove the contents of your home directory, for example.
echo_err Change the permissions to something like 600 or 644.
exit 2
fi
# set default values
unset SILENT
unset TARGETDIR
unset WORKDIR
unset BACKUPDIRS
unset BACKUPDIRS_ONEFS
unset NAME
unset LOCKFILE
unset NICELEVEL
unset CHROOT
extracommands() { :; }
postcopy() { :; }
postbackup() { :; }
# more initialization
source "$RCFILE"
TARGET="$NAME"-$(LANG=C date +%Y%m%d)
# check configuration
if [ -z "$TARGETDIR" ] ; then
echo_err TARGETDIR is not set
exit 3
else
if [ ! -d "$TARGETDIR" ] ; then
echo_err TARGETDIR is no directory
exit 3
fi
fi
if [ -z "$WORKDIR" ] ; then
echo_err WORKDIR is not set
exit 4
fi
if [ -z "${BACKUPDIRS}${BACKUPDIRS_ONEFS}" ] ; then
echo_err neither BACKUPDIRS nor BACKUPDIRS_ONEFS is set
exit 5
fi
if [ -z "$NAME" ] ; then
echo_err NAME is not set
exit 6
fi
if [ -z "$LOCKFILE" ] ; then
echo_warn warning: no LOCKFILE is given, multiple instances may run
fi
if [ -z "$NICELEVEL" ] ; then
NICELEVEL=0
fi
# init
if [ "$LOCKFILE" ] ; then
[ -e "$LOCKFILE" ] && echo_err "another backup is already running" && exit 1
echo $$ > "$LOCKFILE"
trap 'STATUS=$?; echo_err "Received ERR $STATUS"; rm -f "$LOCKFILE"; exit $STATUS' ERR
trap 'echo_err "Received SIGTERM"; rm -f "$LOCKFILE"; exit 98' SIGTERM
trap 'echo_err "Received SIGINT"; rm -f "$LOCKFILE"; exit 99' SIGINT
fi
renice $NICELEVEL $$ > /dev/null
# prepare temporary directory
echo_info "setting up"
PARENT_WORKDIR="$WORKDIR"
WORKDIR="$WORKDIR/$TARGET"
rm -rf "$PARENT_WORKDIR"
mkdir -m 700 -p "$PARENT_WORKDIR" "$WORKDIR"
chown $UID "$PARENT_WORKDIR" "$WORKDIR"
# execute extra commands
(
extracommands
)
# display chroot
if [ "$CHROOT" ]; then
echo_info "chroot to $CHROOT"
if [ ! -d "$CHROOT" ]; then
echo_err "chroot path not found!"
exit 3
fi
fi
# backup files
OLDIFS="$IFS"
IFS=":"
for DIR in $BACKUPDIRS; do
if [ -d "$CHROOT$DIR" ]; then
echo_info "backing up $DIR$(dirstats "$CHROOT$DIR")"
mkdir -p "$WORKDIR$DIR"
(cd "$CHROOT$DIR" ; tar -c .) | (cd "$WORKDIR$DIR" ; tar -x)
else
echo_err "$DIR is no directory!"
fi
done
for DIR in $BACKUPDIRS_ONEFS; do
if [ -d "$CHROOT$DIR" ]; then
echo_info "backing up $DIR (one fs)$(dirstats_onefs "$CHROOT$DIR")"
mkdir -p "$WORKDIR$DIR"
(cd "$CHROOT$DIR" ; tar -c --one-file-system .) | (cd "$WORKDIR$DIR" ; tar -x)
else
echo_err "$DIR is no directory!"
fi
done
IFS="$OLDIFS"
# execute extra commands after copying
(
postcopy
)
# build big archive
cd "$WORKDIR/.."
echo_info "building archive$(dirstats "$TARGET")"
BACKUPFILE="$TARGETDIR/$TARGET.$BACKUP_EXTENSION"
(
umask 0377
archive_and_compress
)
rm -rf "$TARGET"
rmdir "$PARENT_WORKDIR"
# execute postbackup commands
(
postbackup
)
echo_info "finished"
[ "$LOCKFILE" ] && rm "$LOCKFILE"
exit 0