среда, 27 октября 2010 г.

ZFS snapshots or something new on something old

Everyone knows that snapshots are fascinating feature of ZFS. Automatic snapshots in OpenSolaris used to save my mental health. After migration to FreeBSD I thought "It would be easy. There are a lot of decisions for managing ZFS snapshots in FreeBSD". In fact, theare are too many decisions for managing ZFS snapshots in FreeBSD. It's hard to choose one decision. I've looked for example, on sysutils/zfsnap, sysutils/zfs-snapshot-mgmt... But my needs are quite different:
  • I'd like the decision for making automatic snapshots to be simple and effective as an axe.
  • I'd like to understand how every line of these scripts (for creating and deleting snapshots) work to feel myself comfortable.
  • I'm going to use snapshots on my home desktop, and I'd only like to have quite recent copy of my data to recover files after making some silly mistakes.

At last, I decided to create my own solution which was supposed to work as rc script (I boot my computer once or twice per day, and it would be good to have startup snapshot of the system).
The text of script is provided below.

#!/bin/sh

. /etc/rc.subr

name="zfs_snapshot"

start_cmd="create_snapshots"
stop_cmd="drop_snapshots"
zfs_pool=${zfs_snapshot_pool:-"zpool"}
keep_days=${zfs_snapshot_keep_days:-30}

create_snapshots()
{
echo "Creating zfs snapshots..."

if [ -z "${zfs_snapshot_filesystems}" ]; then
snapshot_filesystems=`zfs list -H -o name -t filesystem`;
else
snapshot_filesystems=${zfs_snapshot_filesystems};
fi



DATE=`env LC_ALL=C date -j "+%Y_%m_%d-%H:%M"`
for fs in $snapshot_filesystems ; do
snap="${fs}@AUTO_${DATE}"
logger -t $name "Creating snapshot $snap"
zfs snapshot $snap
done
}

drop_snapshots()
{
echo "Destroying old automatic zfs snapshots"

current_date=`date "+%s"`
if [ -z "${zfs_snapshot_filesystems}" ]; then
snapshot_filesystems=`zfs list -H -o name -t filesystem`;
else
snapshot_filesystems=${zfs_snapshot_filesystems};
fi

for fs in $snapshot_filesystems ; do
snapshots=`zfs list -H -t snapshot -o name -r $fs |grep "^${fs}@AUTO_"`;
for snap in $snapshots; do
creation_local=`zfs list -H -t snapshot -o creation $snap`
creation_real=`date -j -f "%a %b %d %k:%M %Y" "${creation_local}" "+%s"`

eval creation_minus=$(( ( ${current_date} - ${creation_real})/3600/24 ))
if [ $creation_minus -gt $keep_days ]; then
logger -t $name "Destroying snapshot $snap"
zfs destroy $snap
fi
done
done

}

load_rc_config $name
run_rc_command $*


There are three parameters for my script - zfs_snapshot_pool, zfs_snapshot_filesystems and zfs_snapshot_keep_days. First or second parameter may be used to specify pool (and all its FS for creating snapshots) or explicitly selecting necessary filesystems. Parameter fs_snapshot_keep_days determines when snapshot is considered outdated.
This simple script is supposed to make my life more comfortable...

Комментариев нет:

Отправить комментарий