Software development, photography, jokes, ....

noyb-logo

Sites by me

 
sds82-logo
tas-logoTransportation Administration System
snoezelkussen-logo-kleinstSnoezelen Pillows for Dementia
ikzoekeenbegeleider-logoBegeleiders voor gehandicapten
Laat uw hond het jaarlijkse vuurwerk overwinnen
Betuweroute en Kunst
logo 50x50Hey Vos! Je eigen naam@vos.net emailadres?
Kunst in huis? Nicole Karrèr maakt echt bijzonder mooie dingen
nettylogo2Kunst in huis? Netty Franssen maakt ook bijzonder mooie dingen

Hosting Favorites

 
ANU Internet Services
Netim
GoDaddy
XEL Media

Blogroll

 
Bomenstichting
Google Translate
PHP
MySQL
jQuery
jQuery UI
YourHead Stacks API
Favicon Generator.
Password Generator.
Check HTTPS problems


Marc's Place


 
Stacks Image 4

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.



buy me something-2
#!/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
© 1997- Marc Vos (and others)   -   Privacy Statement   -    Contact Me

On this website, Google Analytics is used to track visitor statistics. These are anonymised data about the number of visitors, which pages they visit on this site, from which regions they visit, which web browsers they use, etc.. You will also see non-personalised ads via Google AdSense. Cookies from Paddle or Paypal are placed when you click on a 'Buy now!' or 'Donate!' button, and possible cookies from Disqus when you use that system to comment on one or more blogposts.
Privacy Statement