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

Back up a UNIX box (or folder) to a DMG image file UNIX
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?
  • To avoid my Mac managing another file system deep tree.
  • To simplify moving the backup.
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.


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 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.
# 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

# archive Path w/ trailing slash
# name of the archive, should be the same as the disk, without .dmg
# scheme to use below : user@ip_or_host_name
# backup root tree, default is "/", all file system


# 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.]
  • Currently 1.25 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)

Back up a UNIX box (or folder) to a DMG image file | 6 comments | Create New Account
Click here to return to the 'Back up a UNIX box (or folder) to a DMG image file' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Back up a UNIX box (or folder) to a DMG image file
Authored by: merlyn on Aug 27, '08 08:44:51AM

I don't understand the "ssh". You're copying files locally, which rsync can do just fine between two local file hierarchies.

[ Reply to This | # ]
Back up a UNIX box (or folder) to a DMG image file
Authored by: googoo on Aug 27, '08 09:08:22AM

The disk image is locally connected but the UNIX box is connected remotely via the ssh connection in the rsync command.


[ Reply to This | # ]
Back up a UNIX box (or folder) to a DMG image file
Authored by: jediknil on Aug 27, '08 09:28:06AM

You can get some of those Time Machine benefits back if you use a sparse bundle disk image, which is actually a directory of short chunks (Wikipedia says 8MB) of your files. It behaves just like a normal disk image when you mount it, but Time Machine can back up just the slices that change.

[ Reply to This | # ]
Another major benefit of using DMGs
Authored by: BryanD on Aug 27, '08 12:52:51PM

You forgot another major benefit that's available with stuff located on DMG-style disk volumes — security. When you make the DMG (via Disk Utility), select the "Encryption: ...AES..." option and the disk volume (i.e., the DMG file) will be encrypted.

In fact, we use this strategy for all our corporate and customer information to protect everyone's privacy (and our intellectual property) if a system is ever stolen (or "lost," in the case of laptops). We even have our MySQL databases located on (fixed-size) encrypted DMGs ... takes some modification to the startup stuff to ensure that the disk volume is mounted before things are started, but we've been using this strategy for years and it works just fine ('though we only use fixed-size/non-sparse images as we did have problems with sparse images when there's a crash, but that was likely back with 10.3 so it may be better, now).

[ Reply to This | # ]
Watch out for filename case issues.
Authored by: tjfarrell on Aug 27, '08 06:44:31PM

When doing this you have to watch out for the filename case issue. On most unix file systems, "makefile" and "Makefile" are different files, but on a default HFS+ partition, they are the same file.

You need to make sure you use a case sensitive (HFSX) disk image.

T. Farrell

[ Reply to This | # ]
Back up a UNIX box (or folder) to a DMG image file
Authored by: digitalhippie on Aug 28, '08 07:30:58AM
Very interesting. I figured out a similar way to back up my (linux) router (Soekris net4501/5501) to the MacPro. All the logic lives on the router and it's just a ssh key (no passphrase) and two cron lines:
# Weekly backups
59 03 * * wed   DATE=`date "+%Y%m%d"`; /usr/bin/ssh -i .ssh/id_dsa.USER -l USERNAME MacPro "nc -l -p 23456 | gzip > /path/to/backup-$DATE.dd.gz"
00 04 * * wed   /bin/dd if=/dev/hda bs=1024 | nc -w5 MacPro 23456
essentially, every wednesday shortly before 4 am it will make the MacPro listen on port 23456 for a disk-dump (dd) via netcat (nc) which it will gzip and save to a file containing the date. One minute later the router starts to dump the entire disk to the pipe, thus creating the backup on the MacPro. The entire process is about ten minutes for a 4GB flash drive, I have 1.4GB used on it and the backup size gzipped is ~400MB.

Restoring is as simple as
$ cat backup-XXXXXX.dd.gz | gzip -d | dd of=/dev/device-to-restore bs=1024
You might find it useful some day. The method works for almost any flavor of Unix, including Mac OS X.

[ Reply to This | # ]