Simple MySQL Backup Script

Below is a simple database backup script to use with MySQL.

#!/usr/bin/perl -w

use strict;

# Backup – Script used to backup MySQL databases to an NFS share.
# VERSION: 1.2
# DATE: 12152005
# AUTHOR: packetmad

# This is a Freebsd 4.x-centric script. It may run on other
# versions of Freebsd, or any other BSD for that matter, but I
# wrote it on a Freebsd 4.7 system. It’s not even guaranteed
# to run on that.

# Speaking of guarantees, warranties, etc., there ain’t one, so
# don’t even try. :P
# I am not responsible for the output of this script, nor am I
# responsible for any damage or data loss as a result of using
# this script. In short, I am not responsible for anything
# regarding this script.
# Furthermore, I am not responsible if this script causes
# Your dog to die, your wife to leave you, or
# your truck to break down.
# I am releasing this script under the conventions of the BSD
# license. You can use it, modify it, sleep with it, or
# whatever. If you do find this script useful or have
# suggestions on a better way to do some things contained here
# in, I welcome all correspondence.
# The latest version of this script can be obtained at:
# http://tech.kulish.com

# USE THIS SCRIPT AT YOUR OWN RISK!!!!!!!!!!!!!!!!!!

# Change Log
# v1.1
# Date: 11302002
# Initial Release, absolutely BUG FREE! :)
# v1.2
# Date: 12152005
# Added compression functionality
# Added naming of backup files by date
# Changed longterm storeage directory for 1back to store1

###
# Declare variables and arrays.
###

my (
$bdir, $sysname, $logfile, %databases, $key, $database, $rightnow
);

$bdir = ‘/databases/’;
chomp ( $sysname = `uname -n` );
chomp ( $rightnow = `date +%m%d%G%H%M` );
#print $rightnow;
# Databases we want to backup and associated archive names.
# Database => sql file pairs.

%databases = (‘testdb1’ => ‘testdb1.sql’,
‘testdb2’ => ‘testdb2.sql’);

###
# Backup Section
###
foreach $key (sort keys %databases) {
$database = $databases{$key};
`mysqldump -uUSERNAME -pPASSWORD $key >> $bdir$sysname/$database`;}

###
# Compression Functionality
###

foreach $key (sort keys %databases) {
$database = $databases{$key};
`gzip -c $bdir$sysname/$database > $bdir$sysname/$rightnow$database.gz`;}

# scp them to the fileshare
#`scp $bdir$sysname/*.gz USERNAME\@SERVER:databases`;

###
# Clean backup dir
###

`rm -rf $bdir$sysname/*.sql`;
`mv $bdir$sysname/*.gz $bdir$sysname/store1`;

One thought on “Simple MySQL Backup Script”

Comments are closed.