-
Notifications
You must be signed in to change notification settings - Fork 8
/
writelto
executable file
·229 lines (200 loc) · 7.94 KB
/
writelto
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env bash
# writelto
# This script writes the contents of a specified directory onto a mounted
# LTFS-formated LTO tape.
DEPENDENCIES=(rsync)
TAPE_MOUNT_POINT="/Volumes"
TAPE_SERIAL_REGEX="^[A-Z0-9]{6}$"
unset HIDDEN_FILES
TAPE_EJECT="Y"
# no .DS_Store onto external storage
if [[ "$(uname -s)" = "Darwin" ]] ; then
if [[ "$(defaults read com.apple.desktopservices DSDontWriteNetworkStores 2>/dev/null)" != "true" ]] ; then
defaults write com.apple.desktopservices DSDontWriteNetworkStores true
fi
fi
_check_dependencies(){
DEPS_OK=YES
while [ "${*}" != "" ] ; do
DEPENDENCY="${1}"
if [ ! "$(which "${DEPENDENCY}")" ] ; then
_report -wt "This script requires ${DEPENDENCY} to run but it is not installed"
_report -wt "If you are running ubuntu or debian you might be able to install ${DEPENDENCY} with the following command"
_report -wt "sudo apt-get install ${DEPENDENCY}"
_report -wt "If you are running mac you might be able to install ${DEPENDENCY} with the following command"
_report -wt "brew install ${DEPENDENCY}"
DEPS_OK=NO
fi
shift
done
if [[ "${DEPS_OK}" = "NO" ]]; then
_report -wt "Unmet dependencies"
_report -wt "Aborting!"
exit 1
else
return 0
fi
}
_check_dir(){
if [[ ! -d "${1}" ]] ; then
_report -w "[${1}] is not a directory."
_usage
exit 1
fi
}
_check_for_colons(){
COLONALERT=$(find "${1}" -iname '*:*')
if [ -n "${COLONALERT}" ] ; then
_report -w "Note that LTFS formatted tapes called store files with colons in their filenames. Colons have been detected in the following file(s):"
for i in ${COLONALERT} ; do
_report -w " ${i}"
done
_report -w "Exiting"
exit 1
fi
}
_check_for_lto_index_dir(){
# if LTO_INDEX_DIR is set in ltopers.conf then that will be used, otherwise a default
if [ -z "${LTO_INDEX_DIR}" ] ; then
LTO_INDEX_DIR="${DEFAULT_LTO_INDEX_DIR}"
fi
}
_get_iso8601_c(){
date +%Y%m%d-%H%M%S
}
_mkdir2(){
local DIR2MAKE=""
while [ "${*}" != "" ] ; do
DIR2MAKE="${1}"
if [ ! -d "${DIR2MAKE}" ] ; then
mkdir -p "${DIR2MAKE}"
if [ "${?}" -ne 0 ]; then
_report -wt "${0}: Can't create directory at ${DIR2MAKE}"
exit 1
fi
fi
shift
done
}
_report(){
local RED="$(tput setaf 1)" # Red - For Warnings
local GREEN="$(tput setaf 2)" # Green - For Declarations
local BLUE="$(tput setaf 4)" # Blue - For Questions
local NC="$(tput sgr0)" # No Color
local COLOR=""
local STARTMESSAGE=""
local ECHOOPT=""
OPTIND=1
while getopts ":qdwstn" OPT; do
case "${OPT}" in
q) COLOR="${BLUE}" ;; # question mode, use color blue
d) COLOR="${GREEN}" ;; # declaration mode, use color green
w) COLOR="${RED}" ;; # warning mode, use color red
s) STARTMESSAGE+=([$(basename "${0}")] ) ;; # prepend scriptname to the message
t) STARTMESSAGE+=($(_get_iso8601_c) '- ' ) ;; # prepend timestamp to the message
n) ECHOOPT="-n" ;; # to avoid line breaks after echo
esac
done
shift $(( OPTIND - 1 ))
MESSAGE="${1}"
echo "${ECHOOPT}" "${COLOR}${STARTMESSAGE[*]}${MESSAGE}${NC}"
}
_usage(){
cat <<EOF
$(basename "${0}")
This script writes the contents of a specified directory onto a mounted
LTFS-formated LTO tape.
Dependencies: ${DEPENDENCIES[@]}
Usage: $(basename "${0}") -t [-e] [-v] | -h
-t <tape_serial> tape serial
-e [Y|N] state yes (Y) or no (N) to ejecting the tape after write, default
is yes
-v reads back and creates checksums for the contents of a tape, and
writes checksums to a file named with the tape name and date,
located in the LTO logs directory
-V <folder_name> this is like '-v' but only verifies the specified top level directory
-x use barcode label as tape serial (caveat: this is not standard)
-h display this help
EOF
}
OPTIND=1
while getopts ":t:e:vV:xh" opt ; do
case "${opt}" in
t) TAPE_SERIAL="${OPTARG}" ;;
e) TAPE_EJECT="${OPTARG}" ;;
v) VERIFY="Y" ;;
V) VERIFY_TOP_LEVEL="${OPTARG}" ;;
x) TAPE_SERIAL_REGEX="^[A-Z0-9]{6}(L[5-9]|M8)$" ;;
h) _usage ; exit 0 ;;
:) echo "Option -${OPTARG} requires an argument" ; exit 1 ;;
*) echo "Bad option -${OPTARG}" ; _usage ; exit 1 ;;
esac
done
shift "$((OPTIND-1))"
SOURCE_DIR="${1}"
DEFAULT_LTO_INDEX_DIR="${HOME}/Documents/lto_indexes"
_check_for_lto_index_dir
LTO_LOGS="${LTO_INDEX_DIR}"
_check_dependencies "${DEPENDENCIES[@]}"
if [[ ! "${TAPE_SERIAL}" ]] ; then
_report -qn "Enter the LTO tape barcode: "
read -e TAPE_SERIAL
fi
if [[ ! "${SOURCE_DIR}" ]] ; then
_report -qn "Drag in the directory to write to tape: "
read -e SOURCE_DIR
fi
if [[ ! $(echo "${TAPE_SERIAL}" | grep -E "${TAPE_SERIAL_REGEX}") ]] ; then
_report -w "Tape serial ${TAPE_SERIAL} is not valid."
_report -w "Proceeding with non-standard Tape Serial"
fi
# Check for colons in filenames of source directory.
_check_for_colons "${SOURCE_DIR}"
TAPE_PATH="${TAPE_MOUNT_POINT}/${TAPE_SERIAL}"
_check_dir "${SOURCE_DIR}"
_check_dir "${TAPE_PATH}"
WRITELTODIR="${LTO_LOGS}/writelto"
_mkdir2 "${WRITELTODIR}"
RSYNC_LOGS="${LTO_INDEX_DIR}/writelto_logs"
_mkdir2 "${RSYNC_LOGS}"
LOGFILE="${RSYNC_LOGS}/writelto_${TAPE_SERIAL}_$(_get_iso8601_c).log"
LOGFILE2="${RSYNC_LOGS}/writelto_${TAPE_SERIAL}_$(_get_iso8601_c)2.log"
RSYNC_ERROR_REPORT="${WRITELTODIR}/${TAPE_SERIAL}_writelto.txt"
removeDSStore "${SOURCE_DIR}"
rsync -rtvPih --log-file="${LOGFILE}" --cc none --whole-file --log-file-format="%t,%o,%C,%i,%M,%b,%l,%f" --exclude=".DS_Store" "${SOURCE_DIR}" "${TAPE_PATH}"
#gcp --preserve=mode,timestamps -nRv "${SOURCE_DIR}/" "${TAPE_PATH}"
RSYNC_ERR_1="${?}"
if [ -n "${RSYNC_ERR_1}" ] ; then
echo "$(_get_iso8601_c) rsync exited with ${RSYNC_ERR_1}. ${SOURCE_DIR} -> ${TAPE_PATH}" | tee -a "${RSYNC_ERROR_REPORT}" 2>&1
fi
rsync -rtvPih --log-file="${LOGFILE2}" --cc none --whole-file --log-file-format="%t,%o,%C,%i,%M,%b,%l,%f" --exclude=".DS_Store" "${SOURCE_DIR}" "${TAPE_PATH}"
RSYNC_ERR_2="${?}"
if [ -n "${RSYNC_ERR_2}" ] ; then
echo "$(_get_iso8601_c) rsync exited with ${RSYNC_ERR_2}. ${SOURCE_DIR} -> ${TAPE_PATH}" | tee -a "${RSYNC_ERROR_REPORT}" 2>&1
fi
_report -d "rsync error report written to: ${RSYNC_ERROR_REPORT}, "
if [[ "${VERIFY}" = "Y" ]] ; then
VERIFYTIME="$(_get_iso8601_c)"
READBACKDIR="${LTO_LOGS}/readback_checksums"
READBACKOUTPUT="${READBACKDIR}/${TAPE_SERIAL}_ReadBack_checksum_${VERIFYTIME}.md5"
_mkdir2 "${READBACKDIR}"
find "${TAPE_PATH}" -type f ! -name .DS_Store -exec md5deep -rel "{}" >> "${READBACKOUTPUT}" \;
sort -k 2 -o "${READBACKOUTPUT}" "${READBACKOUTPUT}"
_report -d "readback checksums written to: ${READBACKOUTPUT}"
elif [[ -n "${VERIFY_TOP_LEVEL}" ]]; then
VERIFY_TAPE_PATH="${TAPE_PATH}/${VERIFY_TOP_LEVEL}"
VERIFYTIME="$(_get_iso8601_c)"
READBACKDIR="${LTO_LOGS}/readback_checksums"
READBACKOUTPUT="${READBACKDIR}/${TAPE_SERIAL}_${VERIFY_TOP_LEVEL}_ReadBack_checksum_${VERIFYTIME}.md5"
_mkdir2 "${READBACKDIR}"
find "${VERIFY_TAPE_PATH}" -type f ! -name .DS_Store -exec md5deep -rel "{}" >> "${READBACKOUTPUT}" \;
sort -k 2 -o "${READBACKOUTPUT}" "${READBACKOUTPUT}"
_report -d "readback checksums written to: ${READBACKOUTPUT}"
fi
echo -n "$(date +%FT%T) " >> "${LTO_LOGS}/tape_capacity.txt"
echo $(df -Ph "${TAPE_PATH}" | tail -n 1) >> "${LTO_LOGS}/tape_capacity.txt"
renameschemas -u
case "${TAPE_EJECT}" in
y|Y) umount "${TAPE_PATH}" ;;
*) echo "Done writing but not ejecting ${TAPE_SERIAL}." ;;
esac