Skip to content

Instantly share code, notes, and snippets.

@theHilikus
Last active November 22, 2023 09:33
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 theHilikus/238597e4e725fddbe10b to your computer and use it in GitHub Desktop.
Save theHilikus/238597e4e725fddbe10b to your computer and use it in GitHub Desktop.

I wanted to do an automatic sync of a directory in my hard drive with 1 directory in my mp3 player (MSU Enabled) To launch an action on detecting it

I used the udev system by creating a rule under /etc/udev/rules.d/10-local.rules

SUBSYSTEMS=="scsi", ATTRS{model}=="MK3006GAL       ", SYMLINK+="DAP", MODE="0666"
SUBSYSTEMS=="scsi", ATTRS{model}=="MK3006GAL       ", ACTION=="add", RUN+="/usr/local/bin/music-sync %k"

The first line creates a symbolic link called DAP under /dev/ \ The second line runs a script I wrote to sync (and mount if required) the device

#!/bin/bash
# Author: Alejandro Endo
### Variables ###
LABEL="smartphone"; #directory inside /media where the device is mounted
DAP_DIR_TO_SYNC="/music/"; #dir in the device
LOCAL_DIR_TO_SYNC="/mnt/Jukebox/"; #dir in the local computer (should end in '/')"
LOG_FILE="/tmp/rsync.log"
### Code ###
print () {
if [ "$broad" = 1 ];then
echo -e "$1" | wall
# echo -e $1
else
#echo -e \[`date +"%F %r"`\] "$1"
echo -e "$1"
fi
}
usage () {
print "Usage: $0 [--dry-run] [--broadcast] <device under /dev/>"
exit 1
}
output=""
#Parse arguments
until [ -z "$2" ] # Until all parameters used up . . .
do
case "$1" in
--broadcast)
broad=1;;
--dry-run)
dryRun="--dry-run";;
-*)
usage;;
esac
shift
done
DEVICE="$1"
if [ "$DEVICE" = "" ]; then
print "Missing dev parameter"
usage
fi
if [ ! -b "/dev/$DEVICE" ]; then
print "Device not found: $DEVICE\n"
usage
fi
#Mount the device
print "Determining if we need to mount...\n"
count=`ls /media/${LABEL} | wc -l`
if [ "$count" -ne 0 ]; then
#exists already
print "Device mounted already\n"
else
print "Mounting: $DEVICE\n"
pmount --type vfat --umask 022 --sync "$DEVICE" "$LABEL"
count=`ls /media/${LABEL} | wc -l`
if [ "$count" -eq 0 ]; then
print "Mount Failed\n"
exit 1
fi
fi
#Autosync
print "\nSync...\n"
rsync $dryRun --delete --verbose --times --recursive --stats --human-readable --progress --log-file="$LOG_FILE" --modify-window=3601 "$LOCAL_DIR_TO_SYNC" "/media/${LABEL}${DAP_DIR_TO_SYNC}" #| tee -a "$LOG_FILE"
#echo -e "$?\n$output\n" >> "$LOG_FILE"
#echo -e `ls /media/${LABEL}` >> "$LOG_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment