The script:
Code:
#!/bin/sh
# $Id: USB-rsync.sh,v 1.5 2013/06/30 18:11:05 adriaan Exp $
# Copyright (c) 2013 J65nko - administrator daemonforums.org
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
USB_STICK=/dev/sd0a
LOGFILE=/var/log/USB-rsync.log
LOGFILE_DRY=/var/log/USB-rsync_dry_run.log
# --- directory to exclude from rsync:
FIREFOX_CACHE='.mozilla/firefox/chmj5t7f.default/Cache'
# ---- source and destination for rysnc
# --- this will create a directory 'adriaan' on '/mnt'
SOURCE_DIR=/home/adriaan
MOUNT_DIR=/mnt
# alternative specification with identical result
# SOURCE_DIR=/home/adriaan/
# MOUNT_DIR=/mnt/adriaan
# here no directory will not be created, giving you the choice to
# specify an alternative destination directory e.g. :
# MOUNT_DIR=/mnt/j65nko
# Need root privileges to mount stick on '/mnt'
# and to create logfile in '/var/log'
if [ "$(id -u)" -ne 0 ]; then
echo $0 : you are not ROOT !!
echo $0 : aborting .......
exit 1
fi
# ---
echo deleting old .serverauth files
find ${SOURCE_DIR} -name '.serverauth*' -maxdepth 1 ! -ctime 1 -ls
find ${SOURCE_DIR} -name '.serverauth*' -maxdepth 1 ! -ctime 1 -exec rm {} \;
# ---
echo Mounting USB stick .......
if mount -o noatime,softdep ${USB_STICK} ${MOUNT_DIR} ; then
echo USB has been been mounted
df -h
echo ----------
else
echo Could not mount USB stick!
exit 2
fi
# -------- rsync options
# -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
# -v, --verbose increase verbosity
# -z, --compress compress file data during the transfer
# -n, --dry-run perform a trial run with no changes made
# --delete delete extraneous files from dest dirs
# --delete-excluded also delete excluded files from dest dirs
echo ----- dry-run
rsync -avz \
--delete \
--exclude=${FIREFOX_CACHE} \
--dry-run \
${SOURCE_DIR} \
${MOUNT_DIR} 2>&1 | tee ${LOGFILE_DRY} | less
printf "Do you want to perform the actual, real rsync now? (Y/N): "
read ANSWER
case ${ANSWER} in
[Yy] ) echo OK doing the rsync .....
;;
* ) echo "$0: You decided not to do the actual rsync from ${SOURCE_DIR} to ${MOUNT_DIR}"
exit 3
;;
esac
# ---- perform the real rsync
rsync -avz \
--delete \
--exclude=${FIREFOX_CACHE} \
${SOURCE_DIR} \
${MOUNT_DIR} 2>&1 | tee ${LOGFILE}
# ----- message to user
cat <<END
$0 : has finished
=======================================================
$(df -h)
The log file of the dry run : ${LOGFILE_DRY}
The log file of the actual run : ${LOGFILE}
Please do not forget to umount your USB stick .........
=======================================================
END
# ---- end of script ---
Please use the link to download the script