-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdsync.sh
executable file
·240 lines (209 loc) · 6 KB
/
sdsync.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
#!/bin/bash
#
# sdsync.sh
#
# An ugly hack to perform backup/sync of theoritically
# arbitrary directories, but intended to be used with
# memory cards
#
# It utilizes a unique identifier text file located
# on the cards to determine which source card is
# being read from, optionally this identifier can be
# automatically created
#
# After copying the files from source card to a
# destination folder, the script will calculate
# checksums of the newly copied data and log
# to a file, eventually to be used to identify
# corruption/bad data
#
#
# TODO:
# Usage:
# sdsync.sh [-b] [-s SRC] DEST
#
# -b will cause a backup to be run immediately, skipping
# the dry-run
# -s will supply a specific SRC folder (if not provided,
# will attempt to auto-detect)
# DEST is the backup destination, shortcuts can be used
#
#
# Future ideas:
# - Reimplement in Python, integrate with metasync as
# separate functionality
# - Possibly do EXIF datetime adjustment for TZ/location
# Would be non-trivial, maybe easier to keep using
# excellent regex.info GPS plugin and perform file
# rename/sync with new datetime while within LR
# catalog. Want to avoid solely external rename
# which would cause a rescan of catalog/on disk
# data. Maybe a LR plugin to a post-import file
# rename would work.
#
### Do the rsync ###
function do_backup()
{
SRC_ARG="$1"
DEST_ARG="$2"
rsync -av --progress "$SRC_ARG" "$DEST_ARG"
}
### main() ###
#ARGS=`getopt bs: $*`
#if [ "$?" != 0 ]; then
# echo "Usage: $0 [-b] [-s SRC] DEST"
# exit 2
#fi
#set -- $ARGS
#
#for i
#do
# case "$i"
# in
# -b)
# BACKUP=1
#
# TODO: Autodetect SRC (eg: SD card)
# This may be good place to start?
# mount | egrep disk[0-9]s1
#
SRC1="/Volumes/Untitled"
SRC2="/Volumes/NO NAME"
# Check to ensure that one of SRC1 or SRC2 paths exist
if [ ! -d "$SRC1" -a ! -d "$SRC2" ]; then
echo "[error] Source paths $SRC1 and $SRC2 not detected, aborting"
exit 1
fi
# If both SRC1 and SRC2 exist, SRC1 has higher priority
if [ -d "$SRC1" ]; then
SRC=$SRC1
elif [ -d "$SRC2" ]; then
SRC=$SRC2
fi
# Determine which data to backup
# TODO: Support handling multiple sources with one execution
SRCDATA="$SRC/DCIM/100MSDCF"
#SRCDATA="$SRC/DCIM/101PHOTO"
#SRCDATA="$SRC/DCIM/102SAVED"
#SRCDATA="$SRC/PRIVATE/M4ROOT/CLIP"
if [ ! -d "$SRCDATA" ]; then
echo "[error] Source path $SRCDATA not detected, aborting"
exit 1
fi
if [ -z "$1" ]; then
echo "[error] Destination argument required (one of [sdbackup, photography]), aborting"
exit 1
elif [ "$1" == "sdbackup" ]; then
DEST="/Users/adechiaro/Backup/sdbackup"
elif [ "$1" == "photography" ]; then
DEST="/Volumes/Photography/SD Backup"
else
echo "[error] Destination argument $1 invalid, aborting"
exit 1
fi
# Check to ensure that DEST path exists
if [ ! -d "$DEST" ]; then
echo "[error] Destination path $DEST not detected, aborting"
exit 1
fi
if [ "$2" == "--backup" ]; then
BACKUP="y"
else
BACKUP="n"
fi
### Provide a status of existing backups
echo "Detected card backups at $DEST:"
for d in `/bin/ls "$DEST"`;
do {
echo -n "$d: ";
COUNT=$(find "$DEST/$d" -type f | wc -l);
echo "$COUNT files";
} done | sort -k2 -rn
### Check for identifier and create if missing and requested
echo
ID=$(cat "$SRC/id.txt" 2>/dev/null)
if [ "$ID" == "" ]; then
echo "[error] No ID on card (at $SRC/id.txt) detected"
read -p "Create? [y/N]: " CREATE
if [ "$CREATE" == "y" -o "$CREATE" == "Y" ]; then
ID=$(python -c "import uuid; print str(uuid.uuid1()).upper()")
echo "echo $ID | tee \"$SRC/id.txt\""
exit 0
else
echo "Aborting"
exit 1
fi
fi
echo "Source ID $ID detected"
##echo rsync -avn --filter="+ DCIM/" --filter="+ DCIM/**" --filter="- *" /Volumes/Untitled/ "/Volumes/SD Backup/Backup/EA2E5044-EB98-43E8-B251-98C5ED0B484B"
#rsync -avn --filter="+ DCIM/" --filter="+ DCIM/**" --filter="- *" $SRCDATA "$DEST/$ID"
### Copy data
echo
echo "Starting rsync"
echo "Command:"
echo "rsync -avn --progress \"$SRCDATA/\" \"$DEST/$ID\""
rsync -avn --progress "$SRCDATA/" "$DEST/$ID"
# TODO: Detect state where no data needs to be copied
# Show potential differences
echo
echo -n "Source size (MB): "
du -sm "$SRCDATA"
echo -n "Destination size (MB): "
if [ ! -d "$DEST/$ID" ]; then
echo "0 $DEST/$ID"
else
du -sm "$DEST/$ID"
fi
# This can be improved
if [ "$BACKUP" == "y" -o "$BACKUP" == "Y" ]; then
do_backup "$SRCDATA/" "$DEST/$ID"
else
read -p "Perform backup? [y/N]: " BACKUP
if [ "$BACKUP" == "y" -o "$BACKUP" == "Y" ]; then
do_backup "$SRCDATA/" "$DEST/$ID"
else
exit 0
fi
fi
### Calculate checksums of new files
echo
echo "Calculating checksums of new files..."
# Change to our target directory to make this easier
cd "$DEST/$ID"
if [ "$?" -ne 0 ]; then
echo "[error] unable to change to target directory \"$DEST/$ID\""
fi
# Iterate through $DEST and for each file grep checksum file
# Calculate any that are missing (eg: newly copied data)
# Inefficient, but simple method
# TODO: Some sort of progress, eg: [current file #/total file #]
rm md5sum.new 2>/dev/null
for f in `/bin/ls`;
do {
if [ "$f" == "md5sum.txt" ]; then
continue
elif [[ "$f" == *"xmp" ]]; then
continue
fi
MD5=$(grep $f md5sum.txt 2>/dev/null)
if [ "$MD5" == "" ]; then
# TODO: Provide count + total for progress
echo "Checksum for $f missing, calculating"
md5sum "$f" >> md5sum.new
fi
} done
# If not commited, no automatic way to recalculate
# That functionality could be added as a different
# mode of invocation.
read -p "Append new checksums to md5sum.txt? [y/N]: " COMMIT_MD5
if [ "$COMMIT_MD5" == "y" -o "$COMMIT_MD5" == "Y" ]; then
cat md5sum.new >> md5sum.txt
rm md5sum.new 2>/dev/null
else
echo "Skipping modification of md5sum.txt"
echo "Calculated new file checksums stored in md5sum.new"
fi
# TODO: Calculate checksums of original files & verify
echo "Files synced to $DEST/$ID"
# Fin #
exit 0