Commit 9a32a414 authored by Marco Giuntini's avatar Marco Giuntini

first commit

parents
# MySQL-Databases-Backup
MySQL-Databases-Backup is a shell script to backup MySql databases into directories.
This script backup all databases in MySQL in separated folders and it auto-rotates these backup every 7 days.
## Configuration
In mysql_dbs_backup.conf set the following parameters:
* The database credential with root privilege
* The backup destination folder
[..]
#Database parameters
MyUSER="myusername" # USERNAME with root privilege
MyPASS="mypassword" # PASSWORD
MyHOST="localhost" # Hostname
[..]
# Backup Dest directory, change this if you have some other location
DEST="/root/backups"
[..]
# Ignore everything in this directory
*
# Except this file
!.gitignore
#!/bin/bash
#
# Config file for shell script to backup MySql databases into directories.
# -------------------------------------------------------------------------
# File Name: mysql_dbs_backup.conf
# Version: 1.0
# Last updated: Nov 2014
# -------------------------------------------------------------------------
# Released under the GPLv3 Licence
#
# @AUTHORS: Hispanico (hispanico@ninux.org)
# -------------------------------------------------------------------------
#Database parameters
MyUSER="myusername" # USERNAME with root privilege
MyPASS="mypassword" # PASSWORD
MyHOST="localhost" # Hostname
# Linux bin paths, change this if it can't be autodetected via which command
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
# Backup Dest directory, change this if you have someother location
DEST="/root/backups"
# Main directory where backup will be stored
MBD="$DEST/mysql"
# nome di base del file log
base_log_filename="mysql_dbs_backup_"
# Get hostname
HOST="$(hostname)"
# Get data in day of week (0..6); 0 is Sunday. Autorotate every 7 days
DAY="$(date +"%w")"
# File to store current backup file
FILE=""
# Store list of databases
DBS=""
# DO NOT BACKUP these databases
IGGY=(information_schema mysql performance_schema phpmyadmin)
#!/bin/bash
#
# Shell script to backup MySql databases into directories.
# -------------------------------------------------------------------------
# File Name: mysql_dbs_backup.sh
# Version: 1.0
# Last updated: Nov 2014
# -------------------------------------------------------------------------
# Released under the GPLv3 Licence
#
# @AUTHORS: Hispanico (hispanico@ninux.org)
# -------------------------------------------------------------------------
# load config file
. ${0%.*}.conf
SCRIPTDIR=$(dirname $0)
# Log directory
LOGDIR="$SCRIPTDIR/logs"
# Log file
log_file=${LOGDIR}/${base_log_filename}$DAY.log
# Redirect stdout and stderr to log file
[ ! -d ${LOGDIR} ] && mkdir -p ${LOGDIR} || :
exec 2>>${log_file}
exec 1>>${log_file}
# Begin Script
# =========================================================================
[ ! -d $MBD ] && mkdir -p $MBD || :
# Only root can access it!
$CHOWN 0.0 -R $DEST
$CHMOD 0600 $DEST
# Get all database list first
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"
for db in $DBS
do
skipdb=-1
if [ "${IGGY[*]}" != "" ];
then
for i in ${IGGY[@]}
do
[ "$db" == "$i" ] && skipdb=1 || :
done
fi
if [ "$skipdb" == "-1" ] ; then
DBD="$MBD/$db"
[ ! -d $DBD ] && mkdir -p $DBD || :
FILE="$MBD/$db/$db.$HOST.$DAY.gz"
# do all inone job in pipe,
# connect to mysql using mysqldump for select mysql database
# and pipe it out to gz file in backup dir :)
$MYSQLDUMP --single-transaction -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE
echo "Database $db backuped in $FILE"
fi
done
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment