How to automatically synchronize your sdcards when the device is plugged

  • Don't use the following scripts "as is": they are just models you need to adapt to your configuration. These models are provided without any guarantee, so don't blame me if you make (big) mistakes with !
  • We use here udev to call a first BASH script when the device is plugged on. This first script calls a background process with a 10s pause, which should be enough to let the system mounting the sdcards in the directory /media/. Then the rsync command is used to synchronize the datas between your SD cards and your target directory. Of course, when rsync is running, unplug the device could result in (very) bad results like any other USB key ! You have always to wait the final sound before unpluging your device.
	$ cat /etc/udev/rules.d/51-android.rules

# Please adapt your idVendor/idProduct according to the output of lsusb when the device is plugged.
# Mine is a Geeksphone Peak (05c6:8013).

SUBSYSTEM=="usb", ATTR{idVendor}=="05c6", ATTR{idProduct}=="8013", MODE="0666", GROUP="plugdev"
ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="05c6", ATTR{idProduct}=="8013", RUN+="/home/myuser/backup/firefoxos/sdcards/backup1.sh"
	
	$ cat backup1.sh

#!/bin/bash

/home/myuser/backup/firefoxos/sdcards/backup2.sh &
exit 0
	
	$ cat backup2.sh

#!/bin/bash

PATH=/bin:/usr/bin
NOW=$(date +"%Y%m%d")

BACKUPDIR="/home/myuser/backup/firefoxos/sdcards/"

# Please adapt you card model here
MOUNTPOINT1="/media/Internal SD"
SD1="${BACKUPDIR}/Internal SD"

# Please adapt you card model here
MOUNTPOINT2="/media/Kingston"
SD2="${BACKUPDIR}/Kingston"

sleep 10

mountpoint -q "${MOUNTPOINT1}"
if [ $? -eq 0 ]; then
	if [ ! -d "${SD1}" ]; then
		echo mkdir -p "\"${SD1}\""
	fi
	if [ -d "${SD1}" ]; then
		rsync -a "${MOUNTPOINT1}"/* "${SD1}"/
		logger "Synchro GP Peak ${MOUNTPOINT1} finished ! ${NOW}"
		aplay -q /usr/share/sounds/pop.wav
	fi
fi

mountpoint -q "${MOUNTPOINT2}"
if [ $? -eq 0 ]; then
	if [ ! -d "${SD2}" ]; then
		echo mkdir -p "\"${SD2}\""
	fi
	if [ -d "${SD2}" ]; then
		rsync -a "${MOUNTPOINT2}"/* "${SD2}"/
		logger "Synchro GP Peak ${MOUNTPOINT2} finished ! ${NOW}"
		aplay -q /usr/share/sounds/pop.wav
	fi
fi

# choose your final sound !
aplay -q /usr/share/sounds/k3b_success1.wav

exit 0
	

Version notes

  • 0.1
    • Initial public release.