September 11, 2015

Scripts for backup website

#######################################################################
## AURELIEN HUSSON ##
## Script   and  ##
## Info : ##
## Exemple : website find in /home/www/website/public_html/ ##
## backup in /home/www/website/backup/ ##
## The script in /home/www/website/backup-site.sh ##
#######################################################################
#######################################################################
## AUTOMATIC CRON ##
## For automatiq backup ex : eveyday’s 00:01 AM ##
## 01 00 * * * /home/linuxtricks/backup-script.sh ##
#######################################################################
#! /bin/bash
echo We define the system variables
path=`dirname $0`
pathsav=”$path/backup”
site=”www.website.com”
wwwpath=”/home/website/”
wwwfolder=”public_html”
dbhost=”localhost”
dbuser=”user”
dbpasswd=”password”
dbname=”site”
dbrestohost=”$dbhost”
dbrestouser=”$dbuser”
dbrestopasswd=”$dbpasswd”
dbrestoname=”siterestore”
maxjourssauvegarde=”30″
echo Defined variables.
echo “”
echo Checking Programs
progtar=$(which tar)
progtar=$(echo $?)
progbzip2=$(which bzip2)
progbzip2=$(echo $?)
progbzcat=$(which bzcat)
progbzcat=$(echo $?)
progmysqdump=$(which mysqldump)
progmysqdump=$(echo $?)
progmysql=$(which )
progmysql=$(echo $?)
if [ $progtar -eq 0 ] && [ $progbzip2 -eq 0 ] && [ $progbzcat -eq 0 ] && [ $progmysqdump -eq 0 ] && [ $progmysql -eq 0 ]
then
echo All programs are available on the server
else
echo It lacks a program to run the backup script. Check tar, bzip2, bzcat, mysqldump and mysql
exit 1
fi
echo Checking Completed programs
echo “”
echo Installing the script
cd “$path”
mkdir “$pathsav” 2>/dev/null
echo Script initialising
echo “”
echo The backup date we define
dte=$(date +%Y-%m-%d–%H-%M-%S)
echo Backup date defined on $dte
echo “”
echo We launch Backup
echo “”
echo We launch archive website $site
cd “$wwwpath”
tar cf “$pathsav/$site-$dte.tar” “$wwwfolder”
cd “$path”
if [ $? -eq 0 ]
then
echo Archiving website ends : “$pathsav/$site-$dte.tar”
else
echo Failed archive website
exit 2
fi
echo “”
echo It will launch the backup database
mysqldump -u “$dbuser” -p”$dbpasswd” -h “$dbhost” “$dbname” > “$pathsav/$site-$dte.sql”
if [ $? -eq 0 ]
then
echo Backed up database.
else
echo Failed to backup the database
exit 3
fi
echo “”
echo We compress the archive website $site
bzip2 -z “$pathsav/$site-$dte.tar”
if [ $? -eq 0 ]
then
echo Compression archive of the finished website : “$pathsav/$site-$dte.tar.bz2”
else
echo Failed compression archive website
exit 4
fi
echo “”
echo We compress the database.
bzip2 -z “$pathsav/$site-$dte.sql”
if [ $? -eq 0 ]
then
echo Database compressed .
else
echo Failed compressing the database
exit 5
fi
echo “”
echo BACKUP ENDED
echo ———————
echo PURGE OLD BACKUP
find “$pathsav” -name “$site*” -mtime +”$maxjourssauvegarde” -exec echo ‘{}’ \;
find “$pathsav” -name “$site*” -mtime +”$maxjourssauvegarde” -exec rm ‘{}’ \;
if [ $? -eq 0 ]
then
echo Old backups deleted.
else
echo Failed to delete old backups.
exit 6
fi
echo ——————–
echo CHECKING THE BACKUP
echo We check archive “$pathsav/$site-$dte.tar.bz2”
tar tf “$pathsav/$site-$dte.tar.bz2” 2>/dev/null 1>/dev/null
if [ $? -eq 0 ]
then
echo Backup Site Files : OK
else
echo An error occurred during restore. The backup is probably damaged.
fi
echo “”
echo We check the backup database : “$pathsav/$site-$dte.sql.bz2”
bzcat “$pathsav/$site-$dte.sql.bz2” | mysql -u “$dbrestouser” -p”$dbrestopasswd” -h “$dbrestohost” $dbrestoname
if [ $? -eq 0 ]
then
echo Backing up the database : OK
else
echo An error occurred during restore. The backup is probably damaged.
fi
echo TEST COMPLETED RESTORATION

0 comments:

Post a Comment