Backup a drupal site - both database and site files through cron

I wrote a little linux bash script that I get cron to run each night to create a backup of my drupal site. I may expand it later to include auto deletion of older files and database dumps, but for the meantime I will just manage the backup files manually.

Basically create a file and call it something like site_backup.sh

#!/bin/bash
# Shell script to backup Drupal MySql database and files
# -------------------------------------------------------------------------
# @file
# Backup Drupal MySql database and live site files
#
# @author Gareth Alexander
# -------------------------------------------------------------------------

Client="myClient" # CLIENTNAME
MyDBUser="myClientUser" # USERNAME
MyDBPass="myClientPass" # PASSWORD
MyDBHost="localhost" # Hostname
MyDB="myClientDb" # Database name

# Linux bin paths, change this if it can't be autodetected via which command
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
TAR="$(which tar)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"


# Backup Dest directory, change this if you have some other location
DBBACKUPDIR=/data/home/$Client/db_dumps
FILEBACKUPDIR=/data/home/$Client/backups

# Backup Dest directory, change this if you have someother location
LIVEDIR=/data/home/$Client/live/public_html

# Get data in ddmmyyyy-hhmm format
NOW="$(date +"%d%m%Y-%H%M")"

# Files to store backup archive files
DBFILE=$MyDB-$NOW-db.sql
BACKUPFILE=$Client-$NOW-dir.tar.gz

# Check for backup directories and create them if they don't exist
if [ ! -d "$DBBACKUPDIR" ] ; then # db_dumps dir does not exist so create it
mkdir ${DBBACKUPDIR}
fi
if [ ! -d "$FILEBACKUPDIR" ] ; then # backups dir does not exist so create it
mkdir ${FILEBACKUPDIR}
fi

$MYSQLDUMP --host=$MyDBHost --user $MyDBUser --password=$MyDBPass --opt $MyDB > $DBBACKUPDIR/$DBFILE
$TAR -cvzpf $FILEBACKUPDIR/$BACKUPFILE $LIVEDIR # c -create, v - verbose, -p restore the files to their original modes, f - use the filename arguement, z - gzip

Comments

Thanks for sharing your views.
Great blog here.. It’s hard to find quality writing like yours these days. I really appreciate people like you.
Regards,

Yeah, I'm sure

Thanks for sharing your thoughts. I really appreciate your efforts and I will be waiting for your next post thank you once again.

I get pleasure from, lead to I discovered exactly what I used to be looking for.
You have ended my four day long hunt! God Bless you man.
Have a great day. Bye

I still have no clue on how to find such changes in the sites these days. Each sites have different codings and has its own unique ways to re structure it. so it is very difficult to mess with those.