Created
July 10, 2015 14:57
-
-
Save madalinignisca/6095a45673673cb18af6 to your computer and use it in GitHub Desktop.
Backup the database from bash, zip it and delete any older then X days. Good to use with Cron when hosting websites.
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 | |
# replace what is in [[ ]] with necessary variables | |
# Database credentials | |
user="[[username]]" | |
password="[[password]]" | |
host="[[host]]" | |
db_name="[[database_name]]" | |
# Other options | |
backup_path="[[path_to_backup_folder]]" | |
date=$(date +"%d-%b-%Y") | |
# Set default file permissions | |
umask 177 | |
# Dump database into SQL file | |
mysqldump --user=$user --password=$password --host=$host $db_name > $backup_path/$db_name-$date.sql | |
zip -9 $backup_path/$db_name-$date.sql.zip $backup_path/$db_name-$date.sql | |
rm $backup_path/$db_name-$date.sql | |
# Delete files older than X days | |
find $backup_path/* -mtime +[[X]] -exec rm {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment