Shell Script Back up Home and MySQL
========================= backup-home.sh ============================#!/bin/bash
# Zip all home user in /home
# By: Vannak
TAR=$(which tar)
MAIL_TO=ken.vannakk@gmail.com
ERROR=/var/log/backup/home_error.log
ERROR1=/var/log/backup/home_error1.log
LOG=/var/log/backup/backup_home.log
DATE=`date '+%Y-%m-%d'`
error=0
# Delete Database backup older then 7days
ARCHIVE_STORE=/opt/backup/home/
find $ARCHIVE_STORE -name "*.gz" -mtime +10 -exec rm -f {} \;
# List all users in /home
ls /home/ > list-user.txt
# Exclude folder or user, that we don't want to backup
sed -i '/lost+found/d' list-user.txt
# List All Users
for alluser in $(ls /home/)
do
# Check if all the users
if (grep -Fxq $alluser list-user.txt)
then
FILE="/home/$alluser/"
FILE_STORE="/opt/backup/home/$alluser.$DATE.tar.gz"
# Zip HOME
echo " Finished --------- $(date) ---------- " >> $LOG
$TAR -czPf $FILE_STORE --exclude logs $FILE 2> $ERROR
# Delete Archive Older then 7 days:
find $FILE_STORE -name "*.tar.gz" -mtime +7 -exec rm -f {} \;
echo "SERVER IP: 12.23.34.45 " > $ERROR1
cat $ERROR >> $ERROR1
if test -s $ERROR;
then
error=1
fi
sleep 2
if [ "$error" = 1 ];
then
# Create email
echo "From: No Reply <noreply@gmail.com>" >> /var/tmp/mail.txt
echo "Subject: ERROR HOME BACKUPS SYSTEMS" >> /var/tmp/mail.txt
echo " " >> /var/tmp/mail.txt
echo "IF WE HAVE ERROR CHECK 12.23.34.45 in /opt/backups/home/error/backup-home.log" >> /var/tmp/mail.txt
echo "##############################################" >> /var/tmp/mail.txt
echo "Kind of error: " >> /var/tmp/mail.txt
echo "-------------------------------------------------------------------------------" >> /var/tmp/mail.txt
`/bin/cat $ERROR1 >> /var/tmp/mail.txt`
echo "###########################################" >> /var/tmp/mail.txt
echo "CHECK THE SERVER IP_SERVER" >> /var/tmp/mail.txt
mail $MAIL_TO < /var/tmp/mail.txt
echo "ERROR!!!!" >> $LOG
cat $ERROR >> $LOG
# Remove Error Log
rm /var/tmp/mail.txt
rm $ERROR
rm $ERROR1
fi
# Change ownership
chown $alluser:$alluser $FILE_STORE
fi
done
echo " Finished --------- $(date) ---------- " >> $LOG
# Delete List users
rm list-user.txt
========================== backup-mysql.sh ==========================
#!/bin/bash
#
# Backup all mysql active
#
MYUSER="root"
MYPASS="MySQLR00tPassw0rd"
MYHOST="database-server-name"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
ERROR=/var/log/backup/mysql_error.log
ERROR1=/var/log/backup/mysql_error1.log
LOG=/var/log/backup/backup_mysql.log
TIMESTAMP=`date +%y-%m-%d-%H%M`
DATE=`date '+%Y-%m-%d'`
error=0
# Delete Database backup older then 7days
ARCHIVE_STORE=/opt/backup/mysql/
find $ARCHIVE_STORE -name "*.gz" -mtime +7 -exec rm -f {} \;
# Get all databases start with "db"
DBS="$($MYSQL -u $MYUSER -h $MYHOST -p$MYPASS -Bse 'show databases' 2> $ERROR )"
$MYSQL -u $MYUSER -h $MYHOST -p$MYPASS -Bse 'show databases' 2> $ERROR > all-db.tmp
# Remove default schema: "information_schema" and "mysql".
sed -i '/information_schema/d' all-db.tmp
sed -i '/mysql/d' all-db.tmp
echo "SERVER 12.23.34.45: " > $ERROR1
cat $ERROR >> $ERROR1
if test -s $ERROR;
then
error=1
fi
sleep 2
if [ "$error" = 1 ];
then
# Create email
echo "From: <ken.vannakk@gmail.com>" >> /var/tmp/mail.txt
echo "Subject: ERROR Databases BACKUPS SYSTEMS" >> /var/tmp/mail.txt
echo " " >> /var/tmp/mail.txt
echo "IF WE HAVE ERROR CHECK 12.23.34.45 in /var/log/backup/backup_mysql.log" >> /var/tmp/mail.txt
echo "##############################################" >> /var/tmp/mail.txt
echo "Kind of error: " >> /var/tmp/mail.txt
echo "----------------------------------------------------------------------------" >> /var/tmp/mail.txt
`/bin/cat $ERROR1 >> /var/tmp/mail.txt`
echo "###############################################" >> /var/tmp/mail.txt
echo "CHECK THE SERVER 12.23.34.45" >> /var/tmp/mail.txt
mail ken.vannakk@gmail.com < /var/tmp/mail.txt
echo "ERROR!!!!" >> $LOG
cat $ERROR >> $LOG
# Delete Log
rm /var/tmp/mail.txt
rm $ERROR
rm $ERROR1
fi
# Check all databases and perform backup
ERROR=/var/log/backup/mysql_error.log
ERROR1=/var/log/backup/mysql_error1.log
LOG=/var/log/backup/backup_mysql.log
DATE=`date '+%Y-%m-%d'`
error=0
for db in $DBS
do
# Checking if the database is active
if (grep -Fxq $db all-db.tmp)
then
FILE="/opt/backup/mysql/$db.$TIMESTAMP.sql.gz" 2> $ERROR
# Perform backup each database
$MYSQLDUMP -u $MYUSER -h $MYHOST -p$MYPASS $db | $GZIP -9 > $FILE 2> $ERROR
echo "SERVER 12.23.34.45: " > $ERROR1
cat $ERROR >> $ERROR1
if test -s $ERROR;
then
error=1
fi
sleep 2
if [ "$error" = 1 ];
then
# Create email
echo "From: <ken.vannakk@gmail.com>" >> /var/tmp/mail.txt
echo "Subject: ERROR Databases BACKUPS SYSTEMS" >> /var/tmp/mail.txt
echo " " >> /var/tmp/mail.txt
echo "IF WE HAVE ERROR CHECK 12.23.34.45 in /var/log/backup/backup_mysql.log" >> /var/tmp/mail.txt
echo "##############################################" >> /var/tmp/mail.txt
echo "Kind of error: " >> /var/tmp/mail.txt
echo "--------------------------------------------------------------------------" >> /var/tmp/mail.txt
`/bin/cat $ERROR1 >> /var/tmp/mail.txt`
echo "##############################################" >> /var/tmp/mail.txt
echo "CHECK THE SERVER 12.23.34.45" >> /var/tmp/mail.txt
mail ken.vannakk@gmail.com < /var/tmp/mail.txt
echo "ERROR!!!!" >> $LOG
cat $ERROR >> $LOG
#Borramos el fichero
rm /var/tmp/mail.txt
rm $ERROR
rm $ERROR1
fi
echo "FINISH BACKUPS:" $DATE >> $LOG
fi
done
rm all-db.tmp
No comments:
Post a Comment