-
Notifications
You must be signed in to change notification settings - Fork 11
/
backmeup
167 lines (133 loc) · 4.26 KB
/
backmeup
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
#!/bin/bash
#----------------------------------------------------------------------------------
# Project Name - miscellaneous/backmeup
# Started On - Tue 7 Nov 21:50:05 GMT 2017
# Last Change - Sun 4 Mar 20:37:56 GMT 2018
# Author E-Mail - [email protected]
# Author GitHub - https://github.com/terminalforlife
#----------------------------------------------------------------------------------
_VERSION_="2018-03-04"
XERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; exit 1; }
ERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; }
USAGE(){
while read -r; do
printf "%s\n" "$REPLY"
done <<-EOF
BACKMEUP ($_VERSION_)
Written by terminalforlife ([email protected])
A simple tool to quickly and easily back up your HOME.
SYNTAX: backmeup [OPTS]
OPTS: --help|-h|-? - Displays this help information.
--version|-v - Output only the version datestamp.
--debug|-D - Enables the built-in bash debugging.
--quiet|-q - Runs in quiet mode. Errors still output.
--reset|-R - Re-run the initial setting prompts.
--notify|-N - Notify after completion, via notify-send.
EXAMPLE: backmeup --notify -Q
FILE: User settings saved in and read from: \$HOME/.backmeup/settings
NOTE: When entering your settings, ensure that no variables are used.
EOF
}
while [ "$1" ]; do
case "$1" in
--help|-h|-\?)
USAGE; exit 0 ;;
--version|-v)
printf "%s\n" "$_VERSION_"
exit 0 ;;
--debug|-D)
DEBUGME="true" ;;
--quiet|-q)
BEQUIET="true" ;;
--reset|-R)
RESET="true" ;;
--notify|-N)
NOTIFY="true" ;;
*)
XERR "$LINENO" "Incorrect argument(s) specified." ;;
esac
shift
done
declare -i DEPCOUNT=0
for DEP in /bin/{gzip,tar,sync,mkdir}; {
[ -x "$DEP" ] || {
ERR "$LINENO" "Dependency '$DEP' not met."
DEPCOUNT+=1
}
}
[ $DEPCOUNT -eq 0 ] || exit 1
[ "$BEQUIET" == "true" ] && exec 1> /dev/null
[ "$DEBUGME" == "true" ] && set -xeu
MAIN_DIR="$HOME/.backmeup"
SETTINGS="$MAIN_DIR/settings"
[ -d "$MAIN_DIR" ] || /bin/mkdir "$MAIN_DIR"
if [ ! -f "$SETTINGS" -o "$RESET" == "true" ]; then
read -ep "Directory for backmeup archives: " STORE
case "$REPLY" in
*)
if ! [ -d "$STORE" ]; then
XERR "$LINENO" "Directory not found or inaccessible."
fi ;;
"")
XERR "$LINENO" "You must enter a valid path." ;;
esac
printf -v DATETIME "%(%F_%H%M)T" "-1"
{
while read -r; do
printf "%s\n" "$REPLY"
done <<-EOF
# User settings file for and parsed by backmeup.
# Created: $DATETIME
# User: $USER
# UID: $UID
# The path to the target file system. (mountpoint)
STORE="$STORE"
# Exclude directories or files here.
EXCLUDE="$HOME/.cache"
EXCLUDE="$HOME/.thumbnails"
EOF
} > "$SETTINGS"
printf "Settings saved - Please relaunch backmeup.\n"
exit 0
elif ! [ -r "$SETTINGS" ]; then
XERR "$LINENO" "File '$SETTINGS' inaccessible."
fi
declare -a EXCLUDE
declare -i LINE=0
while read -a X; do
LINE+=1
[ "${X[0]:0:1}" == "#" ] && continue
IFS="=" read -ra Y <<< "${X[@]}"
case "${Y[0]:-EMPTY}" in
STORE)
printf "STORE: %s\n" "${Y[1]//\"}"
STORE="${Y[1]//\"}" ;;
EXCLUDE)
printf "EXCLUDING: %s\n" "${Y[1]//\"}"
EXCLUDE+=("--exclude=${Y[1]%/}") ;;
EMPTY)
;;
*)
XERR "$LINENO" "Invalid parameter '${Y[0]}' on line #$LINE." ;;
esac
done < "$SETTINGS"
[ -d "$STORE" ] || XERR "$LINENO" "Target '$STORE' not found."
printf -v ARCHIVE "BMU_%(%F_%H%M)T" "-1"
declare -i SECONDS=0
/bin/tar --xattrs --ignore-failed-read `eval printf "%s\\\\\n" ${EXCLUDE[@]}`\
--totals -cvpf "${STORE%/}/$ARCHIVE" "$HOME" || FAIL="true"
/bin/gzip -v --best --suffix ".tgz" "${STORE%/}/$ARCHIVE" || FAIL="true"
/bin/sync "${STORE%/}/$ARCHIVE.tgz" || FAIL="true"
if ! [ "$FAIL" == "true" ]; then
printf "Backup successfully created in %d seconds.\n" "$SECONDS"
else
ERR "$LINENO" "One or more errors detected during backup."
fi
if [ "$NOTIFY" == "true" ]; then
if [ -x /usr/bin/notify-send ]; then
/usr/bin/notify-send "Your backmeup execution is finished."
else
XERR "$LINENO" "Dependency '/usr/bin/notify-send' not met."
fi
fi
# vim: noexpandtab colorcolumn=84 tabstop=8 noswapfile nobackup