Last active
June 6, 2020 11:24
-
-
Save myugan/d8dc94d143f235ecba5ab6d49d841c8e to your computer and use it in GitHub Desktop.
Bash script for databases daily backup
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 | |
# CRED | |
DB_USER="user" | |
DB_PASS="user" | |
DB_NAME="dbname" | |
# PATH/DIR | |
SRCDIR="/path/to/backups" | |
EXPDIR="$SRCDIR/$DB_NAME_$(date +"%H%M-%d-%m-%Y").sql" | |
# Dump db to local | |
mysqldump -u$DB_USER -p$DB_PASS $DB_NAME > $EXPDIR | |
# Compress db | |
tar -zcvf $EXPDIR.tar.gz $EXPDIR | |
# Cleanup db after 30 days | |
find $SRCDIR -maxdepth 1 -type f -iname '*.sql' -mtime +30 -print0 | xargs -0 rm -f | |
find $SRCDIR -maxdepth 1 -type f -iname '*.tar.gz' -mtime +30 -print0 | xargs -0 rm -f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment