Scripts Backup Databases MySQL

 

$ sudo vi /root/.my.cnf
[client]
host=127.0.0.1
user=root
password='yourRootMySQLpassword'
$ sudo vi /etc/scripts/backup_db.sh

#!/bin/sh
####### SCRIPTS BACKUP ######
# create : AGINK
#############################

tgl=`date "+%Y%m%d-%H%M%S"`
home_path=/home/agink
backup_path=$home_path/backup/db
sqlpass='yourRootMySQLpassword'

echo "#--- Backup processing...!!!"
echo ""
echo "Database dump..."
echo ""
DATABASELIST="$(echo "show databases;" | mysql | grep -vwE "(DATABASE|Database|information_schema|mysql|performance_schema)")"

for database in $DATABASELIST;
do
        mysqldump -u root -p$sqlpass $database | gzip > $backup_path/$database-backup-$tgl.sql.gz;
done

echo""
chown agink:users $backup_path/* 

#echo""
echo "remove old backup files"
find $backup_path/ -name "*.sql.gz" -type f -mtime +3 -exec rm -f {} \;
echo ""

echo "#--- DB Backup Done...!!!"
$ sudo chmod +x /etc/scripts/backup_db.sh