[HOWTO] - make automatic backups

Racer D

what?
in this howto I'd like to explain how to do backups of your files onto a samba share.
I will show you how to do this free, without the use of some programs you have to pay for. Instead I created a shell script to do this for you.

  1. first of all, you need to have access to a samba share in your network. I hope you can figure how to do that on your own ;)
  2. now open your Terminal.app (/Applications/Utilities/Terminal.app)
    type in this commands (enter your admin password when prompted for it)
    Code:
    sudo touch /usr/bin/mount
    sudo chmod +x /usr/bin/mount
    sudo pico /usr/bin/mount
    this will create a file, make it executable and open a text editor for you to edit it
    next, paste this into terminal while your pico editor is opened (command+c / command+v)

    Code:
    #! /bin/sh
    # a script to make backups onto a samba share
    # created and tested on MAC OS X 10.3
    #
    # Author: Racer D <racer.d@email.si>
    # Date: 2004-05-24
    #
    
    
    # ----- variables
    
    # where you would like to mount the shared drive
    mount_point=/Volumes/share
    # //username:password@server/share_name
    filesystem=//user:pass@192.168.1.1/share
    # where your files are at
    # must be full path (don't use ~ for home)
    local_dir=/Users/user/test
    # the same as local_dir but with \ infront of every /
    # also there has to be \/ at the end
    local_dir2="\/Users\/racer\/test\/"
    # in what direcotry under remote_dir would you like to keep backups
    backup_dir=backup
    
    # ---------------
    
    remote_dir=$mount_point/$backup_dir
    
    # uncomment if you want to do a full backup (not just files changed from last backup)
    # rm ~/Library/last_backup
    
    # making sure share is mounted, else mounting it
    if test -d $mount_point
    then
    	mounted=no
    	echo share already mounted.
    else
    	mounted=yes
    	echo mounting share...
    	mkdir -v $mount_point
    	mount_smbfs $filesystem $mount_point
    fi
    
    # create all necessary folders
    echo creating necessary folders...
    if ( ! test -d "$remote_dir" )
    then
    	mkdir "$remote_dir"
    fi
    
    if ( test -f ~/Library/last_backup )
    then
    	find -x $local_dir -type d -mindepth 1 -newer ~/Library/last_backup -print | sed s/$local_dir2// > ~/Library/backup_folders
    else
    	find -x $local_dir -type d -mindepth 1 -print | sed s/$local_dir2// > ~/Library/backup_folders
    fi
    
    file=~/Library/backup_folders
    while read line
    do
    	if ( ! test -d "$remote_dir/$line" )
    	then
    		mkdir -v "$remote_dir/$line"
    	fi
    done < $file
    rm ~/Library/backup_folders
    
    
    # listing all files to backup
    # if backup done before, list only files changed from last backup
    # else list all files
    echo listing files to backup...
    if ( test -f ~/Library/last_backup )
    then
    	find -x $local_dir -type f -newer ~/Library/last_backup -print | sed s/$local_dir2// > ~/Library/backup_files
    else
    	find -x $local_dir -type f -print | sed s/$local_dir2// > ~/Library/backup_files
    fi
            
    # update the date when script was last run
    # this creates a file named "last_backup" in your Library.
    # Don't delete this file
    echo updating last run date...
    date > ~/Library/last_backup
            
    # copy the files
    echo copying the files...
    file=~/Library/backup_files
    while read line
    do
    	cp -vf "$local_dir/$line" "$remote_dir/$line"
    done < $file
    rm ~/Library/backup_files
    
    # unmount the share if mounted before
    if [ "$mounted" = yes ]
    then
    	echo unmounting share...
    	umount $mount_point
    	rm -rf $mount_point
    fi
    
    echo backup done.
    now edit the "variables" part to suit your needs.
    press "ctrl + x" and then enter to save the file.
    your basic backup script is now ready. type backup in terminal to backup your files.
    what it does it is:
    - first it checks if your samba share is mounted, else it mounts it
    - then it finds all files that have been changed since the script was last run
    - it copies all the changed files to your samba share.
    - finally it removes all the temporary files and unmounts the share if it had to mount it before
    + it puts a file "last_backup" in your Library, which tells when the backup was last made. Do not delete this file unless you wish to do a full backup
    * it does NOT though remove files on your share that do not exist in the original direcory anymore [anyone who knows how to do this, please post your suggestions]
  3. now, if you want the backups to be made automatically, you have to do this:
    again, in your terminal, enter this command
    Code:
    crontab -e
    to make the backups happen every day at 5 AM, do this:
    press "i". now type (or just paste) this line in
    Code:
    0 5 * * * /usr/bin/backup
    now press escape, type ":wq" end hit enter
    (see here for more info on how to automate your backups)

This should be all, now just wait till 5 AM and have your backups made ;)

post any questions & suggestions to the script or the tutorial you might have

use at your own risk...
It worked for me, but I cannot guarantee it will work for you too.

--- script updated 24. 5. 04 (minor bugs) ---
 
Why did you copy the files instead of using "tar" to backup the files? By using tar you would use less disk space on your samba share.
 
seeing how 4 years have passed since the original post, I'm surprised anyone is still interested.

jb.
 
Sorry, after I posted the question I saw the date it was put out there. The reason I was looking at all those postings is that I am new to Mac's and I am trying to get up to speed on it. But I have been playing around with Linux for several years now.

Sorry
 
Back
Top