Sonntag, 3. November 2013

(Aus dem Archiv) Backup my Mac OS X home directory with rsync

Der vorliegende Artikel habe ich ursprünglich irgendwann einmal ab 2002 auf meinem damaligen Linux-Entwicklungsserver im Web publiziert. Da ich das bloggen erst 2005 entdeckt habe, waren die Tipps in einer grossen HTML-Seite untergebracht. Anlässlich einer Aufräumaktion auf dem Server habe ich mich entschieden, die „Perlen“ über meine heutige Kommunikationsplattform ins Web zu posaunen. Seitdem ich die Artikel verfasst habe, sind viele Tage ins Land gegangen — ob der Artikel noch Gültigkeit hat, entscheidet der geneigte Leser selber.

Please be aware to use the special rsync-Version written for Mac OS X (fucking resource forks, again!). Otherwise you could end up with a mess (only on the backup side, but this sucks as much!).

I separated iPhoto-Files from the rest, because the .sparseimage wouldn’t fit on a DVD anylonger.

backup.sh

#!/bin/sh

if [ $# -lt 1 ]
then
	echo "Please provide a destination to store the backup to:"
	echo "fwdsk (Firewire-Drive)"
	echo "eth (Server-Share)"
	echo "tmp (/tmp)"
	exit 1
fi

case $1 in
	"fwdsk")
		IMAGEFOLDER="/Volumes/TRANSFER";;
	"eth")
		IMAGEFOLDER="/Volumes/RSYNC";;
	"tmp")
		IMAGEFOLDER="/tmp";;
	*)
		echo "Please provide a destination to store the backup to:"
		echo "fwdsk (Firewire-Drive)"
		echo "eth (Server-Share)"
		echo "tmp (/tmp)"
		exit 1;;
esac

if [ ! -d "$IMAGEFOLDER" ]
then
	echo "Directory '$IMAGEFOLDER' not found"
	exit 1
fi

BACKUPINFO=( "home" "iphoto" )

for VOLNAME in ${BACKUPINFO[@]}
do
	DISKIMAGENAME="$VOLNAME"
	DISKIMAGEPATH="$IMAGEFOLDER/$VOLNAME.sparseimage"
	
	BACKUPSOURCEFILE="/Users/mario/Rsync/bkpsrc/$VOLNAME.txt"
	
	if [ ! -f "$BACKUPSOURCEFILE" ]
	then
		echo "File '$BACKUPSOURCEFILE' not found"
		exit 1
	fi
	
	BACKUPSOURCE=`more $BACKUPSOURCEFILE`
	BACKUPDESTINATION="/Volumes/$VOLNAME"
	RSYNCEXCLUDE="/Users/mario/Rsync/exclude/$VOLNAME.txt"
	
	#-------------------------------------------------
	# Create/reuse & mount Disk-Image
	#-------------------------------------------------
	if [ ! -f "$DISKIMAGEPATH" ]
	then
		# Disk-image-file doesn't even exist - create it
		echo "Creating disk image \"$DISKIMAGEPATH\""
		hdiutil create -fs HFS+ -type SPARSE -size 20g -volname "$DISKIMAGENAME" "$DISKIMAGEPATH"
	else
		echo "Using pre-existing disk image \"$DISKIMAGEPATH\""
	fi
	
	if [ ! -d "$BACKUPDESTINATION" ]
	then
		# Disk-image doesn't seem to be mounted
		hdiutil attach "$DISKIMAGEPATH"
	fi
	
	#-------------------------------------------------
	# Did Disk-Image mount correctly?
	#-------------------------------------------------
	if [ ! -e "$BACKUPDESTINATION" ]
	then
		echo "There was a problem creating or mounting the disk image"
		exit 1
	fi
	
	#-------------------------------------------------
	# Rsync
	#-------------------------------------------------
	/Users/mario/Rsync/rsync.sh "$BACKUPSOURCE" "$BACKUPDESTINATION" "$RSYNCEXCLUDE"
	
	#-------------------------------------------------
	# Unmount Disk-Image and do maintenance operations
	#-------------------------------------------------
	# hdiutil info | grep /Volumes/$VOLNAME | cut -f 1
	# 
	# Expected output: /dev/disk2s2    Apple_HFS       /Volumes/Rsync-Backup
	#                  ^ this info is required to unmount
	
	DISKIMAGEDEVICE=`hdiutil info | grep /Volumes/$VOLNAME | cut -f 1`
	echo "Disk image mounted as $DISKIMAGEDEVICE"
	
	hdiutil detach -quiet "$DISKIMAGEDEVICE"
	echo "Disk image ejected"
	
	hdiutil compact "$DISKIMAGEPATH"
	echo "Disk image compacted"
done

rsync.sh

#!/bin/sh

if [ $# -ne 3 ]
then
	echo "Usage:"
	echo "rsync.sh   "
	exit 1
fi

BACKUPSOURCE="$1"
BACKUPDESTINATION="$2"
RSYNCEXCLUDE="$3"

if [ ! -d "$BACKUPSOURCE" ]
then
	echo "Source directory '$BACKUPSOURCE' not found"
	exit 1
fi

if [ ! -d "$BACKUPDESTINATION" ]
then
	echo "Destination directory '$BACKUPDESTINATION' not found"
	exit 1
fi

if [ ! -f "$RSYNCEXCLUDE" ]
then
	echo "Exclude file '$RSYNCEXCLUDE' not found"
	exit 1
fi

# --verbose
# --showtogo
# --dry-run
time sudo /usr/local/bin/rsync -a --delete --delete-excluded --eahfs --showtogo --exclude-from\
 "$RSYNCEXCLUDE" "$BACKUPSOURCE" "$BACKUPDESTINATION"

bkpsrc/home.txt

/Users/mario/.

bkpsrc/iPhoto.txt

/Users/mario/Pictures/iPhoto Library/.

exclude/home.txt

Music/
Movies/
PoisonDownloads/
Cache/
Caches/
.Trash/
Fun-Stuff/
.DS_store
iPhoto Library/

exclude/iphoto.txt

.Trash/
.DS_store

Liked this post? Follow this blog to get more. 

Tags: ,
Labels: Apple

Kommentar erfassen