Wednesday Apr 11, 2007

Rolling Snapshots Made Easy

ZFS's snapshot capability is a great way of being able to preserve data quickly and easily. The overhead is minimal and accessing the data is a piece of cake. It even lets you snapshot an entire hierarchy at once. Let's say you've got a pool called 'tank', and you've created home directories off of it for anna, bill, chad, and dina:

NAME             USED  AVAIL  REFER  MOUNTPOINT
tank            2.73T  5.52T  2.73T  /tank
tank/home         95K  5.52T    23K  /zhome
tank/home/anna    18K  5.52T    18K  /zhome/anna
tank/home/bill    18K  5.52T    18K  /zhome/bill
tank/home/chad    18K  5.52T    18K  /zhome/chad
tank/home/dave    18K  5.52T    18K  /zhome/dina

Now you decide to create a snapshot at midnight every night. This way a user can always go back to 'yesterday' and retrieve a file he accidentally deleted or overwrote today. You set up a cronjob to execute a recursive snapshot, zfs snapshot -r tank/home@yesterday, and the result looks like this:

NAME                       USED  AVAIL  REFER  MOUNTPOINT
tank                      2.73T  5.52T  2.73T  /tank
tank/home                   95K  5.52T    23K  /zhome
tank/home@yesterday           0      -    23K  -
tank/home/anna              18K  5.52T    18K  /zhome/anna
tank/home/anna@yesterday      0      -    18K  -
tank/home/bill              18K  5.52T    18K  /zhome/bill
tank/home/bill@yesterday      0      -    18K  -
tank/home/chad              18K  5.52T    18K  /zhome/chad
tank/home/chad@yesterday      0      -    18K  -
tank/home/dina              18K  5.52T    18K  /zhome/dina
tank/home/dina@yesterday      0      -    18K  -

But what do you do once midnight rolls around again, as is its wont? One option would be to remove the old snapshots using zfs destroy -r tank/home@yesterday and take new ones. But even better would be to preserve the old snapshots. In current zfs implementations, you'd have to write a script, looping over the output of zfs list -Ho name | grep @yesterday and renaming each snapshot in turn.

With the change going into Nevada build 63, you can do this with one command, and all the renaming action happens inside the kernel:

# zfs rename -r tank/home@yesterday @2daysago
# zfs list
NAME                      USED  AVAIL  REFER  MOUNTPOINT
tank                     2.73T  5.52T  2.73T  /tank
tank/home                  95K  5.52T    23K  /zhome
tank/home@2daysago           0      -    23K  -
tank/home/anna             18K  5.52T    18K  /zhome/anna
tank/home/anna@2daysago      0      -    18K  -
tank/home/bill             18K  5.52T    18K  /zhome/bill
tank/home/bill@2daysago      0      -    18K  -
tank/home/chad             18K  5.52T    18K  /zhome/chad
tank/home/chad@2daysago      0      -    18K  -
tank/home/dina             18K  5.52T    18K  /zhome/dina
tank/home/dina@2daysago      0      -    18K  -

And now you can easily have rolling snapshots, with a simple script:

    zfs destroy tank/home@7daysago > /dev/null 2>&1
    zfs rename -r tank/home@6daysago @7daysago > /dev/null 2>&1
    zfs rename -r tank/home@5daysago @6daysago > /dev/null 2>&1
    ...
    zfs rename -r tank/home@yesterday @2daysago > /dev/null 2>&1
    zfs snapshot -r tank/home@yesterday

Extending it to save @7daysago as @1weekago, and preserve weekly snapshots is also easy.

About

Known throughout Sun as a man of infinite wit, of jovial attitude, and of making things up about himself at the slightest whim.

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
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