Posts tagged 'shell'

Backup script for mysql

published on November 05, 2010.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

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.
Categories: Development.
Robert Basic

Robert Basic

Software engineer, consultant, open source contributor.

Let's work together!

If you require outsourcing or consulting help on your projects, I'm available!

Robert Basic © 2008 — 2019
Get the feed