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

DMG backup, archive, and transfer to remote machine script UNIX
The following hint creates a .DMG of a given directory/folder, then uses SCP to move it from your machine to another machine. It also appends a date onto the .DMG name, and lastly sends you an email that it was transferred successfully (or not).

The script has a few assumptions:
  • The source machine can send you email from the command line, if Postfix is working correctly.
  • The source machine has a password-less SCP connection to the destination machine. If not, see this hint for help setting that up.
Once you've moved the script to a location on your machine, you'll need to make it executable by typing the following in Terminal:

sudo chmod +x dmgbackup-script.sh

Of course, the script can then be called via a cron job or other scheduler if desired.

Thanks to BobHarris at the Apple.com » Support » Discussions » Mac OS X Technologies » Unix forums for his help in making this script work the way it does.

Here's the script:
#!/usr/bin/env bash
#
# this script archives a given folder into a compressed .dmg, appends a date onto the .dmg name, and and sends them to another machine
#
SOURCE="/Users/youraccount/yourfolder"
TMPFILE="/tmp/yourfolder-$(date +%Y%m%d).dmg"
# destination must be exact, absolute path on remote machine where backup is to be kept:
DESTINATION="/Volumes/Backups/backup-directory/"
SUBJECT="Success: Backup results `date`"
RECIPIENT="lurch@example.com"
export PATH=/bin:/usr/bin:/sbin:/usr/sbin 
#
hdiutil create "${TMPFILE}" -srcfolder "${SOURCE}" >/tmp/$$
sts=$?
if [[ ${sts} = 0 ]]; then
    echo "hdiutil create successful!!" >>/tmp/$$

# display size in human readable format:

    echo `du -sch ${TMPFILE}` >>/tmp/$$
else
    echo "hdiutil create FAILURE!!" >>/tmp/$$
    SUBJECT="hdiutil FAILURE: Backup results"
fi

#
# if the hdiutil was successful, then copy the file to backup server.
#
if [[ ${sts} = 0 ]]; then
    scp -r -E "${TMPFILE}" username@yourremotemachine.com:"${DESTINATION}"
    sts=$?
    if [[ ${sts} = 0 ]]; then
        echo "DMG backup successful!!" >>/tmp/$$
    else
        echo "DMG backup FAILURE!!" >>/tmp/$$
        SUBJECT="scp FAILURE: Backup results"
    fi
fi

#
# Send mail. This assumes Postfix or another MTA is already working on your machine
#
mail -s "${SUBJECT}" ${RECIPIENT} </tmp/$$
#
#  Clean up temp files
#
rm -rf "${TMPFILE}" </tmp/$$

[crarko adds: I haven't tested this one.]
    •    
  • Currently 2.60 / 5
  You rated: 5 / 5 (5 votes cast)
 
[6,249 views]  

DMG backup, archive, and transfer to remote machine script | 2 comments | Create New Account
Click here to return to the 'DMG backup, archive, and transfer to remote machine script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
DMG backup, archive, and transfer to remote machine script
Authored by: CarlRJ on Jan 27, '11 12:08:26PM

The last line would have to read:

mail -s "${SUBJECT}" ${RECIPIENT} </tmp/$$

for any mail to actually get sent. I suspect the original got cut off at the first less-than when published.



[ Reply to This | # ]
DMG backup, archive, and transfer to remote machine script
Authored by: crarko on Jan 28, '11 04:43:50AM

Yup. I fixed it, thanks for the catch.



[ Reply to This | # ]