Skip to content

Instantly share code, notes, and snippets.

@Haeniken
Last active April 1, 2023 12:21
Show Gist options
  • Save Haeniken/8b29723122302f2f05fd810448b3a6a8 to your computer and use it in GitHub Desktop.
Save Haeniken/8b29723122302f2f05fd810448b3a6a8 to your computer and use it in GitHub Desktop.
Bash Script to backup all MySQL databases with LZMA (long, very compressed)
#!/bin/bash
#==============================================================================
#TITLE: mysql_backup.sh
#DESCRIPTION: script for automating the daily mysql backups
#AUTHOR: haeniken
#DATE: 2023-03-24
#VERSION: 1.0
#USAGE: ./mysql_backup.sh
#CRON:
# example cron for daily db backup @ 4:10 am
# min hr mday month wday command
# 10 4 * * * /home/[your user name]/scripts/mysql_backup.sh
#RESTORE FROM BACKUP
#$ lzma < [backupfile.sql.lzma] | mysql -u [uname] -p[pass] [dbname]
#==============================================================================
# CUSTOM SETTINGS
#==============================================================================
### SETUP MYSQL LOGIN ###
MUSER='user'
MPASS='*****'
MHOST="127.0.0.1"
### Set bins path ###
LZMA=/usr/bin/lzma
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump
RM=/bin/rm
MKDIR=/bin/mkdir
MYSQLADMIN=/usr/bin/mysqladmin
GREP=/bin/grep
FIND=/usr/bin/find
### Setup dump directory ###
BAKRSNROOT=/home/trinity/mysql_bak
### LZMA compression level ###
COMP=9
### Number of days to keep backups
KEEP_BACKUPS_FOR=30 #days
### Set to 1 if you need to see progress while dumping dbs ###
VERBOSE=0
#==============================================================================
# METHODS
#==============================================================================
### Default time format ###
TIME_FORMAT='%Y.%m.%d_%H:%M:%S%P'
### Make a backup ###
backup_mysql_rsnapshot(){
local DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
local db="";
[ ! -d $BAKRSNROOT ] && ${MKDIR} -p $BAKRSNROOT
[ $VERBOSE -eq 1 ] && echo "*** Deleting $BAKRSNROOT/*.sql.lzma older than $KEEP_BACKUPS_FOR days ***"
$FIND $BAKRSNROOT -type f -name "*.sql.lzma" -mtime +$KEEP_BACKUPS_FOR -exec $RM {} >/dev/null 2>&1 \;
[ $VERBOSE -eq 1 ] && echo "*** Dumping MySQL Database ***"
[ $VERBOSE -eq 1 ] && echo -n "Database> "
for db in $DBS
do
if [ $db = "information_schema" ]
then :
elif [ $db != "performance_schema" ]
then
local tTime=$(date +"${TIME_FORMAT}")
local FILE="${BAKRSNROOT}/${db}-${tTime}.sql.lzma"
[ $VERBOSE -eq 1 ] && echo -n "$db.."
${MYSQLDUMP} -u ${MUSER} -h ${MHOST} -p${MPASS} $db --quick --single-transaction --max_allowed_packet=1G --compress --order-by-primary --opt --set-charset --extended-insert | ${LZMA} -$COMP > $FILE
fi
done
[ $VERBOSE -eq 1 ] && echo ""
[ $VERBOSE -eq 1 ] && echo "*** Backup done. Files wrote to [$BAKRSNROOT] ***"
}
### Die on demand with message ###
die(){
echo "$@"
exit 999
}
# Make sure bins exists, else die
verify_bins(){
[ ! -x $GZIP ] && die "File $GZIP does not exists. Make sure correct path is set in $0."
[ ! -x $MYSQL ] && die "File $MYSQL does not exists. Make sure correct path is set in $0."
[ ! -x $MYSQLDUMP ] && die "File $MYSQLDUMP does not exists. Make sure correct path is set in $0."
[ ! -x $RM ] && die "File $RM does not exists. Make sure correct path is set in $0."
[ ! -x $MKDIR ] && die "File $MKDIR does not exists. Make sure correct path is set in $0."
[ ! -x $MYSQLADMIN ] && die "File $MYSQLADMIN does not exists. Make sure correct path is set in $0."
[ ! -x $GREP ] && die "File $GREP does not exists. Make sure correct path is set in $0."
[ ! -x $FIND ] && die "File $FIND does not exists. Make sure correct path is set in $0."
}
# Make sure we can connect to server, else die
verify_mysql_connection(){
$MYSQLADMIN -u $MUSER -h $MHOST -p$MPASS ping | $GREP 'alive'>/dev/null
[ $? -eq 0 ] || die "Error: Cannot connect to MySQL Server. Make sure username and password are set correctly in $0"
}
#==============================================================================
# MAIN
#==============================================================================
verify_bins
verify_mysql_connection
backup_mysql_rsnapshot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment