Bash script to backup all your MySQL databases

por | 3 noviembre, 2021

with process privileges:

#!/bin/bash
HOST="localhost"
USER="youruser"
PASSWORD="password"
OUTPUT="/home/user/backups"
rm "$OUTPUT/*gz" > /dev/null 2>&1
databases=`mysql --user=$USER --host=$HOST --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
for db in $databases; do
    if [[ "$db" != "information_schema" ]] && [[ "$db" != _* ]] ; then
        echo "Dumping database: $db"
        mysqldump --force --opt --routines --user=$USER --password=$PASSWORD --databases $db > $OUTPUT/`date +%Y%m%d`.$db.sql
        gzip $OUTPUT/`date +%Y%m%d`.$db.sql
    fi
done

without process privileges (process privilege error) –no-tablespaces



#!/bin/bash
HOST="localhost"
USER="youruser"
PASSWORD="password"
OUTPUT="/home/user/backups"
rm "$OUTPUT/*gz" > /dev/null 2>&1
databases=`mysql --user=$USER --host=$HOST --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
for db in $databases; do
    if [[ "$db" != "information_schema" ]] && [[ "$db" != _* ]] ; then
        echo "Dumping database: $db"
        mysqldump --no-tablespaces --force --opt --routines --user=$USER --host=$HOST --password=$PASSWORD --databases $db > $OUTPUT/`date +%Y%m%d`.$db.sql
        gzip $OUTPUT/`date +%Y%m%d`.$db.sql
    fi
done