blob: efccad4b8a2bad8e55be3470e3bf4b64a8a74718 (
plain) (
blame)
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
|
#!/usr/bin/env sh
# Generate a backup file named for the date, then run+update+compress
UPDATED=0
TARGET_DIR="/media/hdd/tdavis-archives/tech/backup"
SOURCE_DIR="$HOME"
TODAY=`date +"%Y%m%d"`
TF="home-${TODAY}.tar"
usage="$(basename "$0") [-h] [-t TARGET_DIR] [-s SOURCE_DIR]
where:
-s set the source directory (default: ${SOURCE_DIR})
-t set the backup directory (default: ${TARGET_DIR})
-h show this help text
"
while :; do
case $1 in
-h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit.
echo "$usage"
exit
;;
-s) # Takes an option argument, ensuring it has been specified.
if [ -n "$2" ]; then
SOURCE_DIR=$2
shift
fi
;;
-s=?*)
SOURCE_DIR=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
-s=) # Handle the case of an empty --file=
printf 'ERROR: "-s" requires a non-empty option argument.\n' >&2
exit 1
;;
-t) # Takes an option argument, ensuring it has been specified.
if [ -n "$2" ]; then
TARGET_DIR=$2
shift
fi
;;
-t=?*)
TARGET_DIR=${1#*=} # Delete everything up to "=" and assign the remainder.
;;
-t=) # Handle the case of an empty --t=
printf 'ERROR: "-t" requires a non-empty option argument.\n' >&2
exit 1
;;
--) # End of all options.
shift
break
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: If no more options then break out of the loop.
break ;;
esac
shift
done
# No posix-compliant way of getting processor count, so leverage OS-specifics
if [ "$(uname -s)" = "Darwin" ]; then
FS_SEPARATOR="/"
elif [ "$(uname -s)" = "Linux" ]; then
FS_SEPARATOR="/"
else
# Windows uses backslash
FS_SEPARATOR="\\"
fi
# Verify free space
SOURCE_SIZE=$(du -sk ${SOURCE_DIR} | cut -f 1)
DOUBLE_SOURCE_SIZE=$(echo "${SOURCE_SIZE}*2" | bc -q)
TARGET_FREE=$(df -Pk ${TARGET_DIR} | grep -vE "^Filesystem" | awk '{ print $4 }')
if [ $DOUBLE_SOURCE_SIZE -gt $TARGET_FREE ]; then
echo "insufficient space to perform backup. 2x Source Directory usage is ${DOUBLE_SOURCE_SIZE}, Target Directory free space is: ${TARGET_FREE}"
exit 1
fi
COMPRESSION="gzip" # Gzip default
SUFFIX=".gz"
if [ $(command -v zstd) ]; then
COMPRESSION="zstd -T0 -qq"
SUFFIX=".zstd"
elif [ $(command -v pigz) ]; then
COMPRESSION="pigz -p"
fi
FULL_TF="${TARGET_DIR}${FS_SEPARATOR}${TF}"
# Updating existing archives almost always fails. Build and zip in one shot.
if [ -e ${FULL_TF}${SUFFIX} ]; then
rm -f ${FULL_TF}${SUFFIX};
fi
# Do the backup
if tar -C ${SOURCE_DIR} -c -f ${FULL_TF} ${SOURCE_DIR} >/dev/null 2>&1 ; then
${COMPRESSION} ${FULL_TF}
rm -f ${FULL_TF} # Delete the original tar file just in case
fi
|