Simple Automated Backup Script v0.5b - Updated 3/20/14

Discussion in 'Bukkit Tools' started by eNkrypt, May 11, 2014.

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


    Name: Simple Automated Backup Script
    Created by: eNkrypt (Christopher S.)
    Version: 0.5b
    Last Updated: 03/20/2014

    Hey guys, I created this script and have been working on it for a while. I have it posted on another website, and thought I would share it with you all.

    If you find this post helpful at all, please let me know! I like to know if I am helping!
    If you have any questions, or requests please feel free to ask me and I will try to answer them to the best of my ability.

    Feel free to modify and change the script to suite your needs. However, all I ask is that you PLEASE credit me!

    What this script does:
    This script backups everything that is in the folder specified by $backupPath and uses the highest multi-threaded compression rate to compress the backup into a single tar.gz file.

    If you are running your server within a screen session this script will connect to that screen session and issue the save-all command which will save the current state of the server. It will then issue the save-off command. This will prevent any corruption while the server is backing up. After the script has finished backing up your server it will issue the command save-on so that your server will continue to save (This is customizable for McMyAdmin Users). It will ill display where the backup was stored, the name of the backup, the time it took to run the backup, and the size of the backup. It will exclude the DynMap folder from the backup, if told to (This significantly decreases the backup time). It will also store your backup to a remote server using the SCP protocol.

    You must have screen installed on your server, this can be done very easily depending on your distribution of linux. You must also have pigz installed on your server, the advantage of pigz over gzip is that pigz has multithreaded capabilities and will decrease the impact on server performance while the backup is being compressed. (Thanks Corin for this tip!)

    In version 0.2b I added remote file placement. This requires sshpass to be installed, this allows for a password to be passed though the SCP connection. SCP connection will use SSH credentials. SCP is not to be confused with FTP, they are completely different transfer protocols.

    Note: After many requests, in v0.4b I added the ability for the script to attempt to install dependencies for those of you that are not comfortable using shell commands.

    For RHEL (CentOS) based distributions the commands are:
    For Debian based distributions the command is:
    • apt-get install screen
    • apt-get install pigz
    • apt-get install sshpass
    For this script to work you must name your screen command before starting your server inside of that screen session. This can easily be done by running the following command:
    • screen -S screenName
    Configurable Variables:
    This script has the following configurable variables that you should change. These variables will be server specific.
    • installDependencies - If set to true, the script will attempt to install any dependencies that are required by this script. I suggest turning this off after they have been installed.
    • sendScreenCommand - This allows you to toggle if you want the script sending commands to your screen session or not. I recommend using this if you are backing up your minecraft server while it is running.
    • backupLocal - This allows you to specify if you want the backup to be stored locally or not.
    • backupRemote - This allows you to specify if you want the backup to be stored remotely or not.
    • excludeDynmap - This allows you to specify if you want to exclude the dynmap folder your backup. This significantly decreases the backup time since the dynmap foulder has thousands of images in it.
    • usePigz - enabling this will allow for the server to compress the backup using multiple threads. This significantly decreases the amount of time taken. PIGZ MUST BE INSTALLED.
    • deleteOldFiles - Enabling this allows the script to delete files older than $deleteFilesOlderThan days.
    • deleteFilesOlderThan - This string is the number of days that have gone by before deleting. By default this is set to 30 days. Anything older than 30 days will be delete. MUST HAVE "+" in front of your number.
      • Example: deleteFilesOlderThan="+30"
    • backupPath - This is going to be the COMPLETE path to what you want to backup. Typically people will want to backup their Minecraft folder. Their should be NO trailing forward-slash "/" in this variable!
      • Example: backupPath="/path/to/Minecraft"
    • storePath - This is going to be the path where you want your backups stored. No need to make a separate folder in the backup folder. The script will create a folder with the name you specify in the serverName variable.
      • Example: storePath="/path/to/store/backups"
    • serverName - This variable is going to be the name of the server you want to backup
      • Example: serverName="SMP"
    • screenName - This is going to be the name of the screen session that you are running your Minecraft server under
      • Example: screenName="MinecraftServer"
    • fileName - This is going to be the name of the generated compressed archive will be called.
      • Example: fileName="$server-$now-$date"
        • This will produce a filename similar to: SMP-01:03:35-10-01-2013
    • dynmapPath - This is going to be the location of your dynmap folder. This is usually located in the plugins directory. Make sure you have NO LEADING FORWARD-SLASH in this variable!
      • Example: dynmapPath="/Minecraft/plugins/dynmap/web/tiles"
    • remoteHost - This is the remote server IP or URL in which you want your backup to be transferred to.
    • remoteUsername - This is the username of the remote host you are connecting to.
    • remotePassword - This is the password of the remote host you are connecting to.
    • remotePort - This is the port used to connect to for the remote host. This is usually 22.
    • remoteDirectory - This is the absolute path with no leading "/" on the remote server that you want to store your backup to. This directory MUST exist.
    • useSlashCommand - If you are using a server container like McMyAdmin, this should be set to true for server containers that use /save-all instead of save-all.
    • backupMySQL - Enabling this backs up the MySQL database using the configs below.
    • sqlUser - This is the MySQL DB_Username you want backed up.
    • sqlPassword - This is the MySQL Password to the database you want backed up.
    • sqlHost - This is the host location of where the MySQL server resides. Usually this is localhost.
    • sqlDB - This is the MySQL db_name that you want backed up.
    • sqlUmask - This is the permission set to use for the SQL file (umask 177 is equivalent to CHMOD 600)
    I do not guarantee that this script will work, and will not take any responsibility for anything that may happen as a result of using this script. You should have some basic knowledge of how SSH and BASH scripting works before utilizing this script.

    The Script:

    Running the script:
    In order to utilize this script you will need to create a new file.
    • nano
    You will then need to copy the contents of the script into that file. Once the contents have been placed into the file you will need to exit out of nano.
    You will then need to change the mod of the file to 755 this can be done by issuing the command
    • chmod 755
    Now in order to run the script you have several options:
    • sh
    • ./
    I hope I have helped some of you in some way, I personally am writing this script for me because sometimes I forget to manually save my server. So I setup a chron job in order to save and backup my server every 12 hours. If you find this post helpful at all, please let me know! I like to know if I am helping!
    If you have any questions, or requests please feel free to ask me and I will try to answer them to the best of my ability.

    Feel free to modify and change the script to suite your needs. However, all I ask is that you PLEASE credit me!

    Change Log:
    Added the ability to backup MySQL databases.
    Added the ability to delete files older than x amount of days.
    Fixed many of the issues users were having
    Fixed turning save-on where $saveon variable wasn't defined
    Added the ability to exclude the dynmap folder
    Added the ability to customize what the script does and doesn't do
    Added remote support abilities using SCP protocol
    Script Created
    -Add tutorial on how to setup a chronjob to run this script
    -Add a file deleter inside of this script to delete old backups to conserve space
    -Backup any and all SQL databases that are being used by plugins
    -Ability to exclude the dynmap plugin due to the extensive about of images and files
    -Add a remote connection (FTP and sFTP) to the script to allow transferring of the file to a remote location
    -Add FTP support.
    -Add ability for the user to use rsync to copy contents of a server over to another
    -Add script update check and automatic download

    If you like what I have made, feel free to buy me a beer :)

    For those of you that are having problems (many people have messaged me)

    It would appear that a lot of users are editing this script in windows then uploading to their server. This can result in invalid return characters from the lines you have edited (shown by the \r)

    Windows formats and saves files weird. If you are going to do it that way I suggest you paste the file into notepad then configure the settings, then copy the text.

    Once the text is copied go to your server and vi the bash file and paste the text into the console.

    This usually takes away all the messy saves that windows puts in the files.


    If you want you could try and run this command to your .sh file

    sed -i -e 's/\r$//'

    This should remove any spurious CR characters.
Thread Status:
Not open for further replies.

Share This Page