This week I finished setting up a new virtualmin server which uses BTRFS for /home. Each user has their own btrfs subvolume with about 10 automatic snapper snapshots going back 2 weeks. After configuring your vmin server as described below, each user can browse and read and recover old versions of (deleted) files by browsing their ~/.snapshots dir.
This is the poor mans version of what I previously did by integrating ZFS snapshots with virtualmin. I do not cover how I configured my btrfs /home dir here but this should still be useful for those familiar enough with BTRFS to configure your vmin servers disk config in a suitable manner.
I have already discussed the basics of configuring BTRFS quotas in a previous post on here;
If you wish to use snapper to create automatic btrfs snapshots of users home subvolumes, under Ubuntu and Debian you should configure /etc/snapper/config-templates/default first.
I use these snapper default settings to keep 10 snapshots per user subvol. BTRFS performance can start to suffer if you have more than about 15 or so snapshots per subvolume.
TIMELINE_LIMIT_HOURLY="3"
TIMELINE_LIMIT_DAILY="5"
TIMELINE_LIMIT_WEEKLY="2"
TIMELINE_LIMIT_MONTHLY="0"
TIMELINE_LIMIT_YEARLY="0"
This following script must be configured to be run via System Settings → Virtualmin Configuration → Actions upon server and user creation and pasting the full path to this script in the field next to Command to run before making changes to a server.
When you have this script configured to bed run from there, every time you create a new domain/user they will have snapper snapshots configured as well as a standardised btrfs quota limit configured for their subvolume. All of the users snapshots and their related snapper config files will be removed when you delete their domain/user using the regular virtualmin create/delete domain commands.
#!/bin/sh
if [ "$VIRTUALSERVER_ACTION" = "CREATE_DOMAIN" ]; then
#Create a new subvol for the user
/usr/bin/btrfs sub create /home/$VIRTUALSERVER_USER
#Limit the subvol to 1GB
/usr/bin/btrfs qgroup limit 1024M /home/$VIRTUALSERVER_USER
#Create a snapper config for the new user/subvol
/usr/bin/snapper -c $VIRTUALSERVER_USER create-config /home/$VIRTUALSERVER_USER
#Configure the users snapper config to enable them to use the snapper tool to list snapshots
/usr/bin/sed -i "/ALLOW_USERS/c\ALLOW_USERS=\"$VIRTUALSERVER_USER\"" /etc/snapper/configs/$VIRTUALSERVER_USER
#Change the users .snapshots dir permissions to allow the user read access
/usr/bin/chown $VIRTUALSERVER_USER:$VIRTUALSERVER_USER /home/$VIRTUALSERVER_USER/.snapshots
/usr/bin/chmod 500 /home/$VIRTUALSERVER_USER/.snapshots
fi
if [ "$VIRTUALSERVER_ACTION" = "DELETE_DOMAIN" ]; then
#Remove the users snapper config and snapshots
/usr/bin/snapper -c $VIRTUALSERVER_USER delete-config
#Delete the users subvolume
/usr/bin/btrfs sub delete /home/$VIRTUALSERVER_USER
fi