Home > concrete5 for advanced users, concrete5 for developers > Doing daily automatic backup for concrete5

Doing daily automatic backup for concrete5

October 25th, 2010 Leave a comment Go to comments

Doing backups is always good. If something crashes, you can restore the website from backup. If your website is changing dynamically, even a backup made a week ago might be too outdated and you will lose a lot of information and efforts restoring the website from that state, that’s why it is important to make backups often enough to keep the loss of information as low as possible. If you backup your websites manually, you might spend a lot of time (especially if you often do it), so let’s try to automate this process as much as possible.

Note: This article assumes that concrete5 is running on Unix server. If you are using Windows server, this article is not for you unfortunately.

Concrete5 has a lot of files we don’t need to back up every day. For example, the core files that are located in ‘/concrete’ directory. The other files, such as /packages, /blocks, can be backupped manually as it is not updated very often. What we really need to backup regularly is:

  1. Database
  2. The /files/ directory which contains user uploaded files.

We prepared a bash script that actually does this work. Here is the code:

#!/bin/bash
# Shell script to backup concrete5 (database and /files/)
#

##########################
# CONFIGURATION
##########################

#
# DESTDIR - destination directory for backups. Backups will be stored there
# no trailing slash
DESTDIR="/home/user/backups"

#
# CONCRETE5_DIR - directory of concrete5 (where it is located)
# no trailing slash
CONCRETE5_DIR="/home/user/public_html"


#
# Database config
# If you don't remember this, check /config/site.php of your concrete5 installation
#

# Database User (see DB_USERNAME in config/site.php)
MYSQL_USER="concrete5"
# Database password (see DB_PASSWORD in config/site.php)
MYSQL_PASSWORD="concrete5"
# Database name (see DB_DATABASE in config/site.php)
MYSQL_DATABASE="concrete5"
# Database Host (see DB_SERVER in config/site.php)
MYSQL_HOST="localhost"

#########################################################
# DO NOT EDIT THE CODE BELOW UNLESS YOU KNOW WHAT YOU ARE DOING 
#########################################################

# detecting paths to executables
TARPATH="$(which tar)"
MYSQLDUMPPATH="$(which mysqldump)"
GZIPPATH="$(which gzip)"

# Get date in yyyy-mm-dd format
# (this format is convenient to sort the folders by name)
TODAY="$(date +"%Y-%m-%d")"


# create folder in backups/, for example: backups/2010-10-25/
DESTPATH="$DESTDIR/$TODAY"
mkdir $DESTPATH

# Destination file for the files backup
FILES_DESTFILE="$DESTPATH/files.$TODAY.tgz"
# Destination file for the database backup
DB_DESTFILE="$DESTPATH/db.$TODAY.gz"

# backuping database
$MYSQLDUMPPATH -u $MYSQL_USER -h $MYSQL_HOST -p$MYSQL_PASSWORD $MYSQL_DATABASE | $GZIPPATH -9 > $DB_DESTFILE

# backuping /files/
FILES_TO_BACKUP="$CONCRETE5_DIR/files"
$TARPATH -czf $FILES_DESTFILE $FILES_TO_BACKUP

You can download the bash script here.

You should edit the script in order to put correct file path and database connection details. See comments in the CONFIGURATION section of the script.

As soon as you edited the script and configured it for your website, put the script to somewhere on the server, it is better to store it in your home directory or in a separate folder, but not in web directory (not in public_html or httpdocs, etc).

Then you will need to set executable permissions on this script:

chmod 755 concrete5_backup.sh

To make sure everything is done properly, try to run the script:

./concrete5_backup.sh

If there is no error, it will make a new folder in your backups directory, for example: 2010-10-25. There will be 2 files in that folder: tgz file with the /files/ directory and .gz file with the database backup.

Finally, if it works, you can put the script on cron schedule. Open crontab for editing:

crontab -e

And add this line:

0 3 * * * /home/user/scripts/mysql_backup.sh

Where "/home/user/scripts/" should be replaced with the full path to the script on your server. The above line in crontab will make the script execute every day at 03:00 a.m. You can change that time to whatever time you want or keep it as it is.

  1. No comments yet.
  1. August 18th, 2013 at 09:32 | #1