-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbakudf.sh
executable file
·152 lines (126 loc) · 3.22 KB
/
bakudf.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
#!/bin/bash
# other program locations
TRUNCATE=/usr/bin/truncate
MKUDFFS=/usr/bin/mkudffs
SUDO=/usr/bin/sudo
MOUNT=/usr/bin/mount
UMOUNT=/usr/bin/umount
# output text
USAGE="bakudf - udf backup helper script
Usage:
$0 <create> <project> <size>
$0 <md5> <project> [create|verify]
$0 <finalize> <project>
Arguments:
create: create a new project
md5: create (default) or verify md5 checksums for a project
finalize: finalize a project
project: name of the new project
size: expected size of the project, e.g., 25GB, 5GB
First steps:
* Create a new project with
$0 create <project> <size>"
STEPS_AFTER_CREATE="Next steps:
* Copy all files to the folder \"%s\".
* Optional: create md5 checksums for the files in \"%s\" with
$0 md5 %s
* Finalize your project with:
$0 finalize %s"
STEPS_AFTER_MD5_CREATE="Next steps:
* Optional: copy \"%s.md5\" into your project folder \"%s\".
* Finalize your project with:
$0 finalize %s"
STEPS_AFTER_FINALIZE="Next steps:
* Burn the udf file to a disc, e.g., with:
cdrecord -v -doa driveropts=burnfree dev=/dev/sr0 %s.udf
(Replace /dev/sr0 with the path to your device).
* You can remove the project folder \"%s\" and udf file \"%s.udf\""
# create new project
function create_project {
# arguments
PROJECT=$1
SIZE=$2
# check arguments
if [[ "$#" -lt 2 ]]; then
echo "$USAGE"
exit
fi
# check if files/folders exist and abort in that case
FILES="$PROJECT.udf $PROJECT"
for i in $FILES; do
if [[ -e "$i" ]]; then
echo "$i already exists."
exit
fi
done
# create udf file for project
echo "Creating a new udf file: $PROJECT.udf"
$TRUNCATE -s "$SIZE" "$PROJECT".udf
# create udf file system in udf file
echo "Creating a new udf file system in $PROJECT.udf"
$MKUDFFS --label="$PROJECT" "$PROJECT".udf > /dev/null
# mount udf file
echo "Mounting $PROJECT.udf to the folder \"$PROJECT\""
mkdir "$PROJECT"
$SUDO $MOUNT -t udf -o loop,rw "$PROJECT".udf "$PROJECT"
# give further instructions
echo "${STEPS_AFTER_CREATE//%s/$PROJECT}"
}
# run md5 commands
function run_md5 {
# arguments
PROJECT=$1
CMD=$2
# md5 file
MD5=$(pwd)"/$PROJECT".md5
# check arguments
if [[ "$#" -lt 1 ]]; then
echo "$USAGE"
exit
fi
# check if file already exists
if [[ -e "$MD5" ]]; then
echo "$MD5 already exists."
exit
fi
# create a new md5 file for all files in a directory
if [[ -z "$CMD" || "$CMD" == "create" ]]; then
echo "Creating md5 checksums for \"$PROJECT\" in \"$MD5\""
cd "$PROJECT" || exit
find . -type f -exec md5sum {} \;>> "$MD5"
# give further instruction
echo "${STEPS_AFTER_MD5_CREATE//%s/$PROJECT}"
fi
# verify files in a directory with md5 file
if [[ "$CMD" == "verify" ]]; then
cd "$PROJECT" || exit
echo "Verifying $(pwd) with $MD5"
md5sum -c "$MD5"
fi
}
# finalize a project
function finalize_project {
# arguments
PROJECT=$1
# check arguments
if [[ "$#" -lt 1 ]]; then
echo "$USAGE"
exit
fi
# umount udf file
$SUDO $UMOUNT "$PROJECT"
# give further instructions
echo "${STEPS_AFTER_FINALIZE//%s/$PROJECT}"
}
if [[ "$1" == "create" ]]; then
shift
create_project "$@"
elif [[ "$1" == "finalize" ]]; then
shift
finalize_project "$@"
elif [[ "$1" == "md5" ]]; then
shift
run_md5 "$@"
else
echo "$USAGE"
fi