Script to Backup your websites and databases

T

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.

Web-backup

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:

[root@server][~]
$ wget http://sh.beadmin.2tl.eu/www-backup.sh

[root@server][~]
$ chmod x www-backup-local.sh

[root@server][~]
$ mv www-backup.sh www-backup-local.sh

[root@server][~]
$ chmod x www-backup-local.sh

[root@server][~]
$ mkdir -p /backups/www/
 

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

$ nano www-backup-local.sh

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
SQLUSER=root
SQLPASS=your_password
SQLDBS=mysql,roundcubemail # ,anyotherdatabaseyouwanttobackup
 

Save and exit.

Test run:

$ ./www-backup-local.sh

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/www-backup-local.sh /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 www-backup-local.sh www-backup-ftp.sh

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

BACKUPTYPE=0
# Set following ftp details if you have selected to use ftp as backup type
# ftp server
FTPSERVER=youftpserver
# ftp username
FTPUSER="your_username"
# ftp password
FTPPASS=yourpassword
# ftp remote directory
FTPDIR=/your_remote_dir
 

Save and exit.

Test run:

$ ./www-backup-ftp.sh

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

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

 

Restore from backup

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

[root@server][/]
$ cd /backups/www

[root@server][/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:

[root@server][/]
$ cd /backups/www

[root@server][/backups/www]
$ tar -xzf sqlbackup.20-08-2013.tgz

[root@server][/backups/www]
$ mysql -u root -pFukoFuko roundcubemail < roundcubemail.sql
 

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