-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup-script
executable file
·149 lines (126 loc) · 3.05 KB
/
backup-script
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
#!/usr/bin/env bash
usage() {
echo "$0 [-h|--help] [--destination-root DIR] [backup path]..."
echo "--date defaults to today"
echo "--destination-root defaults to \$PWD ($PWD)"
echo "--dry-run"
exit 1
}
DEFAULT_PATHS_TO_BACKUP=(
$(eval echo ~$(logname))
/opt
/etc
/var/lib/plexmediaserver
)
######################
# Helper Functions #
######################
# $1: The root directory
# $2: The current destination
get_previous_destination() {
find "$1" -maxdepth 1 -mindepth 1 -type d -not -path "$2"\
| sort -rg\
| head -1
}
get_disk_usage() {
du -sB1 "$1" | cut -f1
}
#####################
# Parse Arguments #
#####################
opts=$(getopt -o 'h' --long 'help,date:,destination-root:,dry-run' -n "$0" -- "$@")
exit_code=$?
if [[ ${exit_code} != 0 ]]; then
echo "Error parsing options: ${opts}"
exit 1
fi
eval set -- "${opts}"
date=now
destination_root=$PWD
dry_run=
while true; do
case "$1" in
--)
shift
break
;;
-h|--help)
usage
;;
--date)
if [[ -n "$2" ]]; then
date="$2"
fi
shift 2
;;
--destination-root)
if [[ -n $2 ]]; then
destination_root=$(realpath "$2")
fi
shift 2
;;
--dry-run)
dry_run=1
shift
;;
*)
echo "Unrecognized option: $1"
exit 1
esac
done
if [[ $# -gt 0 ]]; then
paths_to_backup=( "$@" )
else
paths_to_backup=( "${DEFAULT_PATHS_TO_BACKUP[@]}" )
fi
###########
# Setup #
###########
if ! date_string=$(date --date "${date}" +%Y.%m.%d); then
echo "Invalid date ${date}"
exit 1
fi
destination="${destination_root}/${date_string}"
previous_destination=$(\
get_previous_destination "${destination_root}" "${destination}")
mkdir -p "${destination}"
exec &> >(tee -ia "${destination}/log")
################
# Processing #
################
echo "Starting at $(date)"
if [[ -n "${dry_run}" ]]; then
echo "THIS IS A DRY RUN"
fi
if [[ -n "${previous_destination}" ]]; then
echo "Identified ${previous_destination} as previous backup."
fi
echo "Estimating previous disk usage."
usage_before=$(get_disk_usage "${destination_root}")
echo "Getting package list -> ${destination}/packages.dpkg"
dpkg -l > "${destination}/packages.dpkg"
cmd=(rsync)
cmd+=(
--archive
--hard-links
--human-readable
--verbose
--progress
--relative
--filter ':- .backup_exclude'
)
if [[ -n "${dry_run}" ]]; then
cmd+=( --dry-run )
fi
if [[ -n "${previous_destination}" ]]; then
cmd+=( --link-dest "${previous_destination}" )
fi
cmd+=( "${paths_to_backup[@]}" "${destination}" )
echo "${cmd[*]}"
"${cmd[@]}"
echo "Estimating incremental disk usage."
usage_after=$(get_disk_usage "${destination_root}")
usage_diff=$((usage_after - usage_before))
human_usage_diff=$(numfmt --to=iec-i --suffix=B "${usage_diff}")
echo "Used ${human_usage_diff}"
echo "Ended at $(date)"