Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
2 total  XML / RSS feed 

zfs migration script

First rev, works but could use improvement

#!/bin/bash
if [ $# -ne 2 ]
then
    echo "Usage: $0 host_to_send_to zpool_name_on_destination"
    exit
fi
SERVER=$1
ZPOOL=$2
echo
echo "*********************************************************************************"
echo "*                                                                               *"
echo "* Please note, this wrapper only creates migration scripts for filesystems with *"
echo "* existing snapshots. If you want to migrate a snapshot-less filesystem, just   *"
echo "* run 'zfs snapshot zpool/filesystem@migrate' to create a snapshot which will   *"
echo "* allow this script to transfer it to the destination zpool.                    *"
echo "*                                                                               *"
echo "*********************************************************************************"
echo
if [ -f ./tmp/${SERVER}.screens ]
then
echo
echo "******************************************************************************"
echo "*"
echo "* Sorry, tmp/${SERVER}.screens exists, please run 'rm tmp/*' and try again..."
echo "*"
echo "******************************************************************************"
echo
exit
else
if [ ! -d tmp ]
then
mkdir tmp
fi
fi
for z in `zfs list | grep @ | awk '{print $1}' `;
do
FSNAME=`echo $z | awk -F@ '{print $1}' | awk -F/ '{print $NF}'`
if [ -f tmp/${FSNAME}.migrate ] 
then
echo "zfs backup -i $PREV $z | ssh root@$SERVER zfs restore $ZPOOL/${FSNAME}" >> tmp/${FSNAME}.migrate
PREV=$z
else
echo "Processing snapshots for ${FSNAME}."
echo "zfs backup $z | ssh root@$SERVER zfs restore $ZPOOL/${FSNAME}" > tmp/${FSNAME}.migrate
echo "screen -m -d bash tmp/${FSNAME}.migrate" >> tmp/${SERVER}.screens
PREV=$z
fi
done
echo
echo "**************************************************************************"
echo "*"
echo "* Ok, all finished - You can either run them individually, or simply type"
echo "*"
echo "* bash tmp/${SERVER}.screens"
echo "*"
echo "* at a prompt to run them concurrently in disconnected screen sessions."
echo "*"
echo "**************************************************************************"
echo

Moving a ZFS filesystem and all of it's snapshots from one zpool to another

Go into the root directory

[hostname:/] root# cd /


You can look in the filesystem and you'll see it's contents and the .zfs folder

[hostname:/] root# ls -l old-pool/filesystem                                                                                                                                                                   
total 24
dr-xr-xr-x   3 root     root           3 Mar 31 00:34 .zfs/
drwxr-xr-x  18 root     root         101 Mar 11 21:45 etc/
drwx------   4 root     root          16 Feb 19 08:07 root/
drwxr-xr-x   3 root     root           3 Feb 19 21:18 usr_local_etc/
drwxr-xr-x   3 root     root           3 Feb 19 21:18 usr_local_var_db_mysql/


Make a final "migration" snapshot that represents the lastest old-pool/filesystem

[hostname:/] root# zfs snapshot old-pool/filesystem@migration                                                                                                                                                   


You can see there are 3 snapshots in there

[hostname:/] root# ls -l old-pool/filesystem/.zfs/snapshot/                                                                                                                                                         
20060329/ 20060330/ migration/


Do a zfs backup of the oldest snapshot and pipe that into a zfs restore. This is will make the filesystem in the new-pool. You could also do this over ssh.

[hostname:/] root# zfs backup old-pool/filesystem@20060329 | zfs restore new-pool/filesystem@20060329


Now you do an incremental (-i) backup and restore using the first snapshot you used above and the one that comes after it. The key here is that incremental backups expect there to be a pre-existing new-pool/filesystem, this is how it is diffrent from the non-incremental backup above.

[hostname:/] root# zfs backup -i old-pool/filesystem@20060329 old-pool/filesystem@20060330 | zfs restore new-pool/filesystem


Do an incremental on the next pair. This happens to be with the last "migration" snapshot.

[hostname:/] root# zfs backup -i old-pool/filesystem@20060330 old-pool/filesystem@migration | zfs restore new-pool/filesystem


When you look in the new-pool/filesystem you'll see that it has been populated from the last migration snapshot.

[hostname:/] root# ls -l new-pool/filesystem                                                                                                                                                                   
total 24
dr-xr-xr-x   3 root     root           3 Mar 31 00:34 .zfs/
drwxr-xr-x  18 root     root         101 Mar 11 21:45 etc/
drwx------   4 root     root          16 Feb 19 08:07 root/
drwxr-xr-x   3 root     root           3 Feb 19 21:18 usr_local_etc/
drwxr-xr-x   3 root     root           3 Feb 19 21:18 usr_local_var_db_mysql/


And all three snapshots are present

[hostname:/] root# ls -l new-pool/filesystem                                                                                                                                                      
20060329/ 20060330/ migration/


There will be some other interesting things to do as soon as there is a "zfs remove _device_"
« Newer Snippets
Older Snippets »
2 total  XML / RSS feed