unix :: backup your webserver (ubuntu, debian, whatevs)

So, a while ago, a friend and I decided to buy a VPS that would host all our sites. He has his VMs and I have mine. Until he decides to expand his disk and cause some fatal system issues (which he denies), and my sites all go down in a burning heap of death.
It was then that I realised that my backup regime is not really all what its cracked up to be, a common issue, So I decided to get my act together. I modified a script mostly written (but not perfect) and integrated it with a dropbox uploader script which will send your backup files to dropbox, which is simply awesome!

So, first thing you’ll need is to head over to here and download the github script. As you know, I host any download on my own site, incase the source site goes down, you can get the version of the script at time of writing this article here.
Run the script, it will walk you through how to setup your dropbox account and get you to put in all the validation keys etc.

Secondly, create a directory called /backup (or wherever you want). Dump the contents of this script into it

#!/bin/sh
 
### System Setup ###
DIRS="/etc /var/www"
BACKUP=/backup/backup.$$
NOW=$(date +"%d-%m-%Y")
INCFILE="/root/tar-inc-backup.dat"
DAY=$(date +"%a")
FULLBACKUP="Sun"
FILETYPE=""
 
### MySQL Setup ###
MUSER="dbUser"
MPASS="dbPassword"
MHOST="dbHost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
 
### Other stuff ###
EMAILID="blah@blah.com"
 
### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :
 
### See if we want to make a full backup ###
if [ "$DAY" = "$FULLBACKUP" ]
then
echo "Taking FULL Backup";
FILETYPE = "Full"
  FTPD="//full"
  FILE="fs-full-$NOW.tar.gz"
  tar -zcvf $BACKUP/$FILE $DIRS
else
echo " Taking Incremental  Backup"
FILETYPE="Incremental"
 i=$(date +"%Hh%Mm%Ss")
  FILE="fs-i-$NOW-$i.tar.gz"
  tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
fi
 
### Start MySQL Backup ###
# Get all databases name
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
 FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
 $MYSQLDUMP -u $MUSER -h $MHOST --single-transaction -p$MPASS $db | $GZIP -9 > $FILE
done
 
##backup to dropbox
/backup/dropbox_uploader.sh upload $BACKUP "Backup_"$NOW"_"$FILETYPE
T=/backup/backup.passed
echo "Date: $(date)">$T
echo "Hostname: $(hostname)" >>$T
echo "Backup Taken Succesfully" >>$T
echo "Filename Is Backup_"$NOW"_"$FILETYPE >> $T
mail -s "Backup Succesfull" "$EMAILID" <$T
 
### Find out if ftp backup failed or not ###
if [ "$?" = "0" ]; then
 rm -f $BACKUP/*
else
 T=/backup/backup.fail
 echo "Date: $(date)">$T
 echo "Hostname: $(hostname)" >>$T
 echo "Backup failed" >>$T
 mail  -s "BACKUP FAILED" "$EMAILID" <$T
 rm -f $T
fi

Setup a crontab to run whenever you want, I set mine for 2.30 am every day.
Make sure you make the script executable

chmod a+x backupScript.sh

So this script will take a full backup every Sunday, and incremental every day and upload it to your dropbox. You can change the file names or whatever you want, you could even put an rm -rf command in the weekly command to delete backup.* before it starts (to keep your directory from filling up). But this is a great foundation to get it working.

enjoy!

2 Comments

  • Borek
    May 12, 2015 - 12:06 am | Permalink

    It’s always the friend….

    • sameh
      January 5, 2016 - 3:37 am | Permalink

      Always, always the “friend”.

  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    You may 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>