Backup script for mysql

on November 05, 2010. in Development. A 1 minute read.

This post is more of a reminder for myself. Anywayz, a little bash script that backups a database, gzipit and deletes all backups older than 3 days.

#!/bin/bash

DBUSER="user"
DBPASS="pass"
DBDB="dbname"
NOW=$(date +"%Y-%m-%d-%H-%M-%S")
BACKUPROOTDIR="/tmp"
BACKUPSQL="$BACKUPROOTDIR/mysqlbackup-$NOW.sql"
BACKUPGZIP="$BACKUPSQL.gz"

mysqldump -u$DBUSER -p$DBPASS $DBDB > "$BACKUPSQL"
gzip -c $BACKUPSQL > $BACKUPGZIP
rm $BACKUPSQL
find $BACKUPROOTDIR -type f -name "mysqlbackup\*" -mtime +3 | xargs rm

Kudos to @zsteva for looking at it to spot any errors I might have made.

Tags: backup, mysql, script, shell.

Get the next article to your inbox

If you'd like, you can get the next article I write to your email inbox.