Logo Background

Backup Script For Linux-Apache-PHP-MySQL

  • By on August 22, 2008 | 2 Comments

    If you have any web based system running on LAMP (Linux-Apache-PHP-MySQL), the script below which runs on .sh extension file will give you a full backup of your application and database file. Remember to perform some restoration just to cross check the backup script was running without any problem

    LAMP Based System Backup Script

    #!/bin/bash
    #
    # Full backup of website files and database content.
    #
    # A number of variables defining file location and database connection
    # information must be set before this script will run.
    # Files are tar'ed from the root directory of the website. All files are
    # saved. The MySQL database tables are dumped without a database name and
    # and with the option to drop and recreate the tables.
     
    # Database connection information
    dbname="db_name" # (e.g.: dbname=joomladb)
    dbhost="localhost"
    dbuser="db_user" # (e.g.: dbuser=joomlauser)
    dbpw="db_password" # (e.g.: dbuser password)
     
    # Website Files
    webrootdir="/home/user/www" # (e.g.: webrootdir=/home/user/public_html)
     
    # Variables
     
    # Default TAR Output File Base Name
    tarnamebase=backup-
    datestamp=`date +'%m-%d-%Y'`
     
    # Execution directory (script start point)
    startdir=`pwd`
    logfile=$startdir"/fullsite.log" # file path and name of log file to use
     
    # Temporary Directory
    tempdir=$datestamp
     
    # Input Parameter Check
     
    if test "$1" = ""
    then
    tarname=$tarnamebase$datestamp.tgz
    else
    tarname=$1
    fi
     
    # Begin logging
    #
    echo "Beginning drupal site backup using fullsitebackup.sh ..." > $logfile
    #
    # Create temporary working directory
    #
    echo " Creating temp working dir ..." >> $logfile
    mkdir $tempdir
     
    sleep 60
     
    #
    # TAR website files
    #
    echo " TARing website files into $webrootdir ..." >> $logfile
    cd $webrootdir
    tar cf $startdir/$tempdir/filecontent.tar .
     
    sleep 60
     
    #
    # sqldump database information
    #
    echo " Dumping drupal database, using ..." >> $logfile
    echo " user:$dbuser; database:$dbname host:$dbhost " >> $logfile
    cd $startdir/$tempdir
    mysqldump --user=$dbuser --password=$dbpw --add-drop-table $dbname > dbcontent.sql
     
    sleep 60
     
    #
    # Create final backup file
    #
    echo " Creating final compressed (tgz) TAR file: $tarname ..." >> $logfile
    tar czf $startdir/$tarname filecontent.tar dbcontent.sql
     
    sleep 60
     
    #
    # Cleanup
    #
    echo " Removing temp dir $tempdir ..." >> $logfile
    cd $startdir
    rm -r $tempdir
     
    sleep 60
     
    #
    # Exit banner
    #
    endtime=`date`
    echo "Backup completed $endtime, TAR file at $tarname. " >> $logfile
     
    #rm $logfile
    #rm $tarname
    Previous
    Next
    » Unix Backup Specific File Extension
  1. #1 Jayce
    June 14, 2009 8:36 am

    So we still need to download the TAR file to local computer, right?

    Post ReplyPost Reply
  1. #2 Jing Hong
    June 14, 2009 8:49 pm

    Yes Jayce, you still need to FTP down the TAR file in local computer 🙂

    Post ReplyPost Reply
Leave a Comment