JJB Blog

My Backup Scripts

This is a followup to Operation: Simple Functional Backup.

Okay, here are the glorious scripts I came up with.

I call this one dailybackup: [UPDATE: a newer version of my script is posted here]

[bash]
#!/bin/sh

SOURCEDIR=/Users/john ;
ARCHIVEROOT=/Volumes/fire/backup ;
ARCHIVENAME=PowerBookHomeDirectory ;rsync \
-a –delete -delete-excluded \
–exclude “/Music/” \
–exclude “/tmp/” \
–exclude “/Library/Caches/” \
–exclude “/Desktop/downloads/” \
$SOURCEDIR/ $ARCHIVEROOT/$ARCHIVENAME ;
[/bash]

I call this one monthlybackup:

[bash]
#!/bin/sh

# Comment this out if you want to use the built-in disk burner. If you want to use an external device find it’s name from `hdiutil burn -list`
DEVICE=‘-device IOService:/MacRISC2PE/pci@f4000000/AppleMacRiscPCI/firewire@E/AppleFWOHCI/IOFireWireController/IOFireWireDevice@30e00150abd577/IOFireWireUnit/IOFireWireSBP2Target/IOFireWireSBP2LUN/com_apple_driver_Oxford_Semi_FW911/IOSCSIPeripheralDeviceNub/IOSCSIPeripheralDeviceType05/IODVDServices’ ;

# Where the scripts should keep its files while working with them. everything is deleted in the end anyway so /tmp should be suitable.
SCRATCH=/tmp ;

# Where dailybackup keeps its archive
ARCHIVEROOT=/Volumes/fire/backup ;

# The name of the archive that dailybackup makes.
ARCHIVENAME=PowerBookHomeDirectory ;
# ARCHIVENAME=testtest ;

# The size of the chunks that gsplit should make, for DVD or CD.
# I have not experimented with how much overhead there is for each type.
# Please let me know if you have found an optimal size: john AT johnjosephbachir DOT org
MEDIASIZE=4300 ;
#MEDIASIZE=600 ;

# stop editing ###########################
cd $ARCHIVEROOT ;
echo “–creating $ARCHIVENAME.tar.gz–” ;
tar czf $SCRATCH/$ARCHIVENAME.tar.gz $ARCHIVENAME ;
cd $SCRATCH ;
echo “–splitting $ARCHIVENAME.tar.gz into $MEDIASIZE MegaByte pieces–” ;
gsplit –numeric-suffixes -b$MEDIASIZE“m” $ARCHIVENAME.tar.gz $ARCHIVENAME.tar.gz_ ;

echo “Pieces:”
TOTAL=0 ;
for i in $ARCHIVENAME.tar.gz_* ; do
echo ” $i” ;
((TOTAL++)) ;
done;

echo “Archive has been split into $TOTAL part(s).” ;

echo “–deleting $ARCHIVENAME.tar.gz–” ;
rm $ARCHIVENAME.tar.gz ;

COUNTER=0 ;
for i in $ARCHIVENAME.tar.gz_* ; do
((COUNTER++)) ;
NAME=$ARCHIVENAME.tar.gz_“$COUNTER”_of_“$TOTAL” ;
mkdir $NAME ;
mv “$i” $NAME ;
echo “–creating disk image of $NAME–” ;
hdiutil create -fs HFS+ -volname $NAME -srcfolder $NAME “$NAME”.dmg ;
echo “–deleting $NAME–” ;
rm -r $NAME ;
done;

for i in *.dmg ; do
echo “–burning $i–”
hdiutil burn -noverifyburn $DEVICE “$i” ;
echo “–deleting $i–” ;
rm “$i” ;
done;
[/bash]

They could use some further refinement, but for the most part I am happy as a clam.

Two initial questions/problems:

  1. Do any of my geeky readers have recomendations for other things I should exclude in the first script?
  2. split seems to only want to make chunks that are about 600 megabytes each, even though I am specifying 4700 megabytes (DVD size). This is really annoying, but I am assuming that either I am missing something simple, or another solution will present itself.

    update: the problem is the split that ships with OS X seems to have a max size of 2^32 bytes. I installed the gnu coreutils using darwinports (sudo port install coreutils, which installs all the utils as “g__“, very nice. If you get the coreutils source from gnu, it builds without a hitch but will install without the “g” prefix), switched to gsplit, and ta-da, everything is working.


4 Comments

oh my god. i am in heaven.

$ monthlybackup
–creating PowerBookHomeDirectory.tar.gz–
–splitting PowerBookHomeDirectory.tar.gz into 4700 MegaByte pieces–
Pieces:
PowerBookHomeDirectory.tar.gz_00
Archive has been split into 1 part(s).
–deleting PowerBookHomeDirectory.tar.gz–
–creating disk image of PowerBookHomeDirectory.tar.gz_1_of_1–
………………………………………………………
created: /private/tmp/PowerBookHomeDirectory.tar.gz_1_of_1.dmg
–deleting PowerBookHomeDirectory.tar.gz_1_of_1–
–burning PowerBookHomeDirectory.tar.gz_1_of_1.dmg–
Please insert a disc:
Preparing data for burn
Opening session
Opening track
Writing track
……………………………………………………………
Closing track
……………………………………………………………
Closing session
……………………………………………………………
Finishing burn
Burn completed successfully
…………………………………………………………….
hdiutil: burn: completed

Posted by John on 2 January 2006 @ 9pm

[...] ibiblio colleague and systems guru John Mills writes: So, I remember you had a cute backup script for OS X. Well I have an external USB drive, which I don’t want active all the time. My script runs from a cronjob, and automatically mounts and unmounts the external drive, and uses rsync to copy the files. [...]

Posted by Backup script from Jon Mills at JJB Blog on 18 January 2007 @ 2am

[...] while back I wrote about my backups scripts I made with rsync. I have since refined my scripts, and am now using rdiff-backup instead, which creates versioned [...]

Posted by Backing up my home directory using rdiff-backup at JJB Blog on 7 August 2007 @ 10pm

[...] a very simple and unixy approach to a pretty common problem. Eventually I’ll have to adapt my DVD backup script to use [...]

Posted by Split tar archives on the fly at JJB Blog on 24 October 2007 @ 9pm

Leave a Comment