Fork me on GitHub

This is an old revision of the document!


Basic Backup Script

This is a very basic and rough script.

It uses diskutil on Mac OS X to generate a disk image and then copy this disk image to a location. Improvements are welcome.

<fiile> #!/bin/bash

## ## Very Basic backup script. V.00003 ##

# Copyright 2006 Henri Shustak GNU GPU v3

## Settings

# Log File logfile=“”

backup_name=“Backup1”

# Volume with Data backup_src_volume=“/Volumes/SoruceVolume”

# This is messy - should be fixed if [ “${backup_src_volume}” == “/” ] ; then

      # Only works with source volumes with no spaces - due the last awk - either improve this script or put in the actual volume name
      will_it_work=`ls -l /Volumes/ | grep -e "-> /$" | awk -F " -> /" '{print $1}' | awk '{print NF}'`
      if [ $will_it_work == 9 ] ; then 
      	backup_src_volume=/Volumes/`ls -l /Volumes/ | grep -e "-> /$" | awk -F " -> /" '{print $1}' | awk '{print $9}'`
      else
      	echo "WARNING! : Specify the full volume name, even if it is the root directory." | tee -ai >> "$logfile"
      fi

fi

# Folder to Backup backup_src=“${backup_src_volume}/path/to/directory/to/backup”

# Volume we are using for backups backup_dst_volume=/Volumes/DestVolumeName

# Destination for Backup backup_dst=“/Volumes/${backup_dst_volume}/Backups/${backup_name}/”

# Backup Locking lock_file_dir=“/tmp/” primary_lock_file_name=“${lock_file_dir}basic_backup_inprogress.lock”

# Templocation for files temp_location=“/tmp/”

## Preflight Check backup_volume_name=`basename “$backup_dst_volume”` backup_volume_check=`ls /Volumes/ | grep -x “$backup_volume_name”` if [ “$backup_volume_check” != “$backup_volume_name” ] ; then

echo "ERROR! : Volume is unavailible" | tee -ai >> "$logfile"
echo "         Backup will not continue" | tee -ai >> "$logfile"
exit -1

fi

function run_backup {

      # Backup RADmind Certs
     
     	current_date=`date "+%Y-%m-%d"`
	backup_image="${backup_name}-(${current_date}).dmg"
     
     	# Check if the directorys to backup and the backup destinations availible and that todays backup has not already been copied
      if [ -d "$backup_src_volume" ] && [ "$backup_src" ] && [ -d "$backup_dst" ] && ! [ -f "${backup_dst}${backup_image}" ] ; then
   
   		# Carry on with the backup 

		touch "${primary_lock_file_name}"
 
		echo "Backup Commencing..." | tee -ai >> "$logfile"
		 
		# Check Temporary Image is not already there 
		if ! [ -f "${temp_location}${backup_image}" ] ; then
			# Create Encrypted Disk Image
			/bin/echo -n "    " >> "$logfile"
			hdiutil create -srcfolder "${backup_src}" "${temp_location}${backup_image}" 2>&1 >> "$logfile"
			if [ $? != 0 ] ; then
					echo "    WARNING! : Failed to Create Temporary Backup Disk Image" | tee -ai >> "$logfile"
			fi	

			if ! [ -f "${temp_location}${backup_image}" ] ; then
				echo "    WARNING! : Unable to copy backup image to backup directory" | tee -ai >> "$logfile"
				echo "               File : ${temp_location}${backup_image} dose not exist" | tee -ai >> "$logfile"
			else
				# Perform Copy
				echo "     Copying up backup image to backup volume..." | tee -ai >> "$logfile"
				ditto -rsrc "${temp_location}${backup_image}" "${backup_dst}${backup_image}"
				if [ $? != 0 ] ; then
					echo "      WARNING! : Error while coping the backup image" | tee -ai >> "$logfile"
				fi
				
				# Remove Temp File
				rm "${temp_location}${backup_image}"
				if [ $? != 0 ] ; then
					echo "    WARNING! : error removing temporary disk image file." | tee -ai >> "$logfile"
				else
					echo "    Backup Complete." | tee -ai "$logfile"
				fi
					
			fi
		else 
			echo "    ERROR! : Temporary Backup Image Exists. This backup will not complete. " | tee -ai >> "$logfile"
		fi
			
		rm "${primary_lock_file_name}"
	
	fi
	

}

function open_log {

echo "#######################" >> $logfile
echo "`date`" >> $logfile
echo "" >> $logfile

}

function close_log {

echo "" >> "$logfile"
echo "" >> "$logfile"
echo "" >> "$logfile"

}

if [ “${logfile}” == “” ] ; then

logfile=/dev/null

fi

if ! [ -d “$backup_dst” ] ; then

      echo "    ERROR: Backup Destination Directory is not available. Backup stopped." | tee -ai "$logfile"
      echo "           Specified Backup Directory : $backup_dst" | tee -ai "$logfile"
      close_log
      exit -1

fi

day_of_the_week=`date | cut -c -3`

open_log

#if [ “${day_of_the_week}” == “Fri” ] ; then

run_backup

#fi

close_log

exit 0 </file>