Last active
June 26, 2022 10:05
-
-
Save MateusBMP/dad273584374c5520ae5eb44308c7c35 to your computer and use it in GitHub Desktop.
Backup all database, clearing old data
This file contains hidden or 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 | |
| #---------------------------------------------- | |
| # OPTIONS | |
| #---------------------------------------------- | |
| USER='user' # MySQL User | |
| PASSWORD='pass' # MySQL Password | |
| DAYS_TO_KEEP=7 # 0 to keep forever | |
| GZIP=1 # 1 = Compress | |
| BACKUP_PATH='/set/backup/path' | |
| #---------------------------------------------- | |
| # Create the backup folder | |
| if [ ! -d $BACKUP_PATH ]; then | |
| mkdir -p $BACKUP_PATH | |
| fi | |
| # Get list of database names | |
| databases=`mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "|" | grep -v Database` | |
| for db in $databases; do | |
| if [ $db == 'information_schema' ] || [ $db == 'performance_schema' ] || [ $db == 'mysql' ] || [ $db == 'sys' ]; then | |
| echo "Gerando backup dos bancos: $db" | |
| continue | |
| fi | |
| date=$(date -I) | |
| if [ "$GZIP" -eq 0 ] ; then | |
| echo "Iniciando backup de: $db sem comprimir" | |
| mysqldump -u $USER -p$PASSWORD --databases $db > $BACKUP_PATH/$date-$db.sql | |
| else | |
| echo "Iniciando backup de: $db comprimido" | |
| mysqldump -u $USER -p$PASSWORD --databases $db | gzip -c > $BACKUP_PATH/$date-$db.gz | |
| fi | |
| done | |
| # Delete old backups | |
| if [ "$DAYS_TO_KEEP" -gt 0 ] ; then | |
| echo "Excluindo backups anteriores a $DAYS_TO_KEEP dias" | |
| find $BACKUP_PATH/* -mtime +$DAYS_TO_KEEP -exec rm {} \; | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment