Bash Script To Backup Your Website

|

I thought I’d share another one of my personal scripts. This one is a script that I use to backup this website including files and the MySQL database. It is very simple and straightforward so I won’t bother with long explanations. If you have any questions about it please feel free to comment. On tip I’ll leave you with is… schedule this (or your own) backup script to run regularly. I’m hosted on a linux server so it is an easy thing to setup a cron job to run this script once a week. Plus if you are really snazzy you could setup a local script to login to your server and download the backup files to your home system where you could archive them off to DVD-R or some other media. Anyway, here is the script… enjoy! 🙂

#!/bin/bash
# ------------------------------------------------------------
# File        : backup
# Author      : Jonathan Franzone
# Company     : http://www.franzone.com
# Date        : 04/16/2007
# Description : Backup web files and MySQL DB
# ------------------------------------------------------------

# ------------------------------------------------------------
# Setup Environment
# ------------------------------------------------------------
PDIR=${0%`basename $0`}
WEB_NAME=franzone.com
DBNAME=
DBUSER=
DBPASS=
DBHOST=localhost

# ------------------------------------------------------------
# File Backup
# ------------------------------------------------------------
BFILE="${WEB_NAME}.`date +%Y%m%d`.tar.gz"
echo "Backing WEB ${WEB_NAME} on `date` to ${BFILE}..."

cd ~/
tar -zcvf ${PDIR}/${BFILE} ${WEB_NAME}
cd ${PDIR}

# ------------------------------------------------------------
# Backup DB
# ------------------------------------------------------------
SFILE="${DBNAME}.`date +%Y%m%d`.sql"
BFILE="${DBNAME}.`date +%Y%m%d`.tar.gz"
echo "Backing DB ${DBNAME} on `date` to ${BFILE}..."

mysqldump -h ${DBHOST} -u ${DBUSER} -p${DBPASS} ${DBNAME} > ${SFILE}
tar -zcvf ${BFILE} --remove-files ${SFILE}

# ------------------------------------------------------------
# Done
# ------------------------------------------------------------
echo "Done"
exit 0