jimsalterjrs / sanoid

These are policy-driven snapshot management and replication tools which use OpenZFS for underlying next-gen storage. (Btrfs support plans are shelved unless and until btrfs becomes reliable.)
http://www.openoid.net/products/
GNU General Public License v3.0
3.09k stars 302 forks source link

we need pre_prune_script #475

Open constin opened 4 years ago

constin commented 4 years ago

Hello , i'd like to be able unmount some snapshots before delete it. It not possible to delete mounted snapshots, and prune_script running after prune, not before.

phreaker0 commented 4 years ago

@constin I just tried and you can destroy mounted snapshots just fine. ZFS will unmount the snapshot first and destroys it after.

constin commented 4 years ago

@phreaker0

it's not. ZFS 0.8.2

root@zfs-snap-test:# mount | grep 14_06:15:22
zpool/it@autosnap_2020-01-14_06:15:22_frequently on /zpool/it/@backup/2020-01-14_06:15 type zfs (ro,relatime,xattr,noacl)
root@zfs-snap-test:# zfs destroy zpool/it@autosnap_2020-01-14_06:15:22_frequently
cannot destroy snapshot zpool/it@autosnap_2020-01-14_06:15:22_frequently: dataset is busy
root@zfs-snap-test:# umount zpool/it@autosnap_2020-01-14_06:15:22_frequently
root@zfs-snap-test:# zfs destroy zpool/it@autosnap_2020-01-14_06:15:22_frequently
root@zfs-snap-test:# 
phreaker0 commented 4 years ago

dataset is busy -> you are using the mount so the unmount fails, so an ordinary "umount /zpool/it/@backup/2020-01-14_06:15" would fail too with an message like "is in use".

constin commented 4 years ago

i don't know how to mount snapshot (without cloning && without mount command). And i don't need clone i need just mounted snapshot in readonly mode. It's possibe usezfs set snapdir=visible but this is not the same i want. I already tried to implement pre_prune_script by myself it's easy , but i'm not good in perl to make it perfect. This can be made easily add will make your script more flexible

phreaker0 commented 4 years ago

Ah, you mounted it directly. I used the zfs automounter. You don't need to use "snapdir=visible", you can just access the snapshots via the hidden ".zfs" directory: ls -l ${datasetMountpoint}/.zfs/snapshot/

I actually use the autosnap snapshots too for backup purposes, but I will clean up the mountpoints after it finished.

constin commented 4 years ago

ok , easy solution it just make main symlink from /.zfs/snapshot/ to / from dataset but in my task i don't want "daily/monthly etc tags in backup folders name" And that's why i getting problem with same folders name for for example daily and monthly snapshot made in same time.

I wrote simple bash script based on symlink solution:

post_snap_script:

#!/bin/bash

symlink_name=$(echo $SANOID_SNAPNAME| cut -f 2- -d '_' | cut -f -2 -d '_')

function weight {
    current_weight=0
    [ "$1" = "frequently" ] && current_weight=1
    [ "$1" = "hourly" ] && current_weight=2
    [ "$1" = "daily" ] && current_weight=3
    [ "$1" = "weekly" ] && current_weight=4
    [ "$1" = "monthly" ] && current_weight=5
    [ "$1" = "yearly" ] && current_weight=6
    echo $current_weight
}

# if symlink already exist
if [ -L /$SANOID_TARGET/@backup/$symlink_name ]; 
    then
    sym_ls=$(ls -l /$SANOID_TARGET/@backup/$symlink_name)
    current_symlink_tag=$(echo $sym_ls| cut -f 5- -d '_')
    candidate_symlink_tag=$(echo $SANOID_SNAPNAME | cut -f 4- -d '_')
    #if candidate shlould delete current symlink?
    [[ "$(weight $candidate_symlink_tag)" -gt "$(weight $current_symlink_tag)" ]] &&  ln -nsf  /$SANOID_TARGET/.zfs/snapshot/$SANOID_SNAPNAME /$SANOID_TARGET/@backup/$symlink_name
#else just create new symlink
else
    ln -s  /$SANOID_TARGET/.zfs/snapshot/$SANOID_SNAPNAME /$SANOID_TARGET/@backup/$symlink_name
fi

prune_script:

#!/bin/bash

symlink_name=$(echo $SANOID_SNAPNAME| cut -f 2- -d '_' | cut -f -2 -d '_')

# if symlink already exist and broken (snapshot not exist)
if [ -L /$SANOID_TARGET/@backup/$symlink_name ] &&  [ ! -e /$SANOID_TARGET/@backup/$symlink_name ]
    then
    rm /$SANOID_TARGET/@backup/$symlink_name
fi

works fine, anyway i'd like to have pre_prune ability for future tasks