A MySQL Backup Script
to backup MySQL databases to .gz files and to a remote server.
This MySQL backup script will backup all local database to compressed .gz files and to a remote MySQL server.
It will store all zipped files + a log file in the folder you specify in GZ_PATH. If the folder does not exist, it will be created - if permissions allow it.
Add this script to cron with, for example, Lingon X .
Copy it and modify it to your needs.
Tweet

It will store all zipped files + a log file in the folder you specify in GZ_PATH. If the folder does not exist, it will be created - if permissions allow it.
Add this script to cron with, for example, Lingon X .
Copy it and modify it to your needs.
Tweet

#!/bin/bash
MYSQL_USER="root"
MYSQL_PASS="gobbledegook"
MYSQL_REMOTE_USER="root"
MYSQL_REMOTE_PASS="gobbledegooktoo"
MYSQL_REMOTE_HOST="mbp2020.local"
GZ_PATH="/Users/marcvos/Documents/MySQL-backups"
LOG_FILE='dbbackup.log'
log_message() {
dd=$(date)
echo -e "${dd}\t$1" >> "${GZ_PATH}/${LOG_FILE}"
}
if [ ! -d "${GZ_PATH}" ]
then
mkdir "${GZ_PATH}"
log_message "Directory '${GZ_PATH}' created."
else
rm -f "${GZ_PATH}/${LOG_FILE}"
fi
log_message "Start the MySQL backup to .gz files and the remote server '${MYSQL_REMOTE_HOST}'"
# Test local MySQL connection
x=$(mysql --user=$MYSQL_USER --password=$MYSQL_PASS -e ";" 2>/dev/null)
x=$((x))
if [ $x -eq 0 ]
then
log_message "Connection to the local MySQL server succeeded."
dblist=$(mysql --user=$MYSQL_USER --password=$MYSQL_PASS -B --disable-column-names -e 'show databases' 2>/dev/null | tr -d ' ')
dbarray=(${dblist//"\n"/ })
for db in "${dbarray[@]}"
do
if [ "$db" != "information_schema" ] && [ "$db" != "mysql" ] && [ "$db" != "performance_schema" ] && [ "$db" != "sys" ] && [ "$db" != "test" ]
then
log_message "Export the database '${db}' into file '/tmp/${db}.sql'."
mysqldump --user=$MYSQL_USER --password=$MYSQL_PASS --databases "${db}" 2>/dev/null > "/tmp/${db}.sql"
# Test remote MySQL connection
x=$(mysql --host=$MYSQL_REMOTE_HOST --user=$MYSQL_REMOTE_USER --password=$MYSQL_REMOTE_PASS -e ";" 2>/dev/null)
x=$((x))
if [ $x -eq 0 ]
then
mysql --host=$MYSQL_REMOTE_HOST --user=$MYSQL_REMOTE_USER --password=$MYSQL_REMOTE_PASS "${db}" 2>/dev/null < "/tmp/${db}.sql"
log_message "The database '${db}' was successfully transferred to the remote MySQL server."
else
log_message "Connection to the remote MySQL server failed."
fi
if [ -f "/tmp/${db}.sql" ]
then
gzip "/tmp/${db}.sql"
mv "/tmp/${db}.sql.gz" "${GZ_PATH}/"
log_message "The compressed SQL-export file '${db}.sql.gz' was placed into '${GZ_PATH}'."
else
log_message "The 'mysqldump' command must have failed, the file '/tmp/${db}.sql' does not exist."
fi
fi
#break # @test
done
else
log_message "Connection to the local MySQL server failed."
fi



Transportation Administration System
Snoezelen Pillows for Dementia
Begeleiders voor gehandicapten
Laat uw hond het jaarlijkse vuurwerk overwinnen
Betuweroute en Kunst
Hey Vos! Je eigen naam@vos.net emailadres?
Kunst in huis? Nicole Karrèr maakt echt bijzonder mooie dingen
Kunst in huis? Netty Franssen maakt ook bijzonder mooie dingen