Backup Script for your Web and Mysql
User Rating: / 1
PoorBest 

I’ve been hosting a few websites under my CentOS server and it’s always a good policy to backup your data.  Since I do not have a backup software I thought about building a script that would backup all my websites including the Mysql database.  My other concern is not only backing up but also have a point in time I can go back if needed.

I will share this for those of you, interested in a not fancy solution, with a very simple ksh script.  Just copy the script, and adjust it to your reality and get the script to run in  your root crontab. Click in "Read More..." to see the script... 

#!/bin/ksh

# This script will backup your Joomla website and databases
# This will keep 5 different backups of the website and database
# Please feel free to use it or change it.

# Paths to webroot, mysql databases and backup path
MYSQLDBSPATH="/var/lib/mysql"
WEBPATH="/web"
BACKUPPATH="/export/software/backups"
NUMBEROFVERSIONS=5
DATE=$(date +"%d_%b_%Y_%H_%M")
CURRENTBACKPATH="${BACKUPPATH}/${DATE}"

backup() {

        mkdir -p $CURRENTBACKPATH
        mkdir -p ${CURRENTBACKPATH}/mysqldb
        mkdir -p ${CURRENTBACKPATH}/web
        echod "Performing Backup to ${CURRENTBACKPATH}"
        cp -pr ${WEBPATH}/* ${CURRENTBACKPATH}/web/
        cp -pr ${MYSQLDBSPATH}/* ${CURRENTBACKPATH}/mysqldb/
        cd ${CURRENTBACKPATH}
        tar -cf ${DATE}.tar *
        rm -rf mysqldb
        rm -rf web
        echod "Backup was made to disk"
}

keepversions() {
        echod "Maintaining Versions Function"
        NUMBERV=$(ls -ltr /export/software/backups | grep -v total | wc -l)
        echod "$NUMBEROFVERSIONS versions will be kept"
        if [ $NUMBERV -gt $NUMBEROFVERSIONS ]
        then
           cd $BACKUPPATH
           TOREMOVE=$(ls -ltr | grep -v total | head -1 | awk '{print $9}')
           echo $(date +"%b %d %H:%M:%S ") Removed $TOREMOVE
           rm -rf $TOREMOVE
        else
           echo $(date +"%b %d %H:%M:%S ") Nothing was removed
        fi
        echod "Finished Version Maintenance"

}

echod() {
        echo $(date +"%b %d %H:%M:%S ") $1
}



exec >> /var/log/backup.log
echo "############################################"
echo "############## Backup Date: $(date) ################"
echod "Starting Backup Sript"
echod "Stopping Mysql"
/etc/init.d/mysqld stop
echod "Stopped Mysql"
backup
keepversions
echod "Start Mysql"
/etc/init.d/mysqld start
echod "Finished Backup Script"
echo "############################################"
 

 
Main Menu
Profile
MaTaPorKoZ 2009