k43D/mysqlbackup/backup-script.sh
2023-06-05 21:34:03 +02:00

17 lines
644 B
Bash

#!/bin/bash
# Set backup directory and file name format
BACKUP_DIR=/backup
DATE=$(date +"%Y-%m-%d-%H-%M-%S")
BACKUP_RETENTION_DAYS=7
# Get list of databases
DATABASES=$(mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -h $MYSQL_HOST -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql)")
# Loop through databases and dump them to separate files
for DB in $DATABASES; do
mysqldump --single-transaction --quick -u $MYSQL_USER -p$MYSQL_PASSWORD -h $MYSQL_HOST $DB | gzip >$BACKUP_DIR/$DB-$DATE.sql.gz
done
# Remove backups older than 7 days
find $BACKUP_DIR -name "*.gz" -type f -mtime +$BACKUP_RETENTION_DAYS -delete