How To Back Up a Web Server

by on August 2, 2008 · 21 comments· LAST UPDATED August 2, 2008

in , ,

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:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 21 comments… read them below or add one }

1 Diya August 2, 2008 at 12:55 pm

Excellent and simple to the point.

Reply

2 S. Nilesh August 2, 2008 at 4:15 pm

superb man… a marvellous script.

Reply

3 dj August 2, 2008 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.

Reply

4 nixCraft August 2, 2008 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

Reply

5 Liju August 3, 2008 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.

Reply

6 Samir August 4, 2008 at 2:59 pm

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

Reply

7 IndianWebhost January 7, 2009 at 12:35 pm

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

Reply

8 devarajan June 8, 2009 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 .

Reply

9 Casey July 17, 2009 at 1:16 pm

Hi Vivek,

Thanks for the great script.

Is there anyway to add an exclude directory list in this script? Thanks

Kind regards

Casey

Reply

10 nixCraft July 17, 2009 at 1:24 pm

Find this line

# Store backup path
BACKUP="/backup/$NOW"

Append:

EXCLUDE_CONF="/root/exclude.files.conf"

Find Line:

# Backup websever dirs
$TAR -zcvf ${BACKUP}/${BFILE} "${DIRS}"

Update it as follows:

# Backup websever dirs
$TAR  --exclude-from=${EXCLUDE_CONF} -zcvf ${BACKUP}/${BFILE} "${DIRS}"

Save and close the file. Create/root/exclude.files.conf file:

vi /root/exclude.files.conf

To exclude all log files and a directory called /home/vivek/mp3 enter:

/var/www/logs/*.logs
/home/vivek/mp3

Reply

11 Casey October 1, 2009 at 10:32 am

Hi Vivek,

Thanks for the above. I have implemented this and it has been working fine for many months now.

Just recently however I have noticed a problem with the last part of the code. I am not sure if this has been going on all the time or not, but I have was just doing a general check up on the backups and noticed an error.

The section below is the culprit:
### Find out if ftp backup failed or not ###
if [ “$?” == “0” ]; then
rm -f $BACKUP/*
else
T=/tmp/backup.fail
echo “Date: $(date)”>$T
echo “Hostname: $(hostname)” >>$T
echo “Backup failed” >>$T
sendmail “BACKUP FAILED” “$EMAILID” <$T
rm -f $T
fi

I actually get an error:
[: 83: ==: unexpected operator

As you can see I have amended the mail part to sendmail as the other one was not working and hence this is the reason why I spotted the other problem.

Do you know why this is happening? I have unbuntu 8.04.

Any help much appreciated. Thanks

Casey

Reply

12 nixCraft October 1, 2009 at 10:49 am

look like problem with the if statement.

Reply

13 Casey October 2, 2009 at 4:35 pm

Hi Vivek,

I believe it is the == that is not working. Obviously this should mean equal to but for some reason I just get an error. I have read around and people have stated to use the -eq instead of the == but this is only for numbers and I have tried this.

The statement below is what I have added for testing purposes. I have put the result at the bottom. I am completely baffled!

ead #This is to cause an error in the script

if [ “$?” -eq “0” ]; then
echo “This is good before $?”
else
echo “This is bad before $?”
fi

### Find out if ftp backup failed or not ###
if [ “$?” -eq “0” ]; then
rm -f $BACKUP/*
echo “All Good $?”
else
T=/tmp/backup.fail
echo “Date: $(date)”>$T
echo “Hostname: $(hostname)” >>$T
echo “Backup failed” >>$T
sendmail “BACKUP FAILED” “$EMAILID” <$T
rm -f $T
echo "ALL Bad $?"
fi

if [ "$?" -eq "0" ]; then
echo "This is good after $?"
else
echo "This is bad after $?"
fi

Result from this is:
test.sh: 42: ead: not found
This is bad before 1
All Good 0
This is good after 0

I am completely stumped. Any ideas? Thanks

Casey

Reply

14 Chris March 3, 2010 at 9:52 pm

Spectacular script. I don’t suppose you have a version that also restores? :)

Reply

15 dhanesh mane January 19, 2011 at 5:05 am

perfect script. I was looking for something like this only and found the correct script works perfect on ubuntu.

Reply

16 Leon October 18, 2011 at 10:16 am

Excellent script, thanks for sharing.

Reply

17 Lo January 26, 2012 at 2:35 pm

Hey Vivec,

this is an excellent script. on Redhat, it is asking me for a password. how can i deal with that.

Reply

18 nixCraft January 26, 2012 at 6:40 pm

Setup ssh keys as described in step #2.

Reply

19 Quran September 29, 2012 at 6:45 am

Thank you for this script, I will use it to setup my backups.

Reply

20 Chris November 4, 2012 at 6:11 pm

Hey, great script. This has been working well until the past week or so. Have Amazon changed something? as I keep getting broken pipe message now. Anyone else experiencing this problem?

Reply

21 Babin Lonston July 16, 2013 at 11:41 am

Will u please give me a script to backup my client machines to centos Server , All the Client machines are ubuntu 12.04 LTS and server machine is centos 6.3 , i want to backup the systems which have ip address 192.168.1.30 and 192.168.1.50 want to backup the whole PC and it want to be saved under /backup folder of centos

Reply

Leave a Comment

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

Previous Faq:

Next Faq: