Fork me on GitHub

Backing up Sparse Bundle Images Over SSH

If you store you backup within a (encrypted) disk image and you would like to keep this disk image consistent across various systems then you may find the information on this page helpful.

NOTE : LBackup 0.9.8r2 and later include a similar script as an example post-action script. The script below is intended for use as a standalone script. If you would like a post action script it is recommended that you start with the example bundled within the LBackup install. Utilizing the bundled version of this script has the advantage of full LBackup integration. This means that the built in and custom reporting options of LBackup become accessible. You may view or download the latest (pre-release version) available version via github.

Mac OS X Sparse Bundle Images

Mac OS X 10.5.x and later supports sparse bundle images. These are disk images which are split into smaller files. The advantage of sparse bundle images is that you may use a tool such as rsync to keep your disk image(s) consistent across various systems.

Copy sparse bundle between machines using rsync.

It is possible to copy / update a sparse bundle between two Mac OS X machines using a patched version of rsync v3.


A One Line Version

Remember that before coping or updating a sparse bundle it is important that you have checked to ensure the images is unmounted on both the source and destination. Also, please note that the online version is not as comprehensive as the full script listed below.

if [ -d /path/to/my_backup.sparsebundle ] && ! [ -d /Volumes/mybackupimage ] ; then ssh backup@remoteserver "df -h" | head -n 2 ; sleep 5 ; /usr/local/bin/rsync_v3 --rsync-path=/usr/local/bin/rsync_v3 -aNHAXEx --delete --progress --stats --protect-args --fileflags --force-change /path/to/my_backup.sparsebundle backup@remoteserver:/backups/ ; else echo "ERROR! : Source not available, or image is mounted." ; fi



A Script Version

#!/usr/bin/env bash

# Copyright 2009 Henri Shustak
# Released Under The GNU GPL v3
# Lucid Information Systems 
# http://www.lucidsystems.org

# This script will use rsync to push an updated copy of a sparse bundle image
# from the local machine to a remote machine. 
# 
# This script could easily be modified to pull the backup or move the backup between two servers.
#
# This script is also easily condensable into a single line so it can be added concisely to your .profile
# for easy execution.
#
# The script will display progress of each band being copied or deleted during the copy. To disable this 
# remove the --progress option when calling rsync.
# 
# Finally, IT IS VERY IMPORTANT that before you call this script you check the disk image is not mounted.
# In addition, this script has only been tested between Mac OS X systems. Use it on other operating systems
# at your own risk.

# Various settings which you will want to alter before running this script.
local_sparse_bundle_to_sync="/path/to/my_backup.sparsebundle"
remote_sparse_bundle_destination="/backups/"
remote_server_address="myremotesshserver.mydomain.com"
remote_server_user="mrbackup"
path_to_rsync="/usr/local/bin/rsync_v3.0.6"

# Internal Variables
hdiutil_mounted_status=`hdiutil info | grep "image-path" | grep "${local_sparse_bundle_to_sync}"`
local_system_kind=`uname`
remote_system_kind=`ssh ${remote_server_user}@${remote_server_address} "uname"`

# Check this is a Mac OS X System
if [ "${local_system_kind}" != "Darwin" ] || [ "${remote_system_kind}" != "Darwin" ] ; then
  echo "ERROR! : Either the remote or local system is not a Mac OS X system" 
  exit -1
fi

# Okay now we have all the configuration information lets copy / update the sparse bundle.

# Check the image available and is not mounted.
if [ -d "${local_sparse_bundle_to_sync}" ] && [ "${hdiutil_mounted_status}" == "" ] ; then 

  # Print some volume statistics for the remote servers root partition.
  ssh ${remote_server_user}@${remote_server_address} "df -h" | head -n 2 

  # Wait a moment so you can easily stop this process if you made a mistake.
  sleep 5

  # Use rsync to copy / update the remote file
  ${path_to_rsync} --rsync-path=${path_to_rsync} \
  -aNHAXEx --delete --progress --stats --protect-args --fileflags --force-change \     
  ${local_sparse_bundle_to_sync} \         
  ${remote_server_user}@${remote_server_address}:${remote_sparse_bundle_destination}

  exit $?

else

  echo "ERROR! : Source not available : ${local_sparse_bundle_to_sync}"
  echo "         Or the image is mounted : ${mounted_image_path}"
  exit -1

fi



Moving the Bundle to Other Servers

The command below will need to modified to your requirements. Essentially, this command is able to sync the disk image between two remote Mac OS X machines. Please keep in mind that this is just one possible approach.

  ssh user@src_server.yourdomain.com "/usr/local/bin/rsync_v3 -aNHAXEx --rsync-path=/usr/local/bin/rsync_v3 --delete --progress --stats --protect-args --fileflags --force-change /path/to/src_image.sparsebundle user@dst_server.yourdomain.com:/path/to/this/sparsebundle_backup/
  • Keep in mind that with the command listed above any other items in the destination path will be deleted.