Penguin

Update, March 2010

I'm gratified there is still interest in a four-year-old article, but obviously times move on. If I were to do this again today, I would look at using Samba with ZFS. I also don't have a copy of the scripts handy (they were last seen on the server of my old employer in NZ), but if I find them, I will link them from this article. — CraigBox

What are shadow copies?

Shadow copies of shared folders are point-in-time snapshots of a shared folder that are exposed to Windows 2000+ users by way of the "Previous Versions" tab on a shared file.

The shadow copies client is built into Windows 2003 and Windows XP SP1 and above, and available for download for Windows 2000.

Can I run a 'shadow copy server' on Linux?

Yes, it is supported in Samba by way of a Samba VFS module.

The only source of documentation on setting up Samba to provide shadow copies to Microsoft clients is the Stackable VFS modules section of the Samba HOWTO.

This document will give you a good idea of how to set this all up.

Requirements

This document is written using Ubuntu Dapper, but you should be able to use the information on any distribution with new enough components.

  • Samba 3.0.3 or higher
  • A kernel that supports LVM and XFS
  • An LVM volume. Create one, if you haven't already.

LVM provides a method for taking a snapshot of a volume; it is these snapshots that will be shared and provided to Windows clients as shadow copies. XFS supports freezing a file system, buffering writes to it until it is thawed again. However, this doesn't actually work right now. Many filesystems in the kernel support waiting until the filesystem is consistent before taking a snapshot - you might wish to research this in more detail, but for this HOWTO we will use XFS.

Steps

1. Join a Windows domain, if you need to. I followed this howto, which should be rolled into our wiki, which is a huge mess of conflicting information about joining Windows domains that really needs some Aristotle love.

It's always handy to keep in time with your Windows network, so install ntp-simple and edit /etc/ntp.conf to parent off your AD server.

2. Set up a Samba share to the root of your LVM volume. Unfortunately, shadow copies don't work if you share a directory inside the volume. You can use "deep mounting" on Windows 2000 clients (net use Z: \\server\share\directory\directory) if you wish to get around this.

3. If "lsmod | grep dm" doesn't report dm_snapshot, add "dm_snapshot" to /etc/modules.

4. If you're on Debian or Ubuntu, check /etc/udev/udev.rules or /etc/udev/rules.d/20-names.rules for a line such as:

KERNEL=="dm-[0-9]*",                     NAME="dm/%n"

udev doesn't like the creation of devices without it having a say, so change it to

KERNEL=="dm-[0-9]*",                     OPTIONS+="ignore_device"

Source: udev: LVM snapshots don't work

It would also appear that with XFS, you should ignore any websites that tell you to use xfs_freeze, as this will hang lvcreate.

5. Configure Samba.

I have used something a little like this:

[data]
        comment = Snapshotted storage
        path = /array/storage
        read only = no
        browseable = yes
        force create mode = 0664
        force directory mode = 0775
        force group = "MYDOMAIN+Domain Users"
        writeable = yes
        vfs objects = shadow_copy

The important part is, of course, the shadow_copy VFS module.

You end up with an /array/storage directory that looks like this:

drwxr-xr-x 8 root  MYDOMAIN+domain users 4096 2006-05-02 12:14 .
drwxr-xr-x 3 root  root                  4096 2006-04-27 11:28 ..
drwxr-xr-x 8 root  MYDOMAIN+domain users 4096 2006-05-02 07:00 @GMT-2006.05.02-07.00.00
drwxr-xr-x 9 root  MYDOMAIN+domain users 4096 2006-05-02 12:00 @GMT-2006.05.02-12.00.00
drwxr-xr-x 9 root  MYDOMAIN+domain users 4096 2006-05-03 07:00 @GMT-2006.05.03-07.00.00
drwxr-xr-x 7 root  MYDOMAIN+domain users  148 2006-05-03 12:00 @GMT-2006.05.03-12.00.00
drwxrwxr-x 2 root  MYDOMAIN+domain users   31 2006-04-27 14:56 public

Samba presents you with just the public directory.

5. Create some scripts to do the work for you.

Scripts

OK, here's the fun part.

Because of the way LVM snapshots are created, you assign the amount of space for the changes you think will be made to it. If you assume you will have 1GB of changes every day and will keep snapshots for five days, you could make all your snapshots just over 5GB (including overhead).

I'm being a bit smarter. I'm creating the snapshot at 1GB, and then I'm growing it to 2GB on the second day, 3GB on the third day, etc.

For this example, I have created a rotation I call 'halfdaily', matching Windows 2003 by taking snapshots at 7am and 12pm; "before work" and "before lunch".

/etc/snapshots/halfdailytab
The table of snapshot date/times. Scripts will mount and umount these at boot/shutdown, and rotate them as appropriate.
/usr/local/sbin/create-snapshot
Creates and mounts a snapshot. Parameters is the snapshot name, which is the time of its taking (in date +%Y.%m.%d-%H.%M.%S format), and an optional snapshot size (in LVM format, ie 20M, 5G).
/usr/local/sbin/create-snapshot-now
Calculate the time now and create/mount a snapshot with that time, and default snapshot size defined in create-snapshot
/usr/local/sbin/remove-snapshot
Removes, unmounts, and removes the mount point for a snapshot. Parameter is the snapshot name.
/usr/local/sbin/grow-snapshot
Wrapper for lvextend; grows the snapshot in $1 by the size specied in $2, or by the default growth size defined in the script.
/usr/local/sbin/mount-all-snapshots
Mounts LVM snapshots into their directories. Intended for use at boot time. Call it with a number of snapshot table files, ie mount-all-snapshots /etc/snapshots/halfdailytab; if you have another rotation, you can specify more than one table.
/usr/local/sbin/umount-all-snapshots
As the name suggests, unmounts the snapshots listed in the table specified on the command line; intended for use at system shutdown time.
/usr/local/sbin/rotate-halfdaily-snapshots
This script brings it all together. Defined in it are a number of snapshots to keep, snapshot growth size, and the table to keep them in.

How to set it up

Put this in /etc/cron.d/snapshots:

# 7am and 12pm every day
0 7,12 * * *    /usr/local/sbin/rotate-halfdaily-snapshots

Put mount-all and umount-all in your startup sequence (FIXME: write this bit)

At 7am and 12pm every day, the script will run. It will see if it needs to expire a snapshot (if there are already the maximum number), and do so if required. It will grow all the snapshots by the defined increment, and it will create a new snapshot.

Extra for experts

If you want, you can have multiple rotations; it would make sense to have half days for the last 2 days and days for the last week; you can do this by having another table, /etc/snapshots/dailytab, and creating a rotate-daily-snapshots script to match. (Be sure to up the snapshot size).

You might also like to support non-Windows users, by letting them access the snapshots with NFS or SMB. Try symlinking to them, or writing a script to generate Samba shares to suit.

Conclusion

Congratulations; all going well you should have a Samba share with shadow copies. You can test it by editing a file, running create-snapshot-now, and editing the same file. After changing it, you should see a "Previous versions" tab appear on the files properties, with the time that you took your snapshot.

Notes:

  • If you don't change the file, you don't see previous versions for it.
  • You can do previous versions on directories, as well as files.
  • The snapshots will appear to the Linux machine to be complete copies of the filesystem at that point in time; with the proviso that they are read-only, you can do what you like with them.

Author

CraigBox

Links


CategoryHowto