Hi Geeks
Tooday I will share a simple shell backup script. The purpose of this backup script is to enable you to backup your server data quickly without doing much. You can then FTP your www and Mysql database to a remote server.
I have chosen to download my backup my data to my local linux server. the local path is /backup
So firstly create a local store
#mkdir /backup
Now Create the backup script in the global bin directory
#cd /usr/local/bin
#vi backup
Add the backup script below and edit the variables to suit your server
Save the file. Set the permissions to be executed you can run a 777 should you wish then finally test it.
Before testing if you opted for FTP remote transfer enable ncftpd
#apt-get install ncftpd
Happy Backups
Simply edit your choices.
#################################################
## ##
## Backup Procedure Start ##
## ##
#################################################
LOG="/tmp/log.txt"
EMAIL="me@email.com"
CLIENTMSG="ServerName-Bkup"
DATESTAMP="`date '+%a%H'`"
DATEMKDIR="`date '+%A'`"
CODE="/var/www/"
BAK="/backup"
NOW=$(date +"%m-%d-%Y") # mm-dd-yyyy format
FILE="fs-full.tar.gz"
#* MySQL login user name *#
MUSER="root"
#* MySQL login PASSWORD name *#
MPASS="somepass"
#* MySQL login HOST name *#
MHOST="127.0.0.1"
#* System binaries *#
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
TAR="$(which tar)"
FTP="$(which ftp)"
ZIP="$(which zip)"
#*FTP Transfer Optional
### FTP server Setup ###
FTPD="My_Backup_folder/"
FTPU="myusername"
FTPP="mypassword"
FTPS="some.ftp.server"
NCFTP="$(which ncftpput)"
#############################################################################
# #
#Starting Backup System - Backup creates folders for the DAY #
# I.E. \\server\backup\DAY Of The Week (Tuesday) #
#############################################################################
#** Clean Up Load Files
/bin/rm $BAK/$DATEMKDIR/*
/bin/echo "Old data cleaned up" >> $LOG
/bin/echo "Database Backup Time Started" >> $LOG
/bin/date >> $LOG
###Mysql backup All databases
#$MYSQLDUMP $DBNAME > /tmp/$DBNAME.sql
/bin/echo "Database Backup Time Completed" >> $LOG
/bin/date >> /tmp/log.txt
/bin/mkdir $BAK/$DATEMKDIR
#* Start Backup Mysql
# get all database listing
#DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
DBS="$($MYSQL -u $MUSER -h $MHOST -Bse 'show databases')"
# start to dump database one by one
for db in $DBS
do
FILE=$BAK/$DATEMKDIR/mysql-$db.gz
# gzip compression for each backup file
$MYSQLDUMP -u $MUSER -h $MHOST $db | $GZIP -9 > $FILE
done
/bin/echo "Database Backup Time Completed" >> $LOG
/bin/date >> /tmp/log.txt
#*Code Base Backup
$TAR zcvf $BAK/$DATEMKDIR/www.tgz $CODE
###ETC System Backup
$TAR zcvf $BAK/$DATEMKDIR/etc.tgz /etc
#### FTP Dump
cd $BAK
### Dump backup using FTP ###
#Start FTP backup using ncftp
ncftp -u"$FTPU" -p"$FTPP" $FTPS<
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BAK/$DATEMKDIR/
mput *
quit
EOF
#**Mail
/bin/cat $LOG | mail -s $CLIENTMSG $EMAIL