Skip to content

Instantly share code, notes, and snippets.

@sirtawast
Last active June 13, 2016 11:14
Show Gist options
  • Save sirtawast/89243c71c54e52b21921ca9aca342234 to your computer and use it in GitHub Desktop.
Save sirtawast/89243c71c54e52b21921ca9aca342234 to your computer and use it in GitHub Desktop.
Backup PostgreSQL or MySQL
# Back up SQL
# ===========
# dump_db.sh (chmod 0700)
# Format in any preferable way, see:
# http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/
# %a is weekday Mon-Sun, %H is hour 00-24
timestamp() {
date +"%a_%H"
}
# Password is for testing purposes and is generally not safe to use
mysqldump -u user_name --password=xxxx db_name > /path/to/backups/db_name-$(timestamp).sql
# OR
psql_dump -U user_name --password=xxxx db_name > /path/to/backups/db_name-$(timestamp).sql
# =================================================
# Put the script in crontab and run every six hours
# $ crontab -e
# 0 */6 * * * /bin/sh /path/to/backups/dump_db.sh
# $ sudo service cron restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment