How To Back Up a Web Server

Q. I'm using Red Hat Enterprise Linux based Apache web server. How do I backup my Apache webserver, MySQL and PostgreSQL database to another disk called /backup and then copy it to other offsite backup ssh server called backup.example.com?

A. There are many tools under Linux / UNIX to backup a webserver. You can create a simple shell script to backup everything to /backup directory. You can also copy /backup directory content offsite using ssh and scp tool.

Step # 1: Create /root/backup.sh script

Use the following shell script (download link):

#!/bin/bash
# A Simple Shell Script to Backup Red Hat / CentOS / Fedora / Debian / Ubuntu Apache Webserver and SQL Database
# Path to backup directories
DIRS="/home/vivek/ /var/www/html/ /etc"
 
# Store todays date
NOW=$(date +"%F")
 
# Store backup path
BACKUP="/backup/$NOW"
 
# Backup file name hostname.time.tar.gz
BFILE="$(hostname).$(date +'%T').tar.gz"
PFILE="$(hostname).$(date +'%T').pg.sql.gz"
MFILE="$(hostname).$(date +'%T').mysql.sq.gz"
 
# Set Pgsql username
PGSQLUSER="vivek"
 
# Set MySQL username and password
MYSQLUSER="vivek"
MYSQLPASSWORD="myPassword"
 
# Remote SSH server setup
SSHSERVER="backup.example.com" # your remote ssh server
SSHUSER="vivek"                # username
SSHDUMPDIR="/backup/remote"    # remote ssh server directory to store dumps
 
# Paths for binary files
TAR="/bin/tar"
PGDUMP="/usr/bin/pg_dump"
MYSQLDUMP="/usr/bin/mysqldump"
GZIP="/bin/gzip"
SCP="/usr/bin/scp"
SSH="/usr/bin/ssh"
LOGGER="/usr/bin/logger"
 
# make sure backup directory exists
[ ! -d $BACKUP ] && mkdir -p ${BACKUP} 
 
# Log backup start time in /var/log/messages
$LOGGER "$0: *** Backup started @ $(date) ***"
 
# Backup websever dirs
$TAR -zcvf ${BACKUP}/${BFILE} "${DIRS}"
 
# Backup PgSQL
$PGDUMP -x -D -U${PGSQLUSER} | $GZIP -c > ${BACKUP}/${PFILE}
 
# Backup MySQL
$MYSQLDUMP  -u ${MYSQLUSER} -h localhost -p${MYSQLPASSWORD} --all-databases | $GZIP -9 > ${BACKUP}/${MFILE}
 
# Dump all local files to failsafe remote UNIX ssh server / home server
$SSH ${SSHUSER}@${SSHSERVER} mkdir -p ${SSHDUMPDIR}/${NOW}
$SCP -r ${BACKUP}/* ${SSHUSER}@${SSHSERVER}:${SSHDUMPDIR}/${NOW}
 
# Log backup end time in /var/log/messages
$LOGGER "$0: *** Backup Ended @ $(date) ***"

Customize it according to your needs, set username, password, ssh settings and other stuff.

Step # 2: Create ssh keys

Create ssh keys for password less login from your server to another offsite server hosted at your own home or another datacenter. See following faqs for more information:

Step #3: Create Cron job

Setup a cronjob to backup server everyday, enter:
# crontab -e
Append following code to backup server everyday at midnight:
@midnight /root/backup.sh

Further readings:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 8 comments… read them below or add one }

1 Diya 08.02.08 at 12:55 pm

Excellent and simple to the point.

2 S. Nilesh 08.02.08 at 4:15 pm

superb man… a marvellous script.

3 dj 08.02.08 at 6:43 pm

Nice script. Are you concerned about pwd in plaintext? Falisafe should be Failsafe in comment :-) We use a combo for daily and monthly, system and user. And use tape and disk. We looked at DVD UDF packet writing format, but decided against it.

Depending on your needs ZAmanda is very nice. Like any product the configuration took a little to understand, but it wasn’t bad, and I believe they also have a GUI now. We liked it over Baccula because it uses tar or dump.
http://www.zmanda.com/

I didn’t find the O’Reilly book, “Backup and Recovery” very helpful. It’s too high level, and the good stuff is all on their website or available online elsewhere.

4 vivek 08.02.08 at 6:50 pm

Are you concerned about pwd in plaintext?
Not much.. if attacker can read my script password, he/she has full access to system. There is no simple solution for password. Usually, I lockdown httpd, named, other service is jail. Continues monitoring and patching prevents lots of bad stuff.

HTH

5 Liju 08.03.08 at 4:24 pm

Excelent Notes.. Vivek

resync command will be best if you have limited bandwidth on backup server.

Also lftp is also support to incremental backup in between the linux servers which support only ftp access in to it.

6 Samir 08.04.08 at 2:59 pm

Concise and useful, a well written script.
Thanks for the effort.

7 IndianWebhost 01.07.09 at 12:35 pm

Nice script. Thanks for sharing your resource.
Are there any scripts for taking backups for windows server?

8 devarajan 06.08.09 at 6:07 am

hi , we r using apache for mailserver . just i want to back the mails …..
could u can give me any scripts for me .

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , , , ,

Previous post: BSD FTP-Proxy: PF Firewall Allow Outgoing Active / Passive FTP Connections

Next post: FreeBSD php5-posix-5.2.6 has known vulnerabilities error – Stop in /usr/ports/sysutils/php5-posix.