Multi-World Backup Script

Discussion in 'Bukkit Discussion' started by Obliviator, Jun 17, 2012.

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

    Obliviator

    Does anyone have a version of this script, or one similar to it, that supports multiple worlds.
    (Hopefully selectable worlds and changable times per world)

    Its a cron supporting backup script that executes at the user-defined interval.
    Code:
    #!/bin/bash
    #MINECRAFT Autobackup By Justin Smith
     
    #Variables
     
    # DateTime stamp format that is used in the tar file names.
    STAMP=`date +%d_%m_%H_%M`
     
    # The screen session name, this is so the script knows where to send the save-all command (for autosave)
    SCREENNAME="mc"
     
    # Whether the script should tell your server to save before backup (requires the server to be running in a screen $
    AUTOSAVE=1
     
    # Notify the server when a backup is completed.
    NOTIFY=1
     
    # Backups DIR name (NOT FILE PATH)
    BACKUPDIR="backups"
     
    # MineCraft server properties file name
    PROPFILE="server.properties"
     
    # Enable/Disable (0/1) Automatic CronJob Manager
    CRONJOB=1
     
    # When an error occurs it will send it to this e-mail
    MAILTO="[email protected]"
     
    # Update every 'n' Minutes
    UPDATEMINS=720
     
    # Delete backups older than 'n' Days
    OLDBACKUPS=2
     
    # Enable/Disable Logging (This will just echo each stage the script reaches, for debugging purposes)
    LOGIT=0
     
    # *-------------------------* SCRIPT *-------------------------*
    # Set todays backup dir
     
    if [ $LOGIT -eq 1 ]
    then
      echo "[LOG] Starting Justins AutoBackup Script.."
      echo "[LOG] Working in directory: $PWD."
    fi
     
    BACKUPDATE=`date +%d-%m-%Y`
    FINALDIR="$BACKUPDIR/$BACKUPDATE"
     
    if [ $LOGIT -eq 1 ]
    then
      echo "[LOG] Checking if backup folders exist, if not then create them."
    fi
     
    if [ -d $BACKUPDIR ]
    then
      echo -n < /dev/null
    else
      mkdir "$BACKUPDIR"
     
      if [ $LOGIT -eq 1 ]
      then
          echo "[LOG] Created Folder: $BACKUPDIR"
      fi
     
    fi
     
    if [ -d "$FINALDIR" ]
    then
      echo -n < /dev/null
    else
      mkdir "$FINALDIR"
     
      if [ $LOGIT -eq 1 ]
      then
          echo "[LOG] Created Folder: $FINALDIR"
      fi
     
    fi
     
    if [ $OLDBACKUPS -lt 0 ]
    then
      OLDBACKUPS=3
    fi
     
    # Deletes backups that are 'n' days old
    if [ $LOGIT -eq 1 ]
    then
      echo "[LOG] Removing backups older than 3 days."
    fi
    OLDBACKUP=`find $PWD/$BACKUPDIR -type d -mtime +$OLDBACKUPS | grep -v -x "$PWD/$BACKUPDIR" | xargs rm -rf`
     
    # --Check for dependencies--
     
    #Is this system Linux?
    #LOL just kidding, at least it better be...
     
    #Get level-name
    if [ $LOGIT -eq 1 ]
    then
      echo "[LOG] Fetching Level Name.."
    fi
     
    while read line
    do
      VARI=`echo $line | cut -d= -f1`
      if [ "$VARI" == "level-name" ]
      then
          WORLD=`echo $line | cut -d= -f2`
      fi
    done < "$PROPFILE"
     
    if [ $LOGIT -eq 1 ]
    then
      echo "[LOG] Level-Name is $WORLD"
      echo ""
    fi
     
    BFILE="$WORLD.$STAMP.tar"
    CMD="tar -cf $FINALDIR/$BFILE $WORLD"
     
    if [ $LOGIT -eq 1 ]
    then
      echo "[LOG] Packing and compressing folder: $WORLD to tar file: $FINALDIR/$BFILE"
    fi
     
    if [ $NOTIFY -eq 1 ]
    then
      screen -x $SCREENNAME -X stuff "`printf "say Backing up world: \'$WORLD\'\r"`"
    fi
     
    #Create timedated backup and create the backup directory if need.
    if [ $AUTOSAVE -eq 1 ]
    then
      if [ $NOTIFY -eq 1 ]
      then
          screen -x $SCREENNAME -X stuff "`printf "say Forcing Save..\r"`"
      fi
      #Send save-all to the console
      screen -x $SCREENNAME -X stuff `printf "save-all\r"`
      sleep 2
    fi
     
    if [ $NOTIFY -eq 1 ]
    then
      screen -x $SCREENNAME -X stuff "`printf "say Packing and compressing world...\r"`"
    fi
     
    # Run backup command
    $CMD
     
    if [ $NOTIFY -eq 1 ]
    then
      # Tell server the backup was completed.
      screen -x $SCREENNAME -X stuff "`printf "say Backup Completed.\r"`"
    fi
     
    # --Cron Job Install--
    if [ $CRONJOB -eq 1 ]
    then
     
      #Check if user can use crontab
      if [ -f "/etc/cron.allow" ]
      then
          EXIST=`grep $USER < /etc/cron.allow`
          if [ "$EXIST" != "$USER" ]
          then
            echo "Sorry. You are not allowed to edit cronjobs."
            exit
          fi
      fi
     
      #Work out crontime
      if [ $UPDATEMINS -eq 0 -o $UPDATEMINS -lt 0 ]
      then
          MINS="*"
      else
          MINS="*/$UPDATEMINS"
      fi
     
      #Check if cronjob exists, if not then create.
      crontab -l > .crons
      EXIST=`crontab -l | grep $0 | cut -d";" -f2`
      CRONSET="$MINS * * * * cd $PWD;$0"
     
      if [ "$EXIST" == "$0" ]
      then
     
          #Check if cron needs updating.
          THECRON=`crontab -l | grep $0`
          if [ "$THECRON" != "$CRONSET" ]
          then
            CRONS=`crontab -l | grep -v "$0"`
            echo "$CRONS" > .crons
            echo "$CRONSET" >> .crons
            crontab .crons
            echo "Cronjob has been updated"
          fi
     
          rm .crons
          exit
      else
          crontab -l > .crons
          echo "$CRONSET" >> .crons
          crontab .crons
          rm .crons
          echo "Autobackup has been installed."
          exit
      fi
     
    fi
    
     
Thread Status:
Not open for further replies.

Share This Page