Skip to content

Instantly share code, notes, and snippets.

@nafiesl
Created April 25, 2021 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nafiesl/22c19e4ef466e1fcf6fdd8fc7c76f9e2 to your computer and use it in GitHub Desktop.
Save nafiesl/22c19e4ef466e1fcf6fdd8fc7c76f9e2 to your computer and use it in GitHub Desktop.
Backup MySQL DB in Single Files
#!/bin/bash
################################################
#
# Backup all MySQL databases in separate files and compress those.
# Furthermore the script will create a folder with the current time stamp
# @author: Per Lasse Baasch (http://skycube.net)
# @Version: 2014-06-13
# NOTE: MySQL and gzip installed on the system
# and you will need write permissions in the directory where you executing this script
#
################################################
# MySQL User
USER='root'
# MySQL Password
PASSWORD='root_password'
# Backup Directory - NO TAILING SLASH!
OUTPUT="."
##### And
TIMESTAMP=`date +%Y%m%d_%H%M%S`;
mkdir $OUTPUT/$TIMESTAMP;
cd $OUTPUT/$TIMESTAMP;
echo "Starting MySQL Backup";
echo `date`;
databases=`mysql --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
for db in $databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
echo "Dumping database: $db"
mysqldump --force --opt --user=$USER --password=$PASSWORD --databases $db > $OUTPUT/dbbackup-$TIMESTAMP-$db.sql
gzip $OUTPUT/dbbackup-$TIMESTAMP-$db.sql
fi
done
echo "Finished MySQL Backup";
echo `date`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment