Created
November 22, 2016 00:36
-
-
Save mtuchi/f91e320a89809480aa44e061afd359fa to your computer and use it in GitHub Desktop.
Backup MySQL DBs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Add your backup dir location, password, mysql location and mysqldump location | |
DATE=$(date +%d-%m-%Y) | |
BACKUP_DIR="$HOME/backup/mysql" | |
MYSQL_USER="root" | |
MYSQL_PASSWORD="***" | |
MYSQL=$(which mysql) | |
MYSQLDUMP=$(which mysqldump) | |
# To create a new directory into backup directory location | |
mkdir -p $BACKUP_DIR/$DATE | |
# get a list of databases | |
databases=`$MYSQL -u$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema)"` | |
# dump each database in separate name | |
for db in $databases; do | |
echo $db | |
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/$DATE/$db.sql.gz" | |
done | |
# Delete files older than 10 days | |
find $BACKUP_DIR/* -mtime +10 -exec rm {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment