Fast, efficient, snapshot like backups

Discussion in 'Bukkit Help' started by crymson, Apr 1, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    crymson

    This might be of help for some of you out there, so I thought I would share.
    To begin with you will need either GNU/Linux or another *nix OS for this to work. I have not tested this on any OSes other than Linux to know for sure, but I would suspect that it would work on other *nix systems if you have bash (I do use bashisms, so this will not work on sh, dash, tcsh, ksh, etc), but if you're running bukkit on one of those, you probably already know what your doing:D.

    To start with you will need to have rsync installed. I have no idea if this is installed on ubuntu by default, but I doubt it. If anyone knows for sure then please let me know. If you don't have it installed, then do so.

    The basic idea of this backup is to use rsync, which does backups by only writing the changes between the source directory and the destination directory. This means that after the first time the backups are very fast because you're only writing changes. The beauty of my setup is that I'm not just keeping one backup, but several of them (I do one per day, but my server load is very light), but I'm only keeping the differences between each backup by using hard links. So I only actually keep one full backup, then the deltas (change) for the rest of them, this saves me a tremendous amount of space.

    EDIT: Uh, yeah, the script:
    Code:
    #!/bin/bash
    # This is a script for doing snapshot style incremental backups of minecraft
    
    # Copyright (c) 2011, Dylan Baker
    # All rights reserved
    # This software is licensed under the terms of the BSD license, which should have been included with this software, if it was not you can obtain it at http://wwww.opensource.org/licenses/bsd-license.php
    
    ## Variables for ease of use
    #The directory to be be backuped up
    sourceDir="/home/minecraft/minecraft"
    
    #The root of your backup directory
    backupDir="/home/minecraft/backup/"
    
    #Total number of backups to make
    #Pick a reasonable number based on the frequancy of backups
    totalBackups="6"
    
    # Do not edit this
    # It sets the backup structure
    destDir="$backupDir/backup.0"
    
    ## Sanity Checking
    #Check for write access in the backup directory
    if [ ! -w $backupDir ];then
        exit 1
    fi
    
    #Check to see if backup structure exists
    for t in {$totalBackups..0..-1}
    do
        if [ ! -d $backupDir/backup.$t ]; then
            mkdir $backupDir/backup.$t
        fi
    done
    
    ## backup process
    #Cycle the backups
    for i in {$totalBackups..0..-1}
    do
        mv "$backupDir.$i" "backupDir.$((i+1))"
    done
    mv "$backupDir.$((totalBackups+1))" "backupDir.0"
    cp -al "$backupDir.1/." "$backupDir.0"
    
    # Run rsync
    rsync -r --delete --exclude ".*" "$sourceDir" "$destDir"
    Save the script and set it executable:
    Code:
    chmod +x backup
    then you'll need to do a little bit of configuration. open the file with a text editor and look at the top block,

    Code:
    ## Variables for ease of use
    #The directory to be be backuped up
    sourceDir="/home/minecraft/minecraft" << Set this to your minecraft directory, keep the quotes
    
    #The root of your backup directory
    backupDir="/home/minecraft/backup/" << Set this to the folder you want to put the backups in, keep the quotes
    
    #Total number of backups to make
    #Pick a reasonable number based on the frequancy of backups
    totalBackups="6" << Set the total number of backups
    
    and that's it. just add the script to your crontab via

    Code:
    crontab -e
    then set the frequency of your backups:
    Code:
     * * * * * /wherever/you/put/backup
    While using cron is a little bit outside the scope of this little how-to I'll give a couple of examples:

    "0 * * * * /path/to/backup" would run every hour on the hour
    "0 1 * * * /path/to/backup" would run at 1 am on the hour
    "0 1 1 * * /path/to/backup" would run at 1 am on the hour on Monday.

    Some crons can do backups more frequently, like every thirty minutes, others will need multiple jobs written (like run on the hours and the half hours) if you want to do more frequent backups. Google or man pages will give you more info.

    Ideally the folder that you're backing up to is not on the same hard drive as your original saves, or better yet, not even on the same computer. I achieve this by mounting an samba share from my file server, although nfs would probably work better.
     
  2. Offline

    Grandcruiser

    Ah, I was actually thinking of setting this up on my server. Thanks for saving me the work! ;)
     
Thread Status:
Not open for further replies.

Share This Page