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

Related Posts:

  • using shell script to automate install VPS linux Vì tính chất công việc, nên đôi khi mình phải setup khá nhiều VPS để dùng. Đặc biệt là các VPS linux, tuy nhiên chỉ cần setup Apache + PHP + mysql + phpMyAdmin là đủ cho 1 cuộc tình Vì phải lặp đi lặp lại 1 mớ lệnh, mà … Read More
  • Script backup zimbra mail##!/bin/bash clear echo Start time of the backup = $(date +%T) before="$(date +%s)" ## Backup Format FORMAT=tgz ## Backup location ZBACKUP=/srv/backup/ ## Folder name for backup and using date DATE=`date +"%d%m%y"` ## Bac… Read More
  • Script Tự động sao lưu toàn bộ website WordPress Thông thường, khi mình muốn di chuyển hoặc sao lưu một website sử dụng WordPress thì sẽ sử dụng plugin Duplicator. Tuy nhiên, có một số trường hợp server bị lỗi, website không truy cập trực tiếp được hoặc bạn muốn sao l… Read More
  • Scripts for backup website ####################################################################### ## AURELIEN HUSSON ## ## Script backup website and Database ## ## Info : ## ## Exemple : website find in /home/www/website/publ… Read More
  • MYSQL BAKUP USING SHELLS SCRIPTS Kindly find the MYSQL shells scripts in given below.-------------------------------------------------------------------------------------------------------#! /bin/bash# You are free to modify and distribute this code,# so… Read More

0 comments:

Post a Comment