DMG backup, archive, and transfer to remote machine script
Jan 27, '11 07:30:00AM • Contributed by: lurch99
Jan 27, '11 07:30:00AM • Contributed by: lurch99
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:
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:
[crarko adds: I haven't tested this one.]
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.
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.]
•
[6,336 views]
