Work:Backup All Databases

From Zoelife4U Wiki
Jump to: navigation, search

These Are A Couple Bash Scripts to cycle through all MySQL Databases and back each on up. It also checks the backup directory and deletes backups older than 30 days. Install to $HOME/bin as backup_db.sh and chmod 755 $HOME/bin/backup_db.sh, then run in a cronjob once a day.

Be sure to change the user and pass variables in the script as directed

Combined UNIX Version

Tested on FreeBSD, CentOS and Ubuntu

#!/bin/bash
#===============================================================================
#
#          FILE:  backup_db.sh
# 
#         USAGE:  ./backup_db.sh password
# 
#   DESCRIPTION:  Backup all my databases one by one :)
# 
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:   (), 
#       COMPANY:  
#       VERSION:  1.3-STABLE
#       CREATED:  11/28/08 11:42:00 MST
#      REVISION:  Added Optimize and Repair...
#===============================================================================
#-------Configuration Settings-------#
user="cPaneluser"; #A Database User with Privlidges on all dbs
pass="cPanelpassword"; #Said user's password !! :)
#-----Don't Edit Below This Line-----#
if [[ `uname` = "Linux" ]];
then
    expath="/usr/bin";
    mystat="stat -c %Y";
fi;
if [[ `uname` = "FreeBSD" ]];
then
    expath="/usr/local/bin";
    mystat="stat -f %B";
fi;
ts=`date +%s`; #For the file timestamps
d=`date +%Y%m%d-%H%M`; # For other uses
month=2592000; #One month
#month=178200; #2 days For testing
dir="$HOME/backup/mysql/"; # Directory to store the backups
if [[ ! -d $dir ]]; #Make sure the dir exists and create if not.
then
    mkdir -p $dir;
fi;
# First let's prune the backup dir of files over a month old
cd $dir;
for l in `ls $dir`;
    # Works CentOS and Ubuntu...
    do mt=`$mystat $l`;result=$(($ts-$mt));
    #echo $result;
    if  (("$result" >= "$month")); then
        echo "Deleting: $l";rm -f $l;  
    fi;     
done;
 
#Let's Optimize and Repair The DBs first
echo "Running Optimize and Repair";
$expath/mysqlcheck -auto-repair -u$user -p$pass -A -o
# Let's do this
echo "Creating Backups of each DataBase into $dir";
 
for i in `echo "show databases" | $expath/mysql -u$user -p$pass | grep -v Database`;
    do var=${i}${d};$expath/mysqldump -R -p$pass -u$user $i > $var.sql && echo $i backed up on $d; 
done;
Personal tools
Online Users
Zoelife4U: