Welcome to Our Community

Registration on our forums are now open for some days! Feel free to sign up today.

[Free Add-on ] XenForo Backup Shell Script 1.0.8

Use this script to back your database up by simply pointing it to your config.php

  1. DEXTER
    This script's intended use is to be run as a cron or called via command line to quickly back up your forum's database, but, I also added a parameter to have it prompt you for your database information. Anyhow, it saves the database in the format database_name_yyyy-mm-dd_hh-mm-ss.sql.gz , because I sometimes have more than one backup from the same day, and having time helps with that.

    I use this on Debian Squeeze Linux, so, that is its intended platform. If you have a different platform, and it's not working right, feel free to provide me with the error and the OS. My Linux knowledge is limited to CentOS, Debian, and Ubuntu, though, so, I may not be able to help you myself.

    To set it up, put it wherever you want it, and grant it execution permissions. You may do so with something like this:

    Code:
    chmod u+x ./xf_backup.sh
    OR something like this:

    Code:
    chmod 700 ./xf_backup.sh
    If you don't do that, it won't be allowed to run. Also, make sure to edit it with the editor of your choice (nano, gedit, vi, etc.) and change the config information. I'd recommend just pointing it to your config file (which it will read and use for connection information) unless it's not working that way (for whatever reason), or if you're trying to back up from a non-XenForo database.

    Here's what it can do:

    Code:
    ./xf_backup.sh backup
    This will read the backup directory (next to the script, by default) and config file location, and back up the database based on the credentials it gets. If you don't fill in the config location, you can instead fill the database credentials, and it'll read those. If it connects to database correctly and saves the file, it won't output anything to sdout, so you won't get emailed by cron if your server's set up that way, even if you didn't send output to /dev/null.

    Code:
    ./xf_backup.sh backup-shell
    This will prompt you for all the database credentials one at a time, if you need to run it like this for some reason. It won't ask for your MySQL database password, instead it passes a blank password to MySQL, that way MySQL will ask you for it, and you won't have to have your password logged from command line.

    Running it without a parameter just outputs this:

    Code:
    XenForo Backup Script - by Mopquill
    To run this script, choose one of the parameters:
    ./xf_backup.sh backup - to automatically back up using config
    ./xf_backup.sh backup-shell - to have script prompt you for database info
    ...which tells you how to use it. This is so it can't accidentally be run and give you backups.

    If the times are coming out wrong, either your timezone is not properly set, or your server time is incorrect! To set your timezone, just fill it in in the config area. You can use stuff like US/Eastern, UTC, etc.

    I don't want to type all the timezones out, so, you can see all the timezones by running:

    Code:
    ls /usr/share/zoneinfo/
    You can check your server's time with the `date` command. If you don't have an environment variable setting your timezone, the hour might be off, but, pay attention to the minutes and seconds.

    The paths for mysqldump and gzip should be detected automatically, but if you don't have them (really just gzip, because if you don't have mysql, you have a very different problem!), you'll either need to ask your host to install them, or if you have shell and the permission to install, you'll want to do one of the following:

    Ubuntu, Debian, etc.

    Code:
    apt-get install gzip
    CentOS, Fedora, Redhat (all RHEL-based Linuxes), etc.

    Code:
    yum install gzip
    Next, you'll want to set it up in a cron, so backups are made automatically, but, that's up to you. From shell, use `crontab -e` to edit your user's cron. Here's what the command I use in mine:

    Code:
    cd /path/to/my/backup/dir && ./xf_backup.sh backup
    But, you could also do something like:

    Code:
    /path/to/my/backup/dir/xf_backup.sh backup
    Though, that may end up putting your backups in /home/your_username/ , or wherever your home directory is.

    I'm sure it's not as efficient or elegant as it could be, but it's not terribly complicated, and it works well. I've zipped it to save XenForo an eensy bit of space since it's plaintext. It should still be Linux-encoded (LF only), so you shouldn't have to worry about dos2unixing it or anything.

    I will be providing support for this in a limited sort of way. If you're getting errors on certain distros or something (I'm on Debian Squeeze), or if you want features added, or if something isn't working quite ideally, by all means, let me know. However, I'm not looking to teach anyone how to use shell (beyond what's already in this documentation) or write scripts, nor how to resolve dependencies or get things installed right. Basically, if it falls under actually *using* this script and nothing more, I'll try to help as best I can, provided I've got time. If it's something that falls under your duties as administrator, please don't expect much from me.

    Having said that, I'm pretty nice, I'll try to help with what I can, within reason.

    I'm also most definitely open to suggestions/improved code. Either way, let me know what you think. =]

    Disclaimer:This script is provided with the hope that it will be useful, but there are no guarantees nor any warranty whatsoever, expressed or implied, including but not limited to implied warranties of merchantability and fitness for a particular use or purpose. Use of this script is entirely at the risk of its user.

    cosjoy likes this.