Monday, September 15, 2008

ZFS snapshots of a running MySQL instance

I run a group of MySQL servers on Solaris 10 with their data on a ZFS filesystem. I snapshot this data hourly, and this is the basic script I use to do it. I build upon this script to do other things after the snapshotting.

The script is called mysql_DataSnapshot, and is called with two parameters, SNAPGROUP and SNAPNUM. These are used to form a snapshot name. The intent is that SNAPGROUP is something like "hourly" or "daily", while SNAPNUM is an identifier within that group – the hour number for hourly snapshots, the day number for daily snapshots, or suchlike.

I make no claim to great code quality, but publish this in the hope that it might help someone else trying to do this.


#!/usr/bin/bash
# author: Matt Brown
# date: June 2, 2008
# Creates a snapshot of a MySQL instance's filesystem.

#---------------------------
# Variable Initialization
#---------------------------

# The script takes two arguments: the snapshot group
# and the snapshot number. These are encoded into
# the snapshot name as filesystem@group.number, e.g.
# mysql/data/4.1.12@hour.12
SNAPGROUP=$1
SNAPNUM=$2

filesystem=mysql/data/4.1.12
snapname=$filesystem@$SNAPGROUP.$SNAPNUM

#--------------------------------------
# Remove any pre-existing snapshot
#--------------------------------------
zfs destroy -R $snapname

#--------------------------------------
# Perform the mysqldump
#--------------------------------------
/var/local/mysql/default/bin/mysql --user=root \
--password='notreally' <<EOF
FLUSH TABLES WITH READ LOCK;
\! zfs snapshot $snapname
UNLOCK TABLES;
EOF

#--------------------------------------
# List the snapshot
#--------------------------------------
zfs list $snapname

0 comments: