Skip to content

Instantly share code, notes, and snippets.

@sayoder
Last active January 28, 2024 03:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sayoder/f93179e17c4046fd8c7810f41a7c4a85 to your computer and use it in GitHub Desktop.
Save sayoder/f93179e17c4046fd8c7810f41a7c4a85 to your computer and use it in GitHub Desktop.
rsync to Astell&Kern AK70 over mtp
#!/bin/bash
# Requirements: go-mtpfs, rsync, fuse2
MUSICDIR="/home/<USER>/Music/"
DEVICEMUSICDIR="/mnt/ak70/Internal storage/Music/"
MOUNTPOINT="/mnt/ak70/"
print_usage() {
echo "Usage: $0 mount|umount|drysync|fullsync|hardsync|sync"
}
if [ "$#" -ne 1 ]; then
print_usage
exit 1
fi
if [ "$1" == "mount" ]; then
sudo go-mtpfs "$MOUNTPOINT" &
exit 0
elif [ "$1" == "umount" ]; then
sudo fusermount -u "$MOUNTPOINT"
exit 0
fi
# normalize directories so nothing funky happens if someone sets the variables weird
normalize_directory() {
local dir="$1"
if [ -z "$dir" ]; then
echo "Set the variables in the script."
exit 1
fi
echo "$dir" | sed 's:/*$::' | sed 's:$:/:'
}
MUSICDIR=$(normalize_directory "$MUSICDIR")
DEVICEMUSICDIR=$(normalize_directory "$DEVICEMUSICDIR")
if sudo [ ! -d "$MUSICDIR" ]; then
echo "Music directory does not exist: $MUSICDIR"
exit 1
fi
if sudo [ ! -d "$DEVICEMUSICDIR" ]; then
echo "Mount point does not exist: $DEVICEMUSICDIR"
exit 1
fi
if [ "$DEVICEMUSICDIR" == "/" ]; then
echo "Sync target is the root directory. No."
exit 1
fi
if [ "$1" == "drysync" ]; then
sudo rsync -rvl --size-only --progress --dry-run "$MUSICDIR" "$DEVICEMUSICDIR"
elif [ "$1" == "fullsync" ]; then
# Sync everything, but no deletes.
sudo rsync -av --progress "$MUSICDIR" "$DEVICEMUSICDIR"
elif [ "$1" == "hardsync" ]; then
# Sync everything and delete files from the receiver if they're not on the sending end.
sudo rsync -av --delete --progress --dry-run "$MUSICDIR" "$DEVICEMUSICDIR"
read -p "The above is the output of a dry run. Files may be deleted from your device. Are you sure you want to do all that? [Y/n]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
sudo rsync -avz --delete --progress "$MUSICDIR" "$DEVICEMUSICDIR"
fi
elif [ "$1" == "sync" ]; then
# standard use case when adding new albums or correcting art or something.
# --size-only skips a lot of files. rsync's standard checksum method
# doesn't really work over mtp for some reason, so without --size-only it
# ends up re-syncing a lot of stuff.
sudo rsync -rvl --size-only --progress "$MUSICDIR" "$DEVICEMUSICDIR"
else
print_usage
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment