Back up a UNIX box (or folder) to a DMG image file

Aug 27, '08 07:30:00AM

Contributed by: zigzogmac

To back up a local UNIX serving box (ALIX1C motherboard with FreeBSD), I wrote a simple but efficient bash script to save all or part of it in a DMG image disk file. This backup should be used as often as required (launching with cron needs a password-free RSA login). This backup is a differential one, and an exact replication is done assuming the RSA user has sufficient privileges to do so.

Why save in a DMG?

I'm aware I'm losing Time Machine benefits with such a solution, but the script is very easy to modify to sync to a simple file system local folder. Personnaly, my DMG is placed on my Time Machine disk, to avoid filtering the Apple Backup system.

Prerequisites:

For security and convenience, I use ssh transfering protocol with a shared RSA key. If you do not know how to set this up, you should look at that kind of search. Both boxes (the remote and the Mac) need rsync binaries. On the Mac, you'll need a DMG that's as large as the remote system to back up to. Its name and its mounted name are supposed to be the same.

The Script

This script has to be used from your Mac's Terminal.app. Change the variables for your needs. Please be aware that script is not made for direct production use -- there is no error control management code, for instance. It is a raw but usable piece of code to help in backing up.

#!/bin/bash
# Goal : backup my alix box with rsync via ssh in a local dmg file
# The archive and mounted archive name should be the same, w/o .dmg extension for the second

## VARIABLES
# archive Path w/ trailing slash
sauvDMGpath=/Volumes/MactivisteTimed/
# name of the archive, should be the same as the disk, without .dmg
sauvDMGname=nomArchiveDuServeur
# scheme to use below : user@ip_or_host_name
connexionID=RSAUser@IPaddress
# backup root tree, default is "/", all file system
savingPath=/

## MAIN

# mounting image
hdiutil mount $sauvDMGpath$sauvDMGname.dmg

# wait a little bit to assure for FileSystem sync OK
sleep 1

# sync. Ass "z" extension in first extension block when using internet
#  or low rated transfert connexion in order to add compression data
rsync -aq -e ssh --delete $connexionID:$savingPath /Volumes/$sauvDMGname/

# unmount image
hdiutil eject /Volumes/$sauvDMGname
The above is based on my original article in French.

[robg adds: I haven't tested this one.]

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080824104702593