Skip to content

Instantly share code, notes, and snippets.

@Tantas
Created February 25, 2016 22:18
Show Gist options
  • Save Tantas/1dd0895785a3efe15c18 to your computer and use it in GitHub Desktop.
Save Tantas/1dd0895785a3efe15c18 to your computer and use it in GitHub Desktop.
mysql_backup.sh
#!/bin/bash
DUMP_DIR="<dump_dir>"
MYSQLDUMP="/usr/bin/mysqldump"
DUMP_FILE_PREFIX="<database>_dump"
dump_date=`date +"%Y%m%d-%H"`
dump_file="${DUMP_FILE_PREFIX}_${dump_date}.sql"
if [ ! -d "$DUMP_DIR" ]; then
echo "error: Dump directory not found: $DUMP_DIR"
exit 1
fi
cd $DUMP_DIR
# Dump db
$MYSQLDUMP -u root -p'<password>' <database> | gzip -9c > $dump_file.gz
# Define older than in days
olderThan=30
# Delete files old files
find . -name "*.sql*" -mtime +$olderThan -type f -exec rm -rf {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment