Skip to content

Instantly share code, notes, and snippets.

@myugan
Last active June 6, 2020 11:24
Show Gist options
  • Save myugan/d8dc94d143f235ecba5ab6d49d841c8e to your computer and use it in GitHub Desktop.
Save myugan/d8dc94d143f235ecba5ab6d49d841c8e to your computer and use it in GitHub Desktop.
Bash script for databases daily backup
#!/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