Tuesday Nov 25, 2008

ZFS send/recv

After my moments of stupidity documented below I decided I'd fix the problem. I've coded up a script that will use zfs send and recv to replicate my home directory. At the moment it's manual, but I'll look into hooking into an automatic system:

#!/bin/bash

USER=fifors
HOST=stormspike.sfbay.sun.com
FS="rpool/export/home"
RS="rpool/zion-home-backup"
MAX_SNAP=15
NOW=`date +%Y%m%d%H%M%S`
INIT=0

set -- `getopt i $\*`
if [ $? != 0 ]
then
echo ${USAGE}
exit 2
fi

for i in $\*
do
case $i in
-i) echo Initialising Snapshot backup;
INIT=1;
shift;
continue;;
esac
done

ALIVE=`ping ${HOST} 2>&1 | grep -c alive`

if [ ${ALIVE} ]
then
SNAPS=`zfs list -t snapshot |grep "${FS}@" | wc -l | tr -d " "`
echo Determine incremental
if [ "${INIT}" == "0" ]
then
ISNAP=`zfs list -t snapshot | grep rpool/export/home@ |tail -1 |cut -d " " -f 1`
fi

echo "Creating zfs snapshot ${FS}@${NOW} ..."
zfs snapshot ${FS}@${NOW} > /dev/null 2> /tmp/error.$$
if [ $? -ne 0 ]
then
echo "Failed ($?)!"
cat /tmp/error.$$
rm /tmp/error.$$
exit 1
else
echo "Successfully created snapshot: ${FS}@${NOW} !"
fi
rm /tmp/error.$$
if [ ${SNAPS} -ge ${MAX_SNAP} ]
then
LAST=`zfs list -t snapshot | grep "${FS}@" | head -1 | awk '{ print $1}'`
echo "Destroying last snapshot: ${LAST}..."
zfs destroy ${LAST} > /dev/null 2> /tmp/error.$$
if [ $? -ne 0 ]
then
echo "Failed ($?)!"
cat /tmp/error.$$
rm /tmp/error.$$
exit 1
else
echo "Successfully destroyed snapshot: ${LAST} !"
fi
rm /tmp/error.$$
fi

if [ "${INIT}" == "1" ]
then
echo "Initialising the zfs replication of ${FS}..."
zfs send ${FS}@${NOW} | ssh ${USER}@${HOST} pfexec /sbin/zfs recv ${RS}@${NOW}
else
echo "Initiate the zfs send of ${FS}@${NOW} to ${RS}@${NOW}..."
zfs send -i ${ISNAP} ${FS}@${NOW} | ssh ${USER}@${HOST} pfexec /sbin/zfs recv -F ${RS}@${NOW}
fi

if [ $? -ne 0 ]
then
echo "Failed ($?)!"
exit 1
else
echo "Successfully synced snapshot: ${FS}@${NOW} and ${RS}@${NOW} !"
fi
else
echo "Cannot connect to ${HOST}"
fi

It's pretty simple really, just relies on the underlying aspects of ZFS to make it all ok.

Grr.. it looks like html destroys the format... I'll fix that later.

About

Peter Buckingham

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today