Skip to content

Instantly share code, notes, and snippets.

@bencates
Created November 4, 2010 14:52
Show Gist options
  • Save bencates/662567 to your computer and use it in GitHub Desktop.
Save bencates/662567 to your computer and use it in GitHub Desktop.
Cron script to back up a database daily
#!/bin/bash
# Database connection info
user=ssh_username
password=ssh_password
host=ssh_host
db=ssh_database
# Filename settings
path=~/dbbackups/
name=dbbackup`date +%y%m%d`.sql.bz2
link=dbbackup.sql.bz2
# Dump and bzip the databases
mysqldump --add-drop-table \
--user=$user --password=$password --host=$host \
--databases $db \
| bzip2 > $path$name
# Symlink the newest backup to a static filename
ln -sf $path$name $path$link
# Delete backups older than a week
find $path -name "*.sql.bz2" -mtime +7 -type f -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment