-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddbag.sh
431 lines (368 loc) · 12.6 KB
/
ddbag.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/bin/bash
# Script started 18/04/2024 7:26
VERSION="1.0"
SCRIPTNAME="dd2bag"
SCRIPTPATH="$(readlink -f "${BASH_SOURCE[0]}")"
SCRIPTDIR="$(dirname "$SCRIPTPATH")"
DATE="$(date +%m-%d-%Y-%T)"
count=1
_usage() {
echo "script usage: ${SCRIPTDIR}/${SCRIPTNAME}.sh [-h]"
}
_help(){
cat <<EOF
${SCRIPTNAME}
Usage: dd2bag [options] <AIP> <target>
<AIP> = The identifier you are giving to the bag that will be created containing disk images. This string can only contain alphanumeric characters, periods, hyphens and underscores.
<target> = This is a folderpath where the bag will be created. This is optional. If a folderpath isn't given then it will default to the present working directory.
Examples: dd2bag 12345 '/home/bishbashbackup/Documents'
dd2bag -m package23 '/home/bishbashbackup/Documents'
dd2bag -mp -l disk1 package23 '/home/bishbashbackup/Documents'
Creates a disk image and then packages in a bagit format
The default disk drive is set to: "/dev/sr0". If yours is different, this can be changed in the config.txt file.
Options:
-d, --device Manually set the path to the device you are imaging, i.e. /dev/sr1
-D, --idirect Turns off ddrescue direct disc access, which is enabled by default
-l, --label Adds information to "originalName" element in PREMIS XML output.
-m, --multidisk Enables prompt to image more than one disk
-p, --premis Generates a simple PREMIS XML file at top level of bagit directory
-r, --retry <n> Set the number of times ddrescue will retry scanning the disk.
-i, --imager cdrdao Sets the imager to use cdrdao instead of ddrescue, for audio CDs
EOF
}
# Transforms long options to short ones
for arg in "$@"; do
shift
case "$arg" in
'--help') set -- "$@" '-h' ;;
'--device') set -- "$@" '-d' ;;
'--imager') set -- "$@" '-i' ;;
'--idirect') set -- "$@" '-D' ;;
'--label') set -- "$@" '-l' ;;
'--multidisk') set -- "$@" '-m' ;;
'--premis') set -- "$@" '-p' ;;
'--retry') set -- "$@" '-r' ;;
*) set -- "$@" "$arg" ;;
esac
done
# Default behavior
imager=ddrescue
idirect="-d"
multidisk=false
premis=false
labeller=false
retry=3
# Load in variable(s) from config.txt
source "$SCRIPTDIR/config.txt"
# Parse short options
OPTIND=1
while getopts "hd:i:Dlmpr:" opt ; do
case "$opt" in
'h') _help ; exit 0 ;;
'd') DRIVE=$OPTARG ;;
'i') imager=$OPTARG ;;
'D') idirect="" ;;
'l') labeller=true ;;
'm') multidisk=true ;;
'p') premis=true ;;
'r') retry=$OPTARG ;;
'?') _usage >&2; exit 1 ;;
esac
done
shift "$((OPTIND-1))"
# Load in variable from first positional parameter
if [[ -n "$1" && "$1" =~ ^[[:alnum:]._-]+$ ]]; then
AIP="$1"
else
echo "Invalid Input - You need to provide a valid AIP identifier in your command. This can only contain alphanumeric characters, periods, underscores and hyphens"
echo "Usage: "$(basename "$0")" <input>"
exit 1
fi
# Load in variable from second positional parameter (optional)
if [ -n "$2" ]; then
if [ -d "$2" ]; then
target="$2"
else
echo "Invalid target folder"
exit 1
fi
else
target="$(pwd)"
fi
# Initial checks to see if folder already exists with AIP name in target directory.
if [ -d "$target/$AIP" ]; then
echo "Folder named $AIP already exists in target directory."
exit 1
fi
# Function to check and handle errors
_check_error() {
if [ $? -ne 0 ]; then
echo "Error occurred during $1. Exiting."
exit 1
fi
}
# Function to check and handle disk imaging errors
_check_disk_error() {
if [ $? -ne 0 ]; then
echo "Error occurred during disk imaging of ${AIP}_${count}. Continuing, but this may need further passes."
fi
}
# Function to create disk images
_disk-image() {
# Check if the disk is present using blkid
while true; do
if [[ "$imager" == "ddrescue" ]] && blkid "$DRIVE" &>/dev/null; then
ddrescuelogs="$tempdir/$AIP/${AIP}_${count}_ddrescuelogs"
mkdir -p "$ddrescuelogs"
ddrescue -r"$retry" $idirect \
--log-events="$ddrescuelogs"/eventlog.txt \
--log-rates="$ddrescuelogs"/ratelog.txt \
--log-reads="$ddrescuelogs"/readlog.txt \
$DRIVE "$tempdir"/"$AIP"/"$AIP"_"$count".dd \
"$ddrescuelogs"/mapfile.txt
_check_disk_error
imagername=ddrescue
# touch "$tempdir"/"$AIP"/"$AIP"_"$count".dd
# echo "this is content" > "$tempdir"/"$AIP"/"$AIP"_"$count".dd
# echo "Created disk image $count"
break
elif [[ "$imager" == "cdrdao" ]] && cd-info "$DRIVE" &>/dev/null; then
cdrdao read-cd --source-device "$DRIVE" \
--paranoia-mode 0 --read-raw --read-subchan rw_raw --datafile \
"$tempdir"/"$AIP"/"$AIP"_"$count".bin \
"$tempdir"/"$AIP"/"$AIP"_"$count".toc
_check_disk_error
# read -p "Move files over" wait
toc2cue "$tempdir"/"$AIP"/"$AIP"_"$count".toc \
"$tempdir"/"$AIP"/"$AIP"_"$count"_temp.cue
_check_error "disk imaging"
sed "1s|^.*|FILE \"${AIP}_${count}.bin\" BINARY|" \
"$tempdir"/"$AIP"/"$AIP"_"$count"_temp.cue \
> "$tempdir"/"$AIP"/"$AIP"_"$count".cue
rm "$tempdir"/"$AIP"/"$AIP"_"$count"_temp.cue
imagername=cdrdao
break
else
echo "Error: Disk not found. Try again? (Answer yes or no): "
while true; do
read choice
if [[ "$choice" =~ ^(y|Y|yes|Yes|YES)$ ]]; then
echo "Trying again..."
sleep 3
break
elif [[ "$choice" =~ ^(n|N|no|No|NO)$ ]]; then
if [ -z $(ls -A "$tempdir/$AIP") ]; then
echo "Exiting script"
exit 0
else
echo "moving files from $tempdir/$AIP to $target"
mv "$tempdir"/"$AIP" "$target"
echo "Exiting script"
exit 0
fi
else
echo "Invalid input. Please enter 'yes' or 'no'."
fi
done
fi
done
}
# Function to check for multidisk imaging
_multi-image() {
while true; do
# eject
echo "Do you want to create another disk image? (Answer yes or no - if yes insert new disk before answering): "
read choice
if [[ "$choice" =~ ^(y|Y|yes|Yes|YES)$ ]]; then
((count++))
_disk-image
continue
elif [[ "$choice" =~ ^(n|N|no|No|NO)$ ]]; then
break
else
echo "Invalid input. Please enter 'yes' or 'no'."
fi
done
}
_update_tag() {
local file_path="$1"
local sha256=$(sha256sum "$file_path" | awk '{print $1}')
local sha512=$(sha512sum "$file_path" | awk '{print $1}')
local filename=$(basename "$file_path")
echo "$sha256 $filename" >> "$tempdir"/"$AIP"/tagmanifest-sha256.txt
_check_error "Updating sha256 tag"
echo "$sha512 $filename" >> "$tempdir"/"$AIP"/tagmanifest-sha512.txt
_check_error "Updating sha512 tag"
}
# Function to cleanup after script execution
_cleanup() {
if [ -d "$tempdir" ]; then
rm -r "$tempdir"
fi
}
# Trap for cleanup on script exit
trap _cleanup EXIT
# Create temporary directory for creating the bag
tempdir=$(mktemp -d "$target/dd2bag_XXXX")
_check_error "making the temp directory"
# Placeholders if PREMIS XML is generated
tempxml="$(mktemp -p "${tempdir}" temp_XXXXX.xml)"
premisxml="$(mktemp -p "${tempdir}" premis_XXXXX.xml)"
echo '<?xml version="1.0" encoding="UTF-8"?>' >> "$tempxml"
echo "<data>" >> "$tempxml"
# Make subdirectory for disk image
mkdir "$tempdir"/"$AIP"
# Create disk image, with loop for imaging multiple disks
_disk-image
_check_error "disk imaging"
if [[ "$multidisk" == true ]]; then
_multi-image
_check_error "multidisk imaging"
fi
echo "Disk Imaging complete! You can remove the disk."
# Optional element for creating PREMIS XML
if [[ "$premis" == true ]]; then
ddrescueline=$(grep "End of run" "$ddrescuelogs"/eventlog.txt)
read -r duration percentage _ <<< "$ddrescueline"
if [[ "$percentage" == "100.00%" ]]; then
outcome="success"
elif [[ "$percentagetest" == "0.00%" ]]; then
outcome="failure"
else
outcome="partial success"
fi
eventid=$(uuidgen)
objectxml=""
agentxml+="
<agent>
<agentname>$imagername</agentname>
<agentrole>executing program</agentrole>
</agent>
<agent>
<agentname>$USER</agentname>
<agentrole>implementer</agentrole>
</agent>"
eventxml+="
<event>
<eventid>$eventid</eventid>
<eventdate>$(date -Ins)</eventdate>
<eventdetail>runtime: "$duration"</eventdetail>
<eventoutcome>"$outcome"</eventoutcome>
<eventoutcomedetail>pct rescued: "$percentage"</eventoutcomedetail>
</event>"
exec 3<&0
exec 4<&0
find "$tempdir"/"$AIP" -type f -print |
while IFS= read -r file; do
extension="${file##*.}"
AIPbase=$(basename "$file")
folderpath="${file%/*}"
AIPdir=$(basename "$folderpath")
if [[ "$extension" == "dd" ]]; then
if [[ "$labeller" == true ]]; then
while true; do
read -rp "Enter disk label for $file or leave blank: " \
label <&3
if [[ -z "$label" || \
"$label" =~ ^[[:alnum:]_[:space:]-]+$ ]]; then
break # Exit the loop if the input is valid
else
echo "Error: Special characters are not allowed. Try again."
fi
done
fi
echo -e "
<file>
<objectid>"$AIPbase"</objectid>
<fixity>$(sha256sum "$file" | awk '{print $1}')</fixity>
<size>$(stat -c %s "$file")</size>
<format>linux dd raw disk image</format>
<label>"$label"</label>
</file>" >> "$tempxml"
elif [[ "$extension" == "bin" ]]; then
if [[ "$labeller" == true ]]; then
read -p "Enter disk label for $file or leave blank: " label <&3
fi
read -p "Enter the assumed format for $file: " format <&4
IFS='_' read -r AIP counter ext <<< "$AIPdir"
echo -e "
<file>
<objectid>"$AIPbase"</objectid>
<fixity>$(sha256sum "$file" | awk '{print $1}')</fixity>
<size>$(stat -c %s "$file")</size>
<format>"$format"</format>
<label>"$label"</label>
<reltype>structural</reltype>
<relsubtype>requires</relsubtype>
<linkedobjecttype>local</linkedobjecttype>
<linkedobjectvalue>"${AIP}"_"${counter}".cue</linkedobjectvalue>
<linkedeventtype>imaging</linkedeventtype>
<linkedeventvalue>"$eventid"</linkedeventvalue>
</file>" >> "$tempxml"
elif [[ "$extension" == "cue" ]]; then
IFS='_' read -r AIP counter ext <<< "$AIPdir"
echo -e "
<file>
<objectid>"$AIPbase"</objectid>
<fixity>$(sha256sum "$file" | awk '{print $1}')</fixity>
<size>$(stat -c %s "$file")</size>
<format>cue sheet file</format>
<reltype>structural</reltype>
<relsubtype>is Required By</relsubtype>
<linkedobjecttype>local</linkedobjecttype>
<linkedobjectvalue>"${AIP}"_"${counter}".bin</linkedobjectvalue>
<linkedeventtype>imaging</linkedeventtype>
<linkedeventvalue>"$eventid"</linkedeventvalue>
</file>" >> "$tempxml"
elif [[ "$extension" == "toc" ]]; then
IFS='_' read -r AIP counter ext <<< "$AIPdir"
echo -e "
<file>
<objectid>"$AIPbase"</objectid>
<fixity>$(sha256sum "$file" | awk '{print $1}')</fixity>
<size>$(stat -c %s "$file")</size>
<format>table of contents file</format>
<reltype>structural</reltype>
<relsubtype>is Required By</relsubtype>
<linkedobjecttype>local</linkedobjecttype>
<linkedobjectvalue>"${AIP}"_"${counter}".bin</linkedobjectvalue>
<linkedeventtype>imaging</linkedeventtype>
<linkedeventvalue>"$eventid"</linkedeventvalue>
</file>" >> "$tempxml"
elif [[ "$extension" == "txt" ]]; then
IFS='_' read -r AIP counter ext <<< "$AIPdir"
echo -e "
<file>
<objectid>"$AIPdir"/"$AIPbase"</objectid>
<fixity>$(sha256sum "$file" | awk '{print $1}')</fixity>
<size>$(stat -c %s "$file")</size>
<format>ddrescue log file</format>
<reltype>reference</reltype>
<relsubtype>documents</relsubtype>
<linkedobjecttype>local</linkedobjecttype>
<linkedobjectvalue>"${AIP}"_"${counter}".dd</linkedobjectvalue>
<linkedeventtype>imaging</linkedeventtype>
<linkedeventvalue>"$eventid"</linkedeventvalue>
</file>" >> "$tempxml"
fi
done
exec 3<&-
exec 4<&-
echo -e "$agentxml" >> "$tempxml"
echo -e "$eventxml" >> "$tempxml"
echo -e "</data>" >> "$tempxml"
xsltproc "$SCRIPTDIR"/resources/dd2premis.xsl "$tempxml" > "$premisxml"
_check_error "generating premis xml"
fi
echo "Tranferring to bagit structure..."
# Create Bagit folder structure
python3 -m bagit --quiet "$tempdir"/"$AIP"
_check_error "Creating Bagit Structure"
# Move Premis XML file to the top level of bagit structure and update tags
if [[ "$premis" == true ]]; then
mv "$premisxml" "$tempdir"/"$AIP"/premis.xml
_update_tag "$tempdir"/"$AIP"/premis.xml
fi
# Move Bagit out of temp directory
mv "$tempdir"/"$AIP" "$target"
echo "Bagging complete!"