Added MySQL host, username and password and changed default retention to 1 day

This commit is contained in:
Yavuz Aydın 2020-02-27 15:02:49 +01:00
parent 2cf4a740cd
commit e6f4a521f0
2 changed files with 54 additions and 12 deletions

View File

@ -6,6 +6,22 @@ Allows you to create and restore local MySQL / MariaDB backups.
# Requirements #
Method 1:
Enter username and password in configuration section and uncomment.
Example:
```
...
# MySQL administrative user, uncomment if you want to use this instead of
# the value in /root/.my.cnf
MYSQL_USER='root'
# MySQL administrative user password, uncomment if you want to use this instead of
# the value in /root/.my.cnf
MYSQL_PW='ThisIsMyMySQLRootPassword'
...
```
Method 2:
Requires a /root/.my.cnf file which contains credentials so the user root can login to the MySQL / MariaDB server with sufficient privileges
without entering a password. Example /root/.my.cnf contents:
@ -22,8 +38,8 @@ sudo chmod 600 /root/.my.cnf
Test if it works with:
```
PING=$(sudo mysqladmin ping 2>/dev/null)
if [ "$PING" != "mysqld is alive" ]; then
sudo mysqladmin status >> /dev/null 2>&1
if [ $? -ne 0 ]; then
clear && echo 'Error: Unable to connect to MySQL Server!'
else
clear && echo 'Successfully connected to the MySQL server!'

46
backup_mysql.sh Normal file → Executable file
View File

@ -29,6 +29,7 @@
# #
##############################################################################################
# #
# 2020/02/27 1.7 Added username and password and set default retention to 1 day #
# 2019/01/28 1.6 Fixed backups of databases with dash (-) in name #
# 2019/01/21 1.5 Fixed creation of databases with dot (.) in name #
# 2018/02/11 1.4 Fixed detection of mydumper #
@ -64,14 +65,20 @@
# #
##############################################################################################
# Get list of Databases except the pid file
DBS_LIST=$(mysql -s -N -e "show databases;")
# MySQL host, uncomment if you want to use this instead of the default localhost
#MYSQL_HOST='localhost'
# MySQL administrative user, uncomment if you want to use this instead of
# the value in /root/.my.cnf
#MYSQL_USER='root'
# MySQL administrative user password, uncomment if you want to use this instead of
# the value in /root/.my.cnf
#MYSQL_PW='ThisIsMyMySQLRootPassword'
# Log file
BAKUP_LOG=/var/backup/log/mysql-backup.log
# Backup Base directory
BASE_BAK_FLDR=/var/backup/mysql
# Backup rotation period.
RM_FLDR_DAYS="+30"
RM_FLDR_DAYS="+1"
##############################################################################################
# #
@ -79,6 +86,7 @@ RM_FLDR_DAYS="+30"
# #
##############################################################################################
# Check whether mydumper is installed and whether we need to use it
USE_MYDUMPER=0
which mydumper &> /dev/null
if [ "$?" == "0" ]; then
@ -86,22 +94,40 @@ if [ "$?" == "0" ]; then
USE_MYDUMPER=1
fi
# Check if MYSQL_USER or MYSQL_PW is set and set options accordingly
MYSQL_OPTS=""
if [[ -n ${MYSQL_HOST} && "${MYSQL_HOST}" != "" ]]; then
MYSQL_OPTS="${MYSQL_OPTS} -u ${MYSQL_HOST}"
fi
if [[ -n ${MYSQL_USER} && "${MYSQL_USER}" != "" ]]; then
MYSQL_OPTS="${MYSQL_OPTS} -u ${MYSQL_USER}"
fi
if [[ -n ${MYSQL_PW} && "${MYSQL_PW}" != "" ]]; then
MYSQL_OPTS="${MYSQL_OPTS} -p${MYSQL_PW}"
fi
# Get list of Databases except the pid file
DBS_LIST=$(mysql ${MYSQL_OPTS} -s -N -e "show databases;")
index=0
# Check if we can connect to the mysql server; otherwise die
if [ ! "$(id -u -n)" = "root" ]; then
echo -e "Error: $0 : Only user 'root' can run this script"
exit 100
fi
PING=$(mysqladmin ping 2>/dev/null)
if [ "$PING" != "mysqld is alive" ]; then
echo "Error: Unable to connected to MySQL Server, exiting!"
mysqladmin ${MYSQL_OPTS} status >> /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: Unable to connect to MySQL Server, exiting!"
exit 101
fi
# Backup process starts here.
START_DATETIME=$(date +%Y%m%d-%H%M)
# Flush logs prior to the backup.
mysql -e "FLUSH LOGS"
mysql ${MYSQL_OPTS} -e "FLUSH LOGS"
if [ $USE_MYDUMPER -eq 1 ]; then
# Use mydumper
@ -117,10 +143,10 @@ else
[ ! -d ${DB_BKP_FLDR} ] && mkdir -p ${DB_BKP_FLDR} && chmod 700 ${DB_BKP_FLDR}
# Get the schema of database with the stored procedures.
# This will be the first file in the database backup folder
mysqldump -R -d --single-transaction ${DB} | gzip -c > ${DB_BKP_FLDR}/000-DB_SCHEMA.sql.gz
mysqldump ${MYSQL_OPTS} -R -d --single-transaction ${DB} | gzip -c > ${DB_BKP_FLDR}/000-DB_SCHEMA.sql.gz
index=0
#Get the tables and its type. Store it in an array.
table_types=($(mysql -e "show table status from \`${DB}\`" | awk '{ if ($2 == "MyISAM" || $2 == "InnoDB") print $1,$2}'))
table_types=($(mysql ${MYSQL_OPTS} -e "show table status from \`${DB}\`" | awk '{ if ($2 == "MyISAM" || $2 == "InnoDB") print $1,$2}'))
table_type_count=${#table_types[@]}
# Loop through the tables and apply the mysqldump option according to the table type
# The table specific SQL files will not contain any create info for the table schema.
@ -135,7 +161,7 @@ else
else
DUMP_OPT="${DB} --no-create-info --single-transaction --tables --events --quote-names "
fi
mysqldump ${DUMP_OPT} ${table} |gzip -c > ${DB_BKP_FLDR}/${table}.sql.gz
mysqldump ${MYSQL_OPTS} ${DUMP_OPT} ${table} |gzip -c > ${DB_BKP_FLDR}/${table}.sql.gz
index=$((${index} + 2))
#echo -e " - Total time : $(($(date +%s) - ${START}))\n"
done