Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Mount / unmount external drive for Backup use' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Mount / unmount external drive for Backup use
Authored by: tbaur on Jun 07, '07 10:27:33PM
#!/bin/bash

# Copyright (c) 2007, panaso.com
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the  nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY panaso.com ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL panaso.com BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


# ==> Backup using rsync to defined mountpoint:/path
#     ...mount before backup, unmount when completed
#     version 0.9, this code is not supported.
#
# - create a .backup file on the destination (extra validation)
# - update the applicable variables: src/dst/name/log
# - adjust the rsync flags if needed
# - how logging is done could be more pretty, but it's all good
#
# run via crontab(1) as needed:
#     * 2 * * * /path/to/script


SRC=/Volumes/
DST=/Volumes/
NAME=
log=

# delete/comment out the following after updating variables
  exit 1


DEVICE=`diskutil list|grep $NAME|awk '{print $6}'`
PROC=`ps auwx | grep "rsync" | grep -v grep | wc -l`
TOP='1'

if test $PROC -lt $TOP ; then
	echo "=============================================" >> $log
	echo "=> Backup start: `date`" >> $log
	if [ -z $DEVICE ]; then
		echo "=> ERROR: Volume -> $NAME is NOT connected - exiting" >> $log
		exit 1
	else
		if [ ! -z `ls -1 /Volumes/ | grep $NAME` ] ; then
			echo "=> Volume -> $NAME is already mounted" >> $log
			if [ ! -f $DST/.backup ]; then
				echo "=> ERROR: Volume did not mount properly (missing .backup?) - exiting" >> $log
				diskutil unmount /Volumes/$NAME
				if [ -d $DST ]; then
					rmdir $DST
				fi
				exit 1
			fi
		else
			echo "=> Checking -> $NAME at /dev/$DEVICE" >> $log
			if [ `file /dev/$DEVICE | awk '{print $2}'` = "block" ] ; then
				echo "=> Mounting -> $NAME at /dev/$DEVICE" >> $log
				if [ -d $DST ]; then
					rmdir $DST
				fi
				diskutil mount /dev/$DEVICE
				if [ -f $DST/.backup ]; then
					echo "=> Volume -> $NAME mounted properly" >> $log
					else
					echo "=> ERROR: Volume did not mount properly (missing .backup?) - exiting" >> $log
					diskutil unmount /Volumes/$NAME
					if [ -d $DST ]; then
						rmdir $DST
					fi
					exit 1
				fi
				else
				echo "=> ERROR: /dev/$DEVICE does not appear to be a disk partition - exiting" >> $log
				exit 1
			fi
		fi
	fi
	echo "=> Starting rsync of $SRC to $DST" >> $log
	rsync --delete -av $SRC/* $DST >> $log
	echo "=> Rsync completed ..." >> $log
	echo "=> Unmounting -> $NAME at /dev/$DEVICE" >> $log
	diskutil unmount /Volumes/$NAME
	if [ -d $DST ]; then
		rmdir $DST
	fi
	echo "=> Backup finished: `date`" >> $log
	exit 1
else
	echo "=> WARNING: Backup already running: `date`" >> $log
	exit 1
fi


[ Reply to This | # ]
Mount / unmount external drive for Backup use
Authored by: tbaur on Jun 08, '07 02:41:23AM
couple minor fixes -- namely when run by cron, it's probable you won't have a path, so we'll just use full path for diskutil/rsync. cheers.
--- backup.old  2007-06-08 02:35:45.000000000 -0700
+++ backup      2007-06-08 02:36:10.000000000 -0700
@@ -28,7 +28,7 @@
 
 # ==> Backup using rsync to defined mountpoint:/path
 #     ...mount before backup, unmount when completed
-#     version 0.9, this code is not supported.
+#     version 0.9.1, this code is not supported.
 #
 # - create a .backup file on the destination (extra validation)
 # - update the applicable variables: src/dst/name/log
@@ -36,11 +36,11 @@
 # - how logging is done could be more pretty, but it's all good
 #
 # run via crontab(1) as needed:
-#     * 2 * * * /path/to/script
+#     0 2 * * * /path/to/script
 
 
-SRC=/Volumes/
-DST=/Volumes/
+SRC=
+DST=
 NAME=
 log=
 
@@ -48,7 +48,7 @@
   exit 1
 
 
-DEVICE=`diskutil list|grep $NAME|awk '{print $6}'`
+DEVICE=`/usr/sbin/diskutil list|grep $NAME|awk '{print $6}'`
 PROC=`ps auwx | grep "rsync" | grep -v grep | wc -l`
 TOP='1'
 
@@ -63,7 +63,7 @@
                        echo "=> Volume -> $NAME is already mounted" >> $log
                        if [ ! -f $DST/.backup ]; then
                                echo "=> ERROR: Volume did not mount properly (missing .backup?) - exiting" >> $log
-                               diskutil unmount /Volumes/$NAME
+                               /usr/sbin/diskutil unmount /Volumes/$NAME
                                if [ -d $DST ]; then
                                        rmdir $DST
                                fi
@@ -76,12 +76,12 @@
                                if [ -d $DST ]; then
                                        rmdir $DST
                                fi
-                               diskutil mount /dev/$DEVICE
+                               /usr/sbin/diskutil mount /dev/$DEVICE
                                if [ -f $DST/.backup ]; then
                                        echo "=> Volume -> $NAME mounted properly" >> $log
                                        else
                                        echo "=> ERROR: Volume did not mount properly (missing .backup?) - exiting" >> $log
-                                       diskutil unmount /Volumes/$NAME
+                                       /usr/sbin/diskutil unmount /Volumes/$NAME
                                        if [ -d $DST ]; then
                                                rmdir $DST
                                        fi
@@ -94,10 +94,10 @@
                fi
        fi
        echo "=> Starting rsync of $SRC to $DST" >> $log
-       rsync --delete -av $SRC/* $DST >> $log
+       /usr/bin/rsync --delete -av $SRC/* $DST >> $log
        echo "=> Rsync completed ..." >> $log
        echo "=> Unmounting -> $NAME at /dev/$DEVICE" >> $log
-       diskutil unmount /Volumes/$NAME
+       /usr/sbin/diskutil unmount /Volumes/$NAME
        if [ -d $DST ]; then
                rmdir $DST
        fi


[ Reply to This | # ]