Sharing my hourly beets cron script

Hey, just in case someone finds it useful, I’m sharing my hourly cron script, it’s not beautiful as I have no idea what I’m doing, but it does the trick. A small explanation first:

1: Create lockfile. When beets stalls or during a particularly large import I don’t want beets running when the next hour comes around.
2: Use rsync & ssh to move only new albums from server 1 to server 2. Uses an --exclude list from previous imports so it only grabs new music, then writes what was transferred to the sync log for future excluding. This could be done with other sync software, however this is more flexible and works well.
3: remove the ‘[’ character from filenames written to the logfile. [ is used by rsync and messes with the —exclude flag.
4: Run a quiet import with beets
5: Delete anything left in the sync folder which wasn’t moved by beets.

#!/bin/bash
echo "Create Lock"
# SPDX-License-Identifier: MIT
## Copyright (C) 2009 Przemyslaw Pawelczyk <przemoc@gmail.com>
##
## This script is licensed under the terms of the MIT license.
## https://opensource.org/licenses/MIT
#
# Lockable script boilerplate
### HEADER ###
# "/var/lock/
# LOCKFILE="/mnt/user/sync/`basename $0`"
LOCKFILE="/mnt/user/sync/sync-locker"
LOCKFD=99
 
# PRIVATE
_lock()             { flock -$1 $LOCKFD; }
_no_more_locking()  { _lock u; _lock xn && rm -f $LOCKFILE; }
_prepare_locking()  { eval "exec $LOCKFD>\"$LOCKFILE\""; trap _no_more_locking EXIT; }
 
# ON START
_prepare_locking
 
# PUBLIC
exlock_now()        { _lock xn; }  # obtain an exclusive lock immediately or fail
exlock()            { _lock x; }   # obtain an exclusive lock
shlock()            { _lock s; }   # obtain a shared lock
unlock()            { _lock u; }   # drop a lock
 
### BEGIN OF SCRIPT ###
 
# Simplest example is avoiding running multiple instances of script.
exlock_now || exit 1
 
# Remember! Lock file is removed when one of the scripts exits and it is
#           the only script holding the lock or lock is not acquired at all.
################################

env LC_ALL=en_US.UTF-8 rsync -vrzs \
--exclude-from=/mnt/user/sync/music-myartists/__sync-log \
-e ssh \
xxx@xxx.xx:/home/xxx/downloads/deluge-filters/my-artists/ /mnt/user/sync/music-myartists/ \
| grep -v ^sending \
| grep -v ^sent \
| grep -v ^total \
| grep -v 'receiving incremental file list' \
>> /mnt/user/sync/music-myartists/__sync-log
sed -i 's/\[/\*/g' /mnt/user/sync/music-myartists/__sync-log

docker exec beets beet -c /config/config-move-quiet.yaml import /sync/music-myartists

find /mnt/user/sync/music-myartists/* -not -name '__sync-log' -delete

echo "All done, thanks!"
4 Likes