-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_dir.sh
executable file
·194 lines (170 loc) · 4.29 KB
/
backup_dir.sh
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
#!/bin/bash
VERSION="1.1"
# V0.1 - V1.0 ; years ago ; sed ; Various versions fixing and optimizing on the run
# Version 1.1 ; 2024-12-12 ; sed ; Modernization and a history section (sort of)
SHORT="a,c,f,h,l:,n,s:t:,v,z"
LONG="auto,cleanup,fsync,help,logfile,no-dir,source:,target:,verify,compress"
OPTS=$(getopt --name backup_dir --options $SHORT --longoptions $LONG -- "$@")
eval set -- "$OPTS"
xAsk="yes"
xCompress=""
xDel=""
xLogFile=""
xShowHelp="no"
xSrc=""
xSubDir="yes"
xTgt=""
xVerify="no"
while :; do
case "$1" in
-a | --auto)
xAsk="no"
shift 1
;;
-c | --cleanup)
xDel="--delete-during"
shift 1
;;
-h | --help)
xShowHelp="yes"
shift 1
;;
-f | --fsync)
xSync="--fsync"
shift 1
;;
-l | --logfile)
xLogFile="$2"
shift 2
;;
-n | --no-dir)
xSubDir="no"
shift 1
;;
-s | --source)
xSrc="$2"
shift 2
;;
-t | --target)
xTgt="$2"
shift 2
;;
-v | --verify)
xVerify="yes"
shift 1
;;
-z | --compress)
xCompress="z"
shift 1
;;
--)
shift
break
;;
*)
echo "Unexpected option \"$1\" encountered"
exit 1
;;
esac
done
xExtras="$*"
if [[ -z $xTgt || -z $xSrc || "$xShowHelp" = "yes" ]]; then
echo "Usage: $0 [OPTIONS] <-s|--source source> <-t|--target target> [-- [rsync options]]"
echo
echo "Backup <source> into <target>"
echo "Return 0 on success, 1 if rsync failed, 2 if CTRL-C was caught"
echo
echo "OPTIONS:"
echo " -a --auto : Do not ask, start directly. Use with care!"
echo " -c --cleanup : Files in the target that no longer exit in source are deleted"
echo " -f --fsync : Do fsync after each written file."
echo " -h --help : Show this help and exit"
echo " -l --logfile : Set a log file. Default is to write into the parent of the target"
echo " -n --no-dir : Do not create a subdirectory in the target, copy directly"
echo " -v --verify : Add a second rsync run that checks all checksums"
echo " -z --compress : Compress file streams. Only use on very slow network connections"
echo -e "\n\tV${VERSION}"
exit 0
fi
# Normalize trailing spaces
xSrc="$(echo -n "$xSrc" | sed -e 's,/*$,,g')/"
xTgt="$(echo -n "$xTgt" | sed -e 's,/*$,,g')/"
# If the --no-dir option was not used, add basename of source as a subdirectory to target
if [[ "yes" = "$xSubDir" ]]; then
xTgt="$xTgt$(basename "$xSrc")/"
fi
# Log into dirname of (the final) target, that should always be writable by the caller.
xLog="$(dirname "$xTgt")/backup_$(basename "$xSrc")_$(date '+%Y%m%d').log"
# If a logfile was set, use that instead
if [[ "x" != "x$xLogFile" ]]; then
xLog="${xLogFile}"
fi
function log {
local xMsg
local xNow
xMsg="$*"
xNow="$(date '+%Y-%m-%d %H:%M:%S')"
echo "$xNow : $xMsg"
echo "$xNow : $xMsg" >>"$xLog"
return 0
}
# Instead of getting killed directly by CTRL-C, let's try a cleaner interrupt.
# Also a verification run should not start if we where told to break off
xHaveCtrlC=0
function ctrlc {
# shellcheck disable=SC2317
xHaveCtrlC=1
# shellcheck disable=SC2317
log "CTRL-C caught, breaking off..."
# shellcheck disable=SC2317
return 0
}
# -----------------------------------------------------------------------------
if [ "$xAsk" = "no" ]; then
echo -n "Backing up content from $xSrc into $xTgt ("
if [ -n "$xDel" ]; then
echo "WITH cleanup)"
else
echo " no cleanup)"
fi
else
echo -n "Backup content from $xSrc into $xTgt ? [Y/n] ("
if [ -n "$xDel" ]; then
echo -n "WITH cleanup) : "
else
echo -n " no cleanup) : "
fi
read -r answer
if [ "n" = "$answer" ]; then
echo "Backup aborted"
exit 0
fi
fi
xReturn=0
# FIRST run: Normal rsync backup
if [[ 0 -eq $xHaveCtrlC ]]; then
log "Start Backup $xSrc into $xTgt"
cmd_args="-avhHA${xCompress} $xDel --log-file=$xLog --progress $xSync $xExtras"
cmd="rsync $cmd_args --size-only $xSrc $xTgt"
log "$cmd"
$cmd
xReturn=$?
fi
# SECOND run; Verification rsync check
if [[ "$xVerify" = "yes" && 0 -eq $xHaveCtrlC && 0 -eq $xReturn ]]; then
log "Start Verify $xTgt"
cmd="rsync $cmd_args -c $xSrc $xTgt"
log "$cmd"
$cmd
xReturn=$?
fi
# Make sure we do post a useful exit code
xExit=0
if [[ 0 -ne $xReturn ]]; then xExit=1; fi
if [[ 0 -ne $xHaveCtrlC ]]; then xExit=2; fi
log "End Backup $xSrc into $xTgt [Exit $xExit]"
# Pack logfile to safe space
if [[ -f "${xLog}" ]]; then
bzip2 "${xLog}"
fi
exit $xExit