Skip to content

Commit 8eb4fdb

Browse files
committed
Refactored fbsnapshot and fbrestore, removed some bugs too.
1 parent 26ad4ef commit 8eb4fdb

File tree

5 files changed

+372
-332
lines changed

5 files changed

+372
-332
lines changed

.local/bin/fb/OneShot/OneShot.backup.sh

+25-28
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ err_report() {
77
trap 'err_report $LINENO' ERR
88
VERSION="\"v0.0.2\""
99
if [ ! -v FB ] ; then
10-
echo "${0##*/}" "The variable \$FB isn't set, is the system initialized? You need configure it.\nTerminating..." | journalThis 2
10+
echo "${0##*/}" "The variable \$FB isn't set, is the system initialized? You need configure it.\nTerminating..." | journalThis 2 OneShot
1111
exit 255
1212
fi
1313

@@ -134,9 +134,11 @@ else
134134
if [ $DRYRUN = false ] ; then
135135
echo -e $PNAME : "The destination folder $2 does not exist.\nTerminating..."
136136
exit 2
137-
else
138-
HAVING_ERRORS=true
139-
echo -e $PNAME : "The destination folder $2 does not exist."
137+
138+
# else
139+
# THE Directory doesn't need to exist when dry-run is true
140+
# HAVING_ERRORS=true
141+
# echo -e $PNAME : "The destination folder $2 does not exist."
140142
fi
141143
fi
142144
TARGET_FOLDER="$1"
@@ -164,9 +166,10 @@ else
164166
if [ $DRYRUN = false ] ; then
165167
echo -e $PNAME : "The destination folder $2 does not exist.\nTerminating..."
166168
exit 2
167-
else
168-
HAVING_ERRORS=true
169-
echo -e $PNAME : "The destination folder $2 does not exist."
169+
# else
170+
# No need under dryrun
171+
# HAVING_ERRORS=true
172+
# echo -e $PNAME : "The destination folder $2 does not exist."
170173
fi
171174
fi
172175

@@ -245,7 +248,7 @@ if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then
245248
else
246249
VERBOSE_OPTIONS="-v"
247250
fi
248-
exit_status=0
251+
EXIT_STATUS=0
249252
if [ $DRYRUN = true ] ; then
250253

251254
DRY_RUN_FOLDER=$(mktemp -d "/tmp/OneShot.backup.sh.XXX")
@@ -267,21 +270,22 @@ ctrl_c() {
267270
echo "$PNAME : sudo tar -z $VERBOSE_OPTIONS -c -f "$TAR_BALL_NAME" $EXCLUDE_OPTIONS -C "$TARGET_FOLDER" . "
268271
# | journalThis 7 OneShot
269272
sudo tar -z -c $VERBOSE_OPTIONS -f "$TAR_BALL_NAME" $EXCLUDE_OPTIONS -C "$TARGET_FOLDER" .
270-
exit_status=$?
271273
# | journalThis 7 OneShot
272-
if [ $exit_status -gt 1 ] ; then
273-
echo "$PNAME : exit status after tar commmand = $exit_status"
274-
echo "$PNAME : rm -fr $DRY_RUN_FOLDER"
275-
if [ -d $DRY_RUN_FOLDER ] ; then
274+
if [ -d $DRY_RUN_FOLDER ] ; then
276275
rm -fr $DRY_RUN_FOLDER
277-
fi
276+
fi
277+
EXIT_STATUS=$?
278+
if [ $EXIT_STATUS -gt 1 ] ; then
279+
echo "$PNAME : exit status after tar commmand = $EXIT_STATUS"
280+
echo "$PNAME : rm -fr $DRY_RUN_FOLDER"
278281
fi
279282
else
280283
echo -e "$PNAME : DRY_RUN_FOLDER=\$(mktemp -d \"/tmp/OneShot.restore.sh.XXX\")"
281284
echo -e "$PNAME : sudo tar -z -c $VERBOSE_OPTIONS -c $EXCLUDE_OPTIONS -f "$TAR_BALL_NAME" -C "$TARGET_FOLDER" . "
282285
echo -e "$PNAME : rm -fr $DRY_RUN_FOLDER"
283286
fi
284287
else
288+
285289
TAR_BALL_NAME="$TODAYS_BACKUP_FOLDER"/$(baseNameTimeStamped "$SYMLINK_NAME" )-backup.tar.gz
286290
# TODO: installer trap her.
287291
trap "HAVING_ERRORS=true;ctrl_c" INT
@@ -295,12 +299,12 @@ ctrl_c() {
295299
echo -e "$PNAME : sudo tar -z -c $VERBOSE_OPTIONS -c $EXCLUDE_OPTIONS -f $TAR_BALL_NAME -C $TARGET_FOLDER" .
296300
fi
297301
sudo tar -z $VERBOSE_OPTIONS -c $EXCLUDE_OPTIONS -f $TAR_BALL_NAME -C "$TARGET_FOLDER" .
298-
exit_status=$?
302+
EXIT_STATUS=$?
299303
# | journalThis 7 OneShot
300-
if [ $exit_status -gt 1 ] ; then
304+
if [ $EXIT_STATUS -gt 1 ] ; then
301305

302306
if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then
303-
echo "$PNAME : exit status after tar commmand = $exit_status"
307+
echo "$PNAME : exit status after tar commmand (fatal error) = $EXIT_STATUS"
304308
fi
305309

306310
if [ -f $TAR_BALL_NAME ] ; then
@@ -309,17 +313,10 @@ ctrl_c() {
309313
fi
310314
rm -f $TAR_BALL_NAME
311315
fi
312-
elif [ $exit_status -eq 0 ] ; then
313-
echo "("$TAR_BALL_NAME")"
316+
elif [ $EXIT_STATUS -eq 0 ] ; then
317+
echo -e "\n("$TAR_BALL_NAME")\n"
314318
fi
315319

316-
# TODO: test på exit code her, og rydd opp hvis stoerre enn 1 (på denne backupen er det bare fila som skal slettes).
317-
# the output sent as a notice message.
318320
fi
319-
320-
# TODO: More work on the notify-send message, and needs to send a message to the Journal as well.
321-
# Needs to learn the journalctl better first.
322-
if [[ $exit_status -eq 0 && $DRYRUN = false && $VERBOSE = true || $DEBUG -eq 0 ]] ; then
323-
echo "${0##*/} (Folder Backup):" "OneShot backup of $TARGET_FOLDER into $TODAYS_BACKUP_FOLDER complete!" | journalThis 5 OneShot
324-
fi
325-
exit $exit_status
321+
322+
exit $EXIT_STATUS

.local/bin/fb/OneShot/OneShot.restore.sh

+44-36
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,13 @@ if [ -r "$1" ] ; then
127127
echo -e $PNAME : "The backup source (candidate) exists!"
128128
fi
129129
DEST_TEST="${1/$FB/}"
130-
if [[ $DRYRUN = false && "$DEST_TEST" = "$1" ]] ; then
131-
echo -e $PNAME : "The backup source to restore are not allowed to be outside "$FB".\nTerminating..."
132-
exit 2
133-
else
134-
HAVING_ERRORS=true
130+
if [[ "$DEST_TEST" = "$1" ]] ; then
131+
echo -e $PNAME : "The backup source to restore are not allowed to be outside "$FB".\nTerminating..."
132+
exit 2
135133
fi
136-
elif [ $DRYRUN = false ] ; then
134+
else
137135
echo -e $PNAME : "The backup source \"$1\" doesn't exist!\nTerminating..."
138136
exit 2
139-
else
140-
echo -e $PNAME : "The backup source \"$1\" doesn't exist!"
141-
HAVING_ERRORS=true
142137
fi
143138

144139
# We need to sort out which backup to restore.
@@ -205,7 +200,7 @@ if [ "$BACKUP_SOURCE_TYPE" = "folder" ] ; then
205200
fi
206201
else
207202
BACKUP_SOURCE="${BACKUP_CANDIDATE[0]}"
208-
if [ $DEBUG -eq 0 ] ; then
203+
if [ $DEBUG -eq || $VERBOSE = true ] ; then
209204
echo -e $PNAME : "We found the latest file within the folder:\n$BACKUP_SOURCE"
210205
fi
211206
fi
@@ -216,7 +211,11 @@ fi
216211

217212
# Preparing the folder name we shall append, or not.
218213
FOLDER_BASE_NAME="${BACKUP_SOURCE##*/}"
219-
FOLDER_STEM_NAME="${FOLDER_BASE_NAME%%.*}"
214+
if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then
215+
echo -e $PNAME : "The folder base name we will use for basis for the the tar-dump in is:\n$FOLDER_BASE_NAME"
216+
fi
217+
FOLDER_STEM_NAME="$( baseNameFromBackupFile \"$FOLDER_BASE_NAME\")"
218+
# FOLDER_STEM_NAME="${FOLDER_BASE_NAME%%.*}"
220219

221220
if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then
222221
echo -e $PNAME : "The folder stem name we will use for storing the tar-dump in is:\n$FOLDER_STEM_NAME"
@@ -246,7 +245,7 @@ if [ "$PROBE" = "$DEST_FOLDER" ] ; then
246245
if [ $DRYRUN = false ] ; then
247246
if [ ! -d "$DEST_FOLDER" ] ; then
248247
if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then
249-
echo "$PNAME : "$DEST_FOLDER" didn't exist: mkdir -p "$DEST_FOLDER"."
248+
echo "$PNAME : $DEST_FOLDER didn't exist: mkdir -p $DEST_FOLDER."
250249
fi
251250
mkdir -p "$DEST_FOLDER"
252251
MADE_FOLDER=true
@@ -263,11 +262,11 @@ if [ "$PROBE" = "$DEST_FOLDER" ] ; then
263262
# NO: because we-re not really making a restore when dryrun is on,
264263
# we do restore to a temp folder that we subsequently delete.
265264
if [ ! -d "$DEST_FOLDER" ] ; then
266-
echo $PNAME : Making destination folder: mkdir -p "$DEST_FOLDER"
267-
else
268-
echo "$PNAME : "$DEST_FOLDER" already exist and --force isn't used : bailing out"
269-
ls -ld "$DEST_FOLDER"
270-
exit 2
265+
echo "$PNAME : WOULD have made destination folder: mkdir -p $DEST_FOLDER"
266+
else
267+
echo "$PNAME : $DEST_FOLDER already exist and --force isn't used : bailing out"
268+
ls -ld "$DEST_FOLDER"
269+
exit 2
271270
fi
272271
fi
273272
else
@@ -318,7 +317,7 @@ else
318317
fi
319318

320319

321-
exit_status=0
320+
EXIT_STATUS=0
322321
if [ $DRYRUN = true ] ; then
323322
trap "ctrl_c" INT
324323

@@ -327,9 +326,8 @@ ctrl_c() {
327326
echo "$PNAME : We: rm -fr $DRY_RUN_FOLDER."
328327
rm -fr $DRY_RUN_FOLDER
329328
}
330-
if [ $HAVING_ERRORS = false ] ; then
331-
DRY_RUN_FOLDER=$(mktemp -d "/tmp/OneShot.restore.sh.XXX")
332-
sudo tar -x -z $VERBOSE_OPTIONS -f "$BACKUP_SOURCE" -C $DRY_RUN_FOLDER
329+
if [ $HAVING_ERRORS = false ] ; then DRY_RUN_FOLDER=$(mktemp -d "/tmp/OneShot.restore.sh.XXX")
330+
sudo tar -x -z $VERBOSE_OPTIONS -f "$BACKUP_SOURCE" -C "$DRY_RUN_FOLDER"
333331
if [ $? -lt 130 ] ; then
334332
rm -fr $DRY_RUN_FOLDER
335333
fi
@@ -338,8 +336,10 @@ ctrl_c() {
338336
echo -e "$PNAME : tar -x -z $VERBOSE_OPTIONS -f \"$BACKUP_SOURCE\" -C $DRY_RUN_FOLDER"
339337
echo -e "$PNAME : rm -fr $DRY_RUN_FOLDER"
340338
fi
339+
341340
else
342-
trap "ctrl_c" INT
341+
342+
trap "ctrl_c" INT
343343

344344
ctrl_c() {
345345
if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then
@@ -350,20 +350,28 @@ ctrl_c() {
350350
rm -fr $DEST_FOLDER
351351
fi
352352
}
353+
354+
if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then
355+
echo -e "$PNAME : sudo tar -x -z $VERBOSE_OPTIONS -f $BACKUP_SOURCE -C $DEST_FOLDER"
356+
fi
353357
sudo tar -x -z $VERBOSE_OPTIONS -f "$BACKUP_SOURCE" -C "$DEST_FOLDER"
354-
exit_status=$?
355-
fi
356-
if [ $exit_status -eq 0 ] ; then
357-
echo "("$DEST_FOLDER")"
358-
# This only looks like this here, not when the script is implicitly
359-
# initiated from a daemon.
360-
elif [ $exit_status -eq 1 ] ; then
361-
echo "$PNAME : Contents of "$DEST_FOLDER"."
362-
ls -l "$DEST_FOLDER"
363-
echo "$PNAME : Contents of "$DEST_FOLDER"."
364-
elif [ $exit_status -ne 130 ] ; then
365-
if [[ $MADE_FOLDER = true || $WITHIN_TMP = true ]] ; then
366-
rm -fr $DEST_FOLDER
358+
EXIT_STATUS=$?
359+
# | journalThis 7 OneShot
360+
361+
if [ $EXIT_STATUS -gt 1 ] ; then
362+
if [[ $VERBOSE = true || $DEBUG -eq 0 ]] ; then
363+
echo "$PNAME : exit status after tar commmand (fatal error) = $EXIT_STATUS"
364+
fi
365+
366+
if [ $EXIT_STATUS -ne 130 ] ; then
367+
if [[ $MADE_FOLDER = true || $WITHIN_TMP = true ]] ; then
368+
rm -fr $DEST_FOLDER
369+
fi
370+
fi
371+
elif [ $EXIT_STATUS -eq 0 ] ; then
372+
echo -e "\n("$DEST_FOLDER")\n"
373+
# This only looks like this here, not when the script is implicitly
374+
# initiated from a daemon.
367375
fi
368376
fi
369-
exit $exit_status
377+
exit $EXIT_STATUS

0 commit comments

Comments
 (0)