Update: ISPConfig3 Backup script – local server

This is updated backup script which can be run on ISPConfig3 server (locally) and copy files to already mounted drive.

Original post_ http://blog.it-podrska.info/2009/08/backup-remote-linux-mysql-and-data/

Variables which are removed:

sshuser=”remote_username_root” # remote ssh username
sshhostname=”remote_hostname” # remote hostname
sshport=”remote ssh port” # remote ssh port

variables which you mast verify:

path=/disk2/pub/Backup
dbuser_x=root #mysql root password
dbpassword_x=mysqlrootpass #mysql password
remotepath=/backup # remote path where will be temporary files
lista=/home/franz/bin/lista.txt #path and filename of txt file where are stored remote directory paths

and here is the script:


#!/bin/bash
#(c) ISPBackup3 by Franz@franz-net.info
#local backup on mounted directory
#define local path
#This path must exist, this is destination path
path=/disk2/pub/Backup1
#define logfile
logfile=$path/log/ISPconfig3-backup-`date +'%F'`
#define week number
week_x=`date +'%V'`
#define locapl final path path/weeknumber/date
dirname=$path/$week_x/`date +'%F'`
#define day of week number
DF_X=`date +'%u'`
#define curent date and time: 2009-08-16_23-01
datum_x=`date +'%F_%I-%M'`
#define database access
dbuser_x="root"
dbpassword_x="password"
#define local temp path, must exist
remotepath="/backup"
#define list of directories in format:
#name /path
lista="/home/franz/bin/lista.txt"
#define temp file for email text, and email address for sending report
zamail="/tmp/zamail$datum_x.txt"
email="franz@franz-net.info"
function check_log {
if [ -d $path/log ]; then
log "Log directory exist"
else
mkdir $path/log
log "Log directory creted"
fi
}
function log {
curenttime_x=`date +"+%Y-%m-%d %H:%M:%S"`
echo "$curenttime_x - $1 $2 $3 $4 $5 $6 $7 $8 $9" >> $logfile
}
function check_dirs {
log "Cheching local directory for saving files..."
if [ -d $path/$week_x ]; then
log "Directory week direcotry exist: $path/$week_x"
else
mkdir $path/$week_x
log "Week directory created: $path/$week_x"
fi
if [ -d $dirname ]; then
rm -r $dirname
mkdir $dirname
log "Destination directory recreated: $dirname"
else
mkdir $dirname
log "Destination directory created: $dirname"
fi
}
#########
# START #
#########
#
log "Starting daily backup"
echo "`date "+%Y-%m-%d %H:%M:%S"` - Starting daily backup" > $zamail
#cheching local path
check_log
check_dirs
log "$dirname"
#
#### define standard commands
SCP="cp $remotepath"
#
#find all databases for backup
#clear remote directory
log "Deleting files from remote directory"
#command for clear all data from remote directory
find $remotepath -name \*.bz2 -type f -exec rm {} \;
log "Deleting OK"
#
echo "All data will be saved in: $dirname. Today is $DF_X day of week" >> $zamail
log "All data will be saved in: $dirname. Today is $DF_X day of week"
### Starting database dumps and copy to local host
for i in `mysql -u$dbuser_x -p$dbpassword_x -Bse 'show databases'`; do
log "Starting mysqldump $i"
`mysqldump -u$dbuser_x -p$dbpassword_x $i | bzip2 > $remotepath/mysql-$i-$datum_x.sql.bz2`
log "Dump OK. Starting SCP"
$SCP/mysql-$i-$datum_x.sql.bz2 $dirname/
echo "`date "+%Y-%m-%d %H:%M:%S"` - mysql: $i" >> $zamail
log "SCP done. $i database saved OK!!!!"
done
#
#prepare for directory dump
IFS=$'\n'
for i in `cat $lista`; do
XX=`echo $i | awk '{print $1}'`
YX=`echo $i | awk '{print $2}'`
unset IFS
if [ $DF_X == 1 ]; then
log "Deleting TAR increment files...."
BRISI="rm /backup/$XX.increment"
$BRISI
FileName="dirbackup_$XX-${datum_x}_Full.tar.bz2"
TAR="tar --exclude='mysqld.sock' -g /backup/$XX.increment -cjPf $remotepath/$FileName $YX"
else
FileName="dirbackup_$XX-${datum_x}_Increment_$DF_X.tar.bz2"
TAR="tar --exclude='mysqld.sock' -g /backup/$XX.increment -cjPf $remotepath/$FileName $YX"
fi
log "Starting to prepare files for $XX"
$TAR
log "Done, starting to copy...."
$SCP/$FileName $dirname/
log "Done. Directory copied."
echo "`date "+%Y-%m-%d %H:%M:%S"` - Directory: $FileName -> $dirname" >> $zamail
done
log "Done copy.."
echo "`date "+%Y-%m-%d %H:%M:%S"` - All Done." >> $zamail
log "send mail to $email:"
cat $zamail >> $logfile
mail -s"Daily ISPConfig3 backup `date +'%F'`" "$email" < $zamail log "All Done."

This entry was posted in Linux tips and tagged , , , . Bookmark the permalink.

5 Responses to Update: ISPConfig3 Backup script – local server

  1. nikola says:

    excellent script, thx!

  2. RFID Reader says:

    This is an interesting article. Thanks for sharing. RFID Reader

  3. Thank you for your great

    content. Your site is very useful for me .I bookmarked your site!

  4. jdr says:

    I’m a bit of a linux noob, but I think the ‘/backup/’ needs to be replaced by ‘$remotepath/’ ?
    Thanks for the script though, very useful!

  5. Dennis says:

    Hi, seems like the link back to the original remote script article is broken (missing “:” after http).

    Greetz!

Leave a Reply

Your email address will not be published. Required fields are marked *