How to create a daily recursive snapshot of a Zpool


An advantage of copy-on-write is that when ZFS writes new data, the blocks containing the old data can be retained, allowing a snapshot version of the file system to be maintained. ZFS snapshots are created very quickly, since all the data composing the snapshot is already stored; they are also space efficient, since any unchanged data is shared among the file system and its snapshots.

In this post we will automate the daily snapshot creation of a Zpool using an script and a crontab entry. This is very handy when you loose files, a bad software installation or someone breaks something. It is some kind of “backup”.

Of course, you need to worry about the space consumption. If the file system changes very often you need to observe the amount of used space by the snapshots (this can be done using the zfs list command).

Basically, if you want to create a recursive snapshot, the comand line is:

# zfs snapshot -r pool_name@snapshot_name

I’ve created this small perl script to create a recursive snapshot based on the current week day. It needs one argument, the pool’s name (/usr/local/bin/daily_snapshot.pl). The snapshots will live for one week and they will be recreated after that:

#!/usr/bin/perl
 
if ( ($#ARGV + 1) == 0 ) {
    print "Usage: daily_snapshot.pl pool_name\n";
    exit 1;
}
my $pool = $ARGV[0];
 
$week_day= ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')[ (localtime)[6] ];
system("/usr/sbin/zfs destroy -r $pool\@$week_day");
system("/usr/sbin/zfs snapshot -r $pool\@$week_day");

Now, add an entry to the crontab for each Zpool you want to snapshot:

# crontab -e
[...]
0 7 * * * /usr/local/bin/daily_snapshot.pl BrutusZpool > /dev/null 2>&1
0 8 * * * /usr/local/bin/daily_snapshot.pl rpool > /dev/null 2>&1

This will create a daily snapshot of “BrutusZpool” and “rpool” at 7am and 8am, just before the people starts working ;-) .

After this, remember to check the space used by the snapshots, specially the first week:

# zfs list
NAME                                          USED  AVAIL  REFER  MOUNTPOINT
HAzpool                                       118G  16,1G  24,0K  none
[...]
HAzpool/qmailspool                           52,6G  16,1G  52,4G  /var/spool/qmail
HAzpool/qmailspool@jueves                    31,4M      -  52,2G  -
HAzpool/qmailspool@viernes                   20,8M      -  52,3G  -
HAzpool/qmailspool@sabado                    8,12M      -  52,3G  -
HAzpool/qmailspool@domingo                   9,15M      -  52,3G  -
HAzpool/qmailspool@lunes                     21,4M      -  52,3G  -
HAzpool/qmailspool@martes                    27,6M      -  52,4G  -
HAzpool/qmailspool@miercoles                 3,91M      -  52,4G  -
[...]

That’s all :-)

VN:F [1.9.12_1141]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.12_1141]
Rating: 0 (from 0 votes)
Sphere: Related Content

FacebookTwitterGoogle BookmarksLinkedInShare

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

,

  1. No comments yet.
(will not be published)