Howto: Simple and fast backup using rsync

Discussion in 'Linux' started by nugroho2, Dec 9, 2008.

  1. nugroho2

    nugroho2

    Joined:
    Oct 6, 2008
    Messages:
    50
    Likes Received:
    0
    After a lot of aches and pains (in real term), as a newbie in Linux, I found one simple and fast backup method, rsync, that I would like to share with newbies.

    See mikerubel.org for complete explanation.

    Steps:
    1. Make a hardisk partition or USB that is slightly bigger in size than the size of the folder you want to copy.
    2. Format it in Linux friendly file system, e.g. ext2, ext3. Fat32 will not work.
    3. Create four folders, i.e. backup.0, backup.1, backup.2, backup.3
    4. Copy the code below to a file, e.g. rsync-backup.

    Code:
     #!/bin/bash
    # ----------------------------------------------------------------------
    # mikes handy rotating-filesystem-snapshot utility
    # ----------------------------------------------------------------------
    # revised by EN
    # ----------------------------------------------------------------------
    #!/bin/bash
    # ----------------------------------------------------------------------
    
    unset PATH
    
    # ------------- system commands used by this script --------------------
    ID=/usr/bin/id;
    ECHO=/bin/echo;
    
    MOUNT=/bin/mount;
    RM=/bin/rm;
    MV=/bin/mv;
    CP=/bin/cp;
    TOUCH=/bin/touch;
    
    RSYNC=/usr/bin/rsync;
    
    # ------------- file locations -----------------------------------------
    
    # change the SNAPSHOT below according to your setting. Look in /media for your harddisk folder
    SNAPSHOT=/media/YOUR-BACKUP-FOLDER;
    # change the EXCLUDES below according to your setting.
    EXCLUDES=/mnt/home/Downloads/backup_exclude;
    
    # ------------- the script itself --------------------------------------
    
    # rotating snapshots of backup 
    
    # step 1: delete the oldest snapshot, if it exists:
    if [ -d $SNAPSHOT/backup.3 ] ; then			\
    $RM -rf $SNAPSHOT/backup.3 ;				\
    fi ;
    
    # step 2: shift the middle snapshots(s) back by one, if they exist
    if [ -d $SNAPSHOT/backup.2 ] ; then			\
    $MV $SNAPSHOT/backup.2 $SNAPSHOT/backup.3 ;	\
    fi;
    if [ -d $SNAPSHOT/backup.1 ] ; then			\
    $MV $SNAPSHOT/backup.1 $SNAPSHOT/backup.2 ;	\
    fi;
    
    # step 3: make a hard-link-only (except for dirs) copy of the latest snapshot,
    # if that exists
    if [ -d $SNAPSHOT/backup.0 ] ; then			\
    $CP -al $SNAPSHOT/backup.0 $SNAPSHOT/backup.1 ;	\
    fi;
    
    # step 4: rsync from the system into the latest snapshot (notice that
    # rsync behaves like cp --remove-destination by default, so the destination
    # is unlinked first.  If it were not so, this would copy over the other
    # snapshot(s) too!
    $RSYNC								\
    	-a --delete --delete-excluded				\
    	--exclude-from="$EXCLUDES"				\
    	/SOURCE $SNAPSHOT/backup.0 ;
    
    # replace the SOURCE above with your source folder. Omit the word SOURCE, leave the slash /, for backup of all folders.
    
    # step 5: update the mtime of backup.0 to reflect the snapshot time
    $TOUCH $SNAPSHOT/backup.0 ;
    
    5. Make the file executable by opening the Thunar file manager. Right click the file -->Permissions-->click "Allow this file to run as a program."
    6. Using mousepad, create an exclude file that contains folders or files to be excluded. This is because many folders in the file system is empty or virtual. For backup of the whole SSD disk, my exclude file contains this:

    Code:
    - /dev
    - /lost+found
    - /media
    - /misc
    - /mnt
    - /net
    - /proc
    - /RPM
    - /srv
    - /sys
    - /tmp
    
    Name the file, e.g. backup_exclude, and note its path.

    7. Run the program :
    - open the terminal,
    - run as a superuser using "su"
    - change directory to the program folder, e.g. cd /mnt/home/Downloads
    - and run the code with dot slash:
    ./rsync-backup

    Overtime there will be four complete backup sets. In my case, the size of each folder is 5.3 Gb but the real size of all the backups is only around 6 Gb for the entire 8 Gb SSD. It is weird for newbie like me, but it is true. (In Linux, two files may point to the same place in the harddisk. So, if file A is changed/deleted, file B will also be changed/deleted).

    This is incremental backup with backup.0 as the newest one. The backup runs very fast.

    For restore, just use Thunar file manager, or go to terminal, "rsync /source /destination"

    P.S. If you have not installed rsync, install it first:
    Code:
     sudo yum install rsync 
    Comments and revision from experts are most welcome.
     
    nugroho2, Dec 9, 2008
    #1
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.