Script to Backup your websites and databases


his tutorial works for CentOS only. For your specific OS tutorial use the tags or serach function in the sidebar area.


Backups are most important thing -just after hardening security - to maintain by sysadmin. We could define two types of backup which will do. First, a Full-system backup (all system files,user files,software libraries,temporary dirs.. and so on) and - call it - Web-backup (website files + database dumps). For now we will take care of the second one.


Imagine situation that your WordPress installation got broken because of some g.r.e.a.t. plugin messed with site files or simply you just overwrite your file with wrong letters pressed.
Backups it's a must have!

We will be using script originally written by Anil Dewani (ver 19 Jan 2011) - this script is just right for a newborn admin, it will backup both (web directory and mysql database) it can:

+ backup to local directory
+ send backup via FTP

- can't make incremental backups
- no SSH/RSYNC ...
- no auto restore

We will set two backups:

Once a day   --to--> local directory /backups/
Once a week  --to--> remote FTP directory


Local Web-Backup (daily)

Get the script:

$ wget

$ chmod x

$ mv

$ chmod x

$ mkdir -p /backups/www/

Now we need to edit some configuration values inside this script:

$ nano

DIR=/var/www # yes all our websites are there
BACKUPTYPE=1 # yep local this time
BACKUPDIR=/backups/www/ # where storage backups

SQLBACKUP=1 # we want SQL backup also
SQLDBS=mysql,roundcubemail # ,anyotherdatabaseyouwanttobackup

Save and exit.

Test run:

$ ./

If backup created successfully add to Cron
assuming the backup script is in /root/ we make a symbolic link instead of makin'n'creatin'

$ ln -s /root/ /etc/cron.daily/www-backup-local


FTP Web-Backup (weekly)

The script uses ncftpput command so we have to install NcFTP

$ yum install ncftp

We need to clone previous script, as it not takes any parameters with ease

$ cp

Now we want to edit script and change the settings to work with FTP

# Set following ftp details if you have selected to use ftp as backup type
# ftp server
# ftp username
# ftp password
# ftp remote directory

Save and exit.

Test run:

$ ./

If backup created successfully add to Cron
assuming the backup script is in /root/

$ ln -s /root/ /etc/cron.weekly/www-backup-ftp


Restore from backup

For web files I reccomend using Midnight Commander (mc) or simply

$ cd /backups/www

$ tar -xzf dirbackup.20-08-2013.tgz

and then copy neede files wherever you want.

MySQL database restore

mysql -u root -p[root_password] [database_name] < dumpfilename.sql

First we must extract sql dumps from archive then restore each database using 'mysql' command, let's assume
database user is root and his password is 'FukoFuko' and we're restoring a Roundcube (roundcubemail) database dump:

$ cd /backups/www

$ tar -xzf sqlbackup.20-08-2013.tgz

$ mysql -u root -pFukoFuko roundcubemail < roundcubemail.sql

You could also restore via phpMyAdmin - but that's a lot quicker.