Created
January 9, 2014 13:15
-
-
Save pilhokim/8333956 to your computer and use it in GitHub Desktop.
This watches the changes when USB inserting event happens. To unify the USB media name, sudo edit /etc/fsab to insert
/dev/usbkey /mnt/usb vfat ro,noauto,user,exec 0 0 Within the content, mail-usbread.py should be additionally downloaded from my account.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# RPI USB stick backup script running on USB insert event | |
# by Pil Ho Kim, pilhokim@gmail.com | |
# Revision History | |
# January 2nd, 2014: Modified from the old USB link insert detection version | |
# Refereces | |
# USB stick insertion detection: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=32&t=41056 | |
# RPI on-bard LED control: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=31&t=12530 | |
# RPi Text to Speech: http://elinux.org/RPi_Text_to_Speech_(Speech_Synthesis) | |
# Prepare directory names | |
# Source USB directory. To unify the USB media name, sudo edit /etc/fsab to insert | |
# /dev/usbkey /mnt/usb vfat ro,noauto,user,exec 0 0 | |
sourceUSBDir="/mnt/usb" | |
# Record the USB copying process as the directory | |
watchUSBDir="/mnt/familylifelog/Media/RpiUSB" | |
# Backup USB contents through FTP | |
sourceFTPIPAddress="ftp_ip_address" | |
sourceFTPUser="user_name" | |
sourceFTPPassword="user_password" | |
# Below should be different by a user. May later extend to support multiple users getting user info through the shell argument | |
backupHomeFTPDir="/Public/Documents/www/data/elog/people/elog_pilhokim/RpiUSB" | |
# This directory is the mount directory that matches with the above | |
backupHomeDir="/mnt/familylifelog/Documents/www/data/elog/people/elog_pilhokim/RpiUSB" | |
# Check date time | |
sourceUSBName=$sourceUSBDir | |
sourceUSBDate=$(date +'%Y-%m-%d_%H-%M-%S') | |
if [ -z "$sourceUSBName" ]; then | |
echo "No new media is inserted." | |
exit | |
fi | |
# Get directory names. These directories should be priori created. | |
sourceProgressUSBDir="$watchUSBDir/progress" | |
sourceCompletedUSBDir="$watchUSBDir/completed" | |
sourceFailedUSBDir="$watchUSBDir/failed" | |
# Check link existence | |
# progressCounter=$(dir -1 $sourceProgressUSBDir/$sourceUSBDate | wc -l) | |
if [ -d "$sourceProgressUSBDir/$sourceUSBDate" ]; then | |
echo "Copying is under progress." | |
exit | |
fi | |
# completedCounter=$(dir -1 $sourceCompletedUSBDir/$sourceUSBDate | wc -l) | |
if [ -d "$sourceCompletedUSBDir/$sourceUSBDate" ]; then | |
echo "This media is alread copied." | |
exit | |
fi | |
# failedCounter=$(dir -1 $sourceFailedUSBDir/$sourceUSBDate | wc -l) | |
if [ -d "$sourceFailedUSBDir/$sourceUSBDate" ]; then | |
echo "This media copy has alread failed" | |
exit | |
fi | |
# Log the insertion event | |
# Check espeak installation | |
PACKAGESTATUS=`dpkg -s espeak | grep Status`; | |
if [[ $PACKAGESTATUS == S* ]] | |
then | |
echo "Package 'espeak' is installed." | |
else | |
echo "Package 'espeak' is NOT installed." | |
echo "Installing package 'espeak'. Please wait..." | |
apt-get -y install espeak | |
fi | |
# Notice via Speaker if attached | |
espeak -ven+f3 -k5 -s150 "A new USB drive is connected." 2> /dev/null | |
# Mark progress | |
mkdir $sourceProgressUSBDir/$sourceUSBDate 2> /dev/null | |
mkdir $sourceProgressUSBDir/$sourceUSBDate/$sourceUSBName 2> /dev/null | |
# Perform copy | |
# Check source data size | |
sourceSize=$(du -s $sourceUSBDir | sed -e 's/\s.*$//') | |
if [ $sourceSize -le 8192 ]; then | |
echo "Mounting a new USB link $sourceUSBDir/$sourceUSBName has failed in initialization or it contains no data: $sourceSize" | |
espeak -ven+f3 -k5 -s150 "The shared network link failes in initialization or it contains no data: $sourceSize" 2> /dev/null | |
mv -f $sourceProgressUSBDir/$sourceUSBDate $sourceFailedUSBDir/ 2> /dev/null | |
logger Mounting a new USB link $sourceUSBDir/$sourceUSBName has failed in initialization or it contains no data: $sourceSize | |
exit | |
fi | |
# Check the date | |
backupDir=$backupHomeDir/$sourceUSBDate | |
backupFTPDir=$backupHomeFTPDir/$sourceUSBDate | |
# Report the progress by email | |
reportStart="Start importing data $sourceSize to $backupDir" | |
python /home/pi/Documents/Software/rpiips/mail-usbread.py -m "$reportStart" 2> /dev/null | |
logger Start backing up USB data $sourceSize | |
echo "Start copying source data $sourceSize to $backupDir." | |
# espeak -ven+f3 -k5 -s150 "Start reading source data: $sourceSize" 2> /dev/null | |
logger Start copying source data $sourceSize to $backupDir. | |
mkdir $backupDir 2> /dev/null | |
# cp -R -f $sourceSMBLink/* $backupDir 2> /dev/null | |
# cp -R -f $sourceSMBLink/* $backupDir 2> /dev/null | |
# rsync -ravcq $sourceSMBLink/* $backupDir 2> /dev/null | |
# Check espeak installation | |
PACKAGESTATUS=`dpkg -s ncftp | grep Status`; | |
if [[ $PACKAGESTATUS == S* ]] | |
then | |
echo "Package 'ncftp' is installed." | |
else | |
echo "Package 'ncftp' is NOT installed." | |
echo "Installing package 'ncftp'. Please wait..." | |
apt-get -y install ncftp | |
fi | |
ncftpput -R -u $sourceFTPUser -p $sourceFTPPassword $sourceFTPIPAddress $backupFTPDir/ $sourceUSBDir/* | |
# Check target size | |
echo "Finished copying data. Checking copyied data size." | |
logger Finished copying data. Checking copyied data size. | |
targetSize=$(du -s $backupDir | sed -e 's/\s.*$//') | |
# Compose the result | |
reportEnd="[$backupDay] Source: $sourceSize, Target: $targetSize, TargetDir: $backupDir" | |
# Report the progress by email | |
# Exceprted from https://github.com/repoman/rpiips | |
python /home/pi/Documents/Software/rpiips/mail-usbread.py -m "$reportEnd" 2> /dev/null | |
if [ "$sourceSize"=="$targetSize" ]; then | |
echo "Successfully copied." | |
mv -f $sourceProgressUSBDir/$sourceUSBDate $sourceCompletedUSBDir/ 2> /dev/null | |
logger Successfully copied. | |
else | |
echo "Copied size mismatched." | |
mv -f $sourceProgressUSBDir/$sourceUSBDate $sourceFailedUSBDir/ 2> /dev/null | |
logger Copied size mismatch | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment