Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button

Article:
  Automated Backups on Tiger Using rsync
Subject:   rsync backup script
Date:   2006-04-01 08:55:07
From:   TheBronx
Hi all,


after been a couple of times to lucky with restoring my apple it's time for a automated backup. As addition to the ver usefull articel I want to post a simple script. It's not the best and nicest but it will do the job!
- Also I have read a lot of articles about mounting and unmouting the external harddisk, but it won't seem to work fine. I don't want my partinioned harddisk on my desktop all the time. So with this script it will fire him up and do the backup, gives an result output on my desktop (to check if the beackup went ok, and unmounting the external HD, and restoring the silence.
p.s. check with the command "diskutil list" for the right external disks. And change the volumename with you partition name.


Well have fun, and if you guys have additions let me know.
cheers,
Henk


do shell script "echo ==rsync Backup script == >>~/Desktop/rsync.txt"
do shell script "date >>~/Desktop/rsync.txt"
do shell script "ls /Volumes/"
if result is not {"Macintosh HD", "volumename"} then
do shell script "diskutil mountDisk disk1s7"
do shell script "echo ==Disk Mounted== >>~/Desktop/rsync.txt"
do shell script "echo ==start rsync logging== >>~/Desktop/rsync.txt"
end if
do shell script "rsync -aE --progress /Users/volumename/ /Volumes/volumename/rsync >>~/Desktop/rsync.txt || echo -n"
do shell script "echo =rsync Backup Ended== >>~/Desktop/rsync.txt"
on idle
return 60
end idle


do shell script "diskutil unmountDisk /dev/disk1s7"
do shell script "diskutil unmountDisk /dev/disk1s3"
do shell script "diskutil unmountDisk /dev/disk1s5"
do shell script "echo =====Ready!===== >>~/Desktop/rsync.txt"

Full Threads Oldest First

Showing messages 1 through 2 of 2.

  • rsync backup script - now Incremental with little storage impact
    2006-12-04 14:18:08  Inspector71 [View]

    Hi TheBronx,

    Thanks for your script - it really helped. I was wanting to do a similar thing, but I found out you can do incrtemental backups that don't copy the files across each time - only the hard links (so you need very little extra storage space); so I used yours and amended it as follows:

    I got it to check if the HDD was available or not before continuing (I'm using a MacBook so I can't guarantee that the backup drive will be plugged in) and to exit if not.

    I also implemented incremental backups so that I have copies of the state of my source for the last 9 times it was backed up (in case I backup an error). I liberally used the code on this page: http://www.macosxhints.com/article.php?story=200310240137579

    Lastly, I fiddled with some of the shell scripts to exclude .Trashes and Volumes folders as well as the Downloads folder on my system. The /Volumes exception is important as the first time I did this I not only backed up my internal HDD but also the other partition on my external one, as rsync followed the symbolic links.

    I also found that Spotlight kicked in and started indexing my backup very quickly, with enormous performance consequences. I recommend you start off by disabling indexing on your backup drive by following the advice here: http://www.thexlab.com/faqs/stopspotlightindex.html, then try running the script.

    As TheBronx suggests, find out what the name of your disk is using diskutil list and replace it into here. My partition for backing up is called Backup and I have folders 01-09 for increments within that. I have it set to backup my entire internal HDD (/).

    The only problem I am having now is that unless I run it as root (which I haven't done yet) I lose all file ownerships and everything reverts to my user.

    Here it is, have fun:


    do shell script "echo ================================ rsync Backup script ================================= >>~/rsync.log"
    do shell script "date >>~/rsync.log"

    -- Make sure Firewire HDD is available, otherwise quit
    do shell script "ls /Volumes/"
    if result does not contain "Backup" then
    do shell script "echo ==Mounting Backup Disk: >>~/rsync.log"
    do shell script "diskutil mountDisk disk1s2"
    do shell script "ls /Volumes/"
    if result does not contain "Backup" then
    do shell script "echo : Could Not Mount Backup Disk - Exiting =============!!!============= >>~/rsync.log"
    return
    else
    do shell script "echo : Backup Disk Mounted >>~/rsync.log"
    end if
    end if

    -- Rotate Backup dirs, deleting the oldest
    do shell script "echo ==Rotating Backup Directories== >>~/rsync.log"
    do shell script "rm -rf /Volumes/Backup/09"
    do shell script "mv /Volumes/Backup/08 /Volumes/Backup/09"
    do shell script "mv /Volumes/Backup/07 /Volumes/Backup/08"
    do shell script "mv /Volumes/Backup/06 /Volumes/Backup/07"
    do shell script "mv /Volumes/Backup/05 /Volumes/Backup/06"
    do shell script "mv /Volumes/Backup/04 /Volumes/Backup/05"
    do shell script "mv /Volumes/Backup/03 /Volumes/Backup/04"
    do shell script "mv /Volumes/Backup/02 /Volumes/Backup/03"
    --do shell script "cpio -pdl /Volumes/Backup/01 /Volumes/Backup/02"
    do shell script "cd /Volumes/Backup/01 && find . -print | cpio -pdla /Volumes/Backup/02"

    do shell script "echo ==Directories Successfully Rotated== >>~/rsync.log"

    -- Do rsync
    do shell script "echo ==start rsync logging== >>~/rsync.log"
    do shell script "rsync -aE --progress --delete-after --exclude=Volumes/ --exclude=Downloads/ --exclude=.Trashes / /Volumes/Backup/01 >>~/rsync.log || echo -n"
    do shell script "echo =rsync Backup Ended== >>~/rsync.log"

    on idle
    return 60
    end idle

    do shell script "diskutil unmount disk1s2"
    do shell script "echo =====Backup Complete===== >>~/rsync.log"


    Cheers,

    Marcel

    • rsync backup script - now Incremental with little storage impact
      2006-12-04 14:25:36  Inspector71 [View]

      OK, so I think the last script will work OK if you are only backing up stuff from within your home dir, but I decided to convert my script to a shell script so I could run it as root from crontab.

      I have also updated the rsync options a bit to preserve permissions and access dates. The cpio command needs a little work as it seems to change perms on the files it 'copies' (or relinks to).

      Anyway, here is the shell script (saved in /etc/backup.sh, called through crontab):


      echo ================================ rsync Backup script ================================= >>/var/log/rsync.log
      date >>/var/log/rsync.log

      if [ -d /Volumes/Backup ]
      then
      echo ==Mounting Backup Disk: >>/var/log/rsync.log
      diskutil mountDisk disk1s2
      if [ -d /Volumes/Backup ]
      then
      echo : Backup Disk Mounted >>/var/log/rsync.log
      else
      echo : Could Not Mount Backup Disk - Exiting =============!!!============= >>/var/log/rsync.log
      exit 2
      fi
      fi

      echo ==Rotating Backup Directories== >>/var/log/rsync.log
      rm -rf /Volumes/Backup/09
      mv /Volumes/Backup/08 /Volumes/Backup/09
      mv /Volumes/Backup/07 /Volumes/Backup/08
      mv /Volumes/Backup/06 /Volumes/Backup/07
      mv /Volumes/Backup/05 /Volumes/Backup/06
      mv /Volumes/Backup/04 /Volumes/Backup/05
      mv /Volumes/Backup/03 /Volumes/Backup/04
      mv /Volumes/Backup/02 /Volumes/Backup/03
      cd /Volumes/Backup/01 && find . -print | cpio -pdla /Volumes/Backup/02

      echo ==Directories Successfully Rotated== >>/var/log/rsync.log

      echo ==start rsync logging== >>/var/log/rsync.log
      rsync -aEp --progress --delete-after --exclude=Volumes/ --exclude=Downloads/ --exclude=.Trashes --exclude=/dev --exclude=/tmp --exclude=/private /Volumes/Backup/01 >>/var/log/rsync.log || echo -n
      echo =rsync Backup Ended== >>/var/log/rsync.log

      sleep 2m

      diskutil unmount disk1s2
      echo =====Backup Complete===== >>/var/log/rsync.log


      One problem I am having is that the HDD won't unmount afterwards; I think a process must be hogging it. If anyone can clean this up a bit the help would be appreciated :)

      Cheers

      Marcel