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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
#!/bin/bash
#
# Sync media assets.
#
# Load
source $APP_BASE/lib/hydra/functions || exit 1
hydra_config_load
# Parameters
REMOTE="$1"
REPOSITORY="$2"
VOLUME="/media/$REMOTE"
DOMAIN="`facter domain`"
HOST="`facter hostname`"
CACHE="/var/cache/$HOST/media"
MEDIA="media.$DOMAIN"
INCOMING="$CACHE/incoming"
WHOAMI="`whoami`"
LOCK=".sync-media.lock"
OPTIONS="$*"
# Dependencies
hydra_install_package rsync
hydra_install_package unison
hydra_install_package git-annex
# Fatal error
# Adapted from borger
function fatal {
info [fatal] $*
exit 1;
}
# Create lockfile
# Adapted from borger
function sync_media_set_lockfile {
if [ ! -z "$LOCKFILE" ]; then
mkdir -p `dirname $LOCKFILE`
if ( set -o noclobber; echo "$$" > "$LOCKFILE" ) &> /dev/null; then
trap 'sync_media_unset_lockfile' INT TERM EXIT
else
fatal "Could not create lockfile $LOCKFILE, exiting"
fi
fi
}
# Remove lockfile
# Adapted from borger
function sync_media_unset_lockfile {
if [ ! -z "$LOCKFILE" ] && [ -e "$LOCKFILE" ]; then
git annex unannex $LOCK &> /dev/null
git rm -f $LOCKFILE &> /dev/null || \
rm -f $LOCKFILE || echo "Could not remove lockfile $LOCKFILE"
fi
}
# Check lockfile
# Adapted from borger
function sync_media_check_lockfile {
local pid process
if [ ! -e ".gitignore" ] || ! grep -q "$LOCK" .gitignore; then
# With git-ignore from git-extras package
#git ignore $LOCK
# Without git-ignore from git-extras package
echo "$LOCK" >> .gitignore
fi
if [ ! -z "$LOCKFILE" ] && [ -f "$LOCKFILE" ]; then
pid="`cat $LOCKFILE`"
process="`ps --no-headers -o comm $pid`"
if [ "$?" == "0" ] && [ "`ps --no-headers -o comm $$`" == "$process" ]; then
echo "Another program is running for $LOCKFILE, skipping"
return 1
else
echo "Found old lockfile $LOCKFILE, removing it"
sync_media_unset_lockfile
fi
return 0
fi
}
# Fix identity
function sync_media_identity {
if [ -z "`git config --local user.email`" ] || [ -z "`git config --local user.name`" ]; then
repo="$(basename `pwd`)"
git config user.name "${repo^} Archive"
git config user.email "$repo@localhost"
fi
}
# Add files into the annex
function sync_media_add {
git annex add .
# Handle playlists which are generally not managed by git-annex
if [ -d "playlists" ]; then
git add playlists
fi
# Adds hidden files and symlinks, git+while version
git status --porcelain -u | grep '^?? ' | sed -e 's/?? //' | while read file; do
if [ -h "$file" ]; then
git add "$file"
else
git annex add "$file"
fi
done
}
# Add meta files, making sure they're handled directly by Git
function sync_media_add_metadata {
# Metadata state on the annex
#
# * add: metadata is managed in the annex, normally.
# * unlock: managed in the annex, but modifications are tracked as well.
# * unannex: metadata is kept out of the annex, but still tracked by Git.
#
# Usually, they should be regular files trackes by Git, so `git diff`
# works as expected.
#
#metadata_state="add"
#metadata_state="unlock"
metadata_state="unannex"
# Gitignore
if [ -e ".gitignore" ]; then
git annex $metadata_state .gitignore
git add .gitignore
fi
# Playlist files in the playlist folder
if [ -d "playlists" ]; then
find playlists -name '*.m3u' -type l -exec git annex $metadata_state {} \;
find playlists -name '*.m3u' -exec git add {} \;
find playlists -name '*.m3u8' -type l -exec git annex $metadata_state {} \;
find playlists -name '*.m3u8' -exec git add {} \;
fi
# Koreader metadata files
find -name metadata.pdf.lua -type l -exec git annex $metadata_state {} \;
find -name metadata.pdf.lua -type l -exec git annex $metadata_state {} \;
find -name metadata.pdf.lua -exec git add {} \;
find -name metadata.pdf.lua.old -type l -exec git annex $metadata_state {} \;
find -name metadata.pdf.lua.old -exec git add {} \;
# Darktable sidecar files
find -name '*.xmp' -type l -not -path '*.git*' -exec git annex $metadata_state {} \;
find -name '*.xmp' -not -path '*.git*' -exec git add {} \;
}
# If there is a playlists folder, make sure mpd user can write to it
function sync_media_playlist_perms {
if [ -d "playlists" ]; then
$sudo chmod 775 playlists
$sudo chown -R mpd:audio playlists
find playlists -type f -exec sudo chmod 664 {} \;
find playlists -type d -exec sudo chmod 775 {} \;
fi
}
# Fix incoming permissions
function sync_media_incoming_perms {
if [ -d "$INCOMING" ]; then
echo "Fixing $INCOMING permissions..."
$sudo find $INCOMING -type f -exec chmod 664 {} \;
$sudo find $INCOMING -type d -exec chmod 775 {} \;
$sudo chown -R $WHOAMI:incoming $INCOMING
fi
}
# Run fsck
function sync_media_fsck {
if [ "$FSCK" == "true" ]; then
git annex fsck --fast
fi
}
# Run dropunused
function sync_media_dropunused {
if [ "$DROPUNUSED" == "true" ]; then
git annex unused
git annex dropunused 1-1000
fi
}
# Get copies of annexed files
function sync_media_get {
local repo="$1"
local numcopies
if [ "`git -C $repo config sync-media.get`" != "false" ]; then
if git -C $repo config sync-media.numcopies &> /dev/null; then
numcopies="`git -C $repo config sync-media.numcopies`"
else
numcopies="3"
fi
git annex get . --numcopies=$numcopies
fi
}
# Control whether the repository should have a copy of everything
function sync_media_getall {
local repo="$1"
if [ "`git -C $repo config sync-media.getall`" == "true" ]; then
git annex get .
fi
}
# Ensure we have a reference to the remote repository
function sync_media_ensure_remote {
local remote="$1"
local path="$2"
if [ -z "$remote" ] || [ "$remote" == "local" ] || [ "$remote" == "localhost" ] || [ -z "$path" ]; then
return
fi
# Check for local or remote repo
if [ -z "$DRIVE" ]; then
path="$REMOTE.$DOMAIN:$path"
elif [ ! -d "$path/.git/annex" ]; then
return
fi
if ! git remote | grep -q "^$remote$"; then
git remote add $remote $path
fi
}
# Set sudo config
if [ "$WHOAMI" != 'root' ]; then
sudo="sudo"
else
echo "Sorry, cannot run as root, since archives are usually user-managed"
exit 1
fi
# Set fsck config
if echo $OPTIONS | grep -q -- "--fsck"; then
FSCK="true"
fi
# Set unused config
if echo $OPTIONS | grep -q -- "--dropunused"; then
DROPUNUSED="true"
fi
# Set drive config
# Ignore drive/volume if it's set to "local"
if [ ! -z "$REMOTE" ] && [ "$REMOTE" != "local" ] && [ "$REMOTE" != "localhost" ]; then
# Check storage media
MOUNT="`mount | grep $VOLUME`"
if [ ! -z "$MOUNT" ]; then
DRIVE="$(basename `echo $MOUNT | awk '{ print $1 }'`)"
#else
# echo "$BASENAME: volume $MEDIA is not mounted"
# exit 1
fi
fi
# Ensure cache exists
mkdir -p $CACHE
# Fix cache permissions
#echo "Fixing $CACHE permissions..."
#$sudo find $CACHE -type f -exec chmod 644 {} \;
#$sudo find $CACHE -type d -exec chmod 755 {} \;
$sudo chown $WHOAMI: $CACHE
$sudo chown $WHOAMI: $CACHE/*
# Check if a specific repository was passed via the command line
if [ ! -z "$REPOSITORY" ] && [ -d "$CACHE/$REPOSITORY" ] && ! echo "$REPOSITORY" | grep -q -- '--'; then
REPOSITORIES="$REPOSITORY"
else
REPOSITORIES="`ls $CACHE`"
fi
if echo $REPOSITORIES | grep -q "incoming"; then
sync_media_incoming_perms
fi
# Iterate over existing repositories in the local cache
for folder in $REPOSITORIES; do
LOCKFILE="$CACHE/$folder/$LOCK"
# Sync each repository in the local cache
if [ -d "$CACHE/$folder/.git/annex" ]; then
if [ "`git -C $CACHE/$folder config sync-media.skip`" == "true" ]; then
continue
fi
(
cd $CACHE/$folder
echo ""
echo "Syncing $CACHE/$folder..."
echo ""
# Lockfile handling
if ! sync_media_check_lockfile; then
continue
else
sync_media_set_lockfile
fi
# Ensure the removable volume is in the list of remotes
sync_media_ensure_remote $REMOTE $VOLUME/$MEDIA/$folder
# Ensure the repository is identified
sync_media_identity
# Disable automatic syncing of content
git config annex.synccontent false
# Sync before changing anything, to make sure the history does not have
# conflicts with other remotes
git annex sync --no-content
# Fix any playlist permissions
sync_media_playlist_perms
# Process metadata
sync_media_add_metadata
# Add new content, and catch up changes
sync_media_add
# Sync everything again
git annex sync --no-content
# Get all that needs to be got
sync_media_getall $CACHE/$folder
# Repository maintenance
sync_media_fsck
sync_media_dropunused
git prune
git gc
# Unset the lockfile
sync_media_unset_lockfile
)
fi
# Ensure the removable media has each repository listed in the local cache
if [ ! -z "$DRIVE" ]; then
mkdir -p $VOLUME/$MEDIA
if [ -d "$CACHE/$folder/.git" ]; then
if [ ! -d "$VOLUME/$MEDIA/$folder" ]; then
(
cd $VOLUME/$MEDIA
echo "Initializing $VOLUME/$MEDIA/$folder..."
git clone $CACHE/$folder && cd $folder && sync_media_identity && git remote rename origin $HOST
cd $CACHE/$folder && git remote add $DRIVE $VOLUME/$MEDIA/$folder
)
if [ -d "$CACHE/$folder/.git/annex" ]; then
(
cd $VOLUME/$MEDIA/$folder
git annex init $DRIVE && git config sync-media.getall true
)
fi
fi
elif [ ! -d "$VOLUME/$MEDIA/$folder" ]; then
mkdir -p $VOLUME/$MEDIA/$folder
# Set timestamp old enough to help unison guess which copy is newer
touch -t 197001010000 $VOLUME/$MEDIA/$folder
fi
fi
done
# Process removable or remote media
if [ ! -z "$DRIVE" ] && [ -d "$VOLUME/$MEDIA" ]; then
# Check if a specific repository was passed via the command line
if [ ! -z "$REPOSITORY" ] && [ -d "$VOLUME/$MEDIA/$REPOSITORY" ] && ! echo "$REPOSITORY" | grep -q -- '--'; then
REPOSITORIES="$REPOSITORY"
else
REPOSITORIES="`ls $VOLUME/$MEDIA`"
fi
# Iterate over existing repositories in the removable media
for folder in $REPOSITORIES; do
LOCKFILE="$VOLUME/$MEDIA/$folder/$LOCK"
# Sync each local repository in the removable media
if [ -d "$VOLUME/$MEDIA/$folder/.git/annex" ]; then
if [ "`git -C $VOLUME/$MEDIA/$folder config sync-media.skip`" == "true" ]; then
continue
fi
(
cd $VOLUME/$MEDIA/$folder
echo ""
echo "Syncing $VOLUME/$MEDIA/$folder..."
echo ""
# Lockfile handling
if ! sync_media_check_lockfile; then
continue
else
sync_media_set_lockfile
fi
sync_media_playlist_perms
sync_media_ensure_remote $HOST $CACHE/$folder
sync_media_identity
sync_media_add
# Disable automatic syncing of content
git config annex.synccontent false
git annex sync --no-content
sync_media_get $VOLUME/$MEDIA/$folder
sync_media_getall $VOLUME/$MEDIA/$folder
sync_media_fsck
sync_media_dropunused
git gc
git prune
#git annex drop --auto --numcopies=2
# Unset the lockfile
sync_media_unset_lockfile
)
elif [ -d "$CACHE/$folder" ] && [ ! -d "$CACHE/$folder/.git" ]; then
# Avoid those configured to be skipped
if [ ! -e "$CACHE/$folder/.sync-media/skip" ]; then
echo "Syncing $CACHE/$folder with $VOLUME/$MEDIA/$folder..."
if [ -e "$CACHE/$folder/.sync-media/method" ]; then
method="`cat $CACHE/$folder/.sync-media/method`"
else
method="unison"
fi
if [ "$method" == "unison" ]; then
unison $CACHE/$folder $VOLUME/$MEDIA/$folder -auto -logfile /dev/null
elif [ "$method" == "rsync-to-media-volume" ]; then
echo "Syncing $CACHE/$folder into $VOLUME/$MEDIA/$folder..."
rsync -av --delete --exclude=.sync-media $CACHE/$folder/ $VOLUME/$MEDIA/$folder/
elif [ "$method" == "rsync-from-media-volume" ]; then
echo "Syncing $VOLUME/$MEDIA/$folder into $CACHE/$folder..."
rsync -av --delete --exclude=.sync-media $VOLUME/$MEDIA/$folder/ $CACHE/$folder/
elif [ "$method" == "rsync-if-empty-dest" ]; then
# Ensure both endpoint folders exist
mkdir -p $CACHE/folder
mkdir -p $VOLUME/$MEDIA/$folder
# One way rsync method, from the non-empty to the empty folder
if [ ! -z "`ls -1 $CACHE/$folder`" ] && [ -z "`ls -1 $VOLUME/$MEDIA/$folder`" ]; then
echo "Syncing $CACHE/$folder into $VOLUME/$MEDIA/$folder..."
rsync -av --delete --exclude=.sync-media $CACHE/$folder/ $VOLUME/$MEDIA/$folder/
elif [ ! -z "`ls -1 $VOLUME/$MEDIA/$folder`" ] && [ -z "`ls $CACHE/$folder`" ]; then
echo "Syncing $VOLUME/$MEDIA/$folder into $CACHE/$folder..."
rsync -av --delete --exclude=.sync-media $VOLUME/$MEDIA/$folder/ $CACHE/$folder/
else
echo "Skipping rsyncing between $CACHE/$folder and $VOLUME/$MEDIA/$folder since both are non-empty"
fi
elif [ "$method" == "" ]; then
true
else
echo "Skipping unknown sync method $method"
fi
fi
fi
# Run an additional custom synchronizer
if [ -x "$CACHE/$folder/.sync-media/custom" ]; then
$CACHE/$folder/.sync-media/custom $VOLUME/$MEDIA/$folder
fi
# Ensure the local cache has each repository listed in the removable media
if [ ! -d "$CACHE/$folder" ]; then
if [ -d "$VOLUME/$MEDIA/$folder/.git" ]; then
(
cd $CACHE
echo "Initializing $CACHE/$folder..."
git clone $VOLUME/$MEDIA/$folder && cd $folder && sync_media_identity && git remote rename origin $REMOTE
cd $VOLUME/$MEDIA/$folder && git remote add $HOST $CACHE/$folder
)
if [ -d "$VOLUME/$MEDIA/$folder/.git/annex" ]; then
(
cd $CACHE/$folder
git annex init $HOST
)
fi
else
echo "Syncing $VOLUME/$MEDIA/$folder into $CACHE/$folder..."
rsync -av --delete --exclude=.sync-media $VOLUME/$MEDIA/$folder/ $CACHE/$folder/
fi
fi
done
elif [ ! -z "$REMOTE" ] && [ "$REMOTE" != "local" ] && [ "$REMOTE" != "localhost" ]; then
# Try to copy to a remote
for folder in `ls $CACHE`; do
if [ -d "$CACHE/$folder/.git/annex" ]; then
if [ "`git -C $CACHE/$folder config sync-media.skip`" == "true" ]; then
continue
fi
sync_media_ensure_remote $REMOTE $CACHE/$folder
(
cd $CACHE/$folder
git annex copy . --to $REMOTE
git annex sync --no-content
)
else
echo "Syncing $CACHE/$folder with ssh://$REMOTE.$DOMAIN/$CACHE/$folder..."
unison $CACHE/$folder ssh://$REMOTE.$DOMAIN/$CACHE/$folder/ -auto -logfile /dev/null
# Avoid empty source folders or those configured to be skipped
#if [ ! -e "$CACHE/$folder/.sync-media/skip" ] && [ ! -z "`ls -1 $CACHE/$folder`" ]; then
# echo "Syncing $CACHE/$folder into $REMOTE.$DOMAIN:$CACHE/$folder..."
# rsync -av --delete --exclude=.sync-media $CACHE/$folder/ $REMOTE.$DOMAIN:$CACHE/$folder/
#fi
fi
done
fi
|