Advertisement
creepynut

sanoid -> shadow symlinker

Mar 17th, 2015
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.72 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #
  3. # Create symbolic links from sanoid's autosnaps
  4. # to something that samba's vfs_shadow_copy2 can read
  5. #
  6. # This script should be run from a directory at the root
  7. # of a ZFS filesystem
  8. #
  9. # Known caveats:
  10. # will break if sanoid creates snapshots with spaces
  11. # or funny characters in the snapshot names
  12. #
  13.  
  14. set -e
  15.  
  16. target_snaproot=$(cd $(dirname "$0"); pwd)
  17. zfs_snaproot=$(cd "$target_snaproot/../.zfs/snapshot"; pwd)
  18.  
  19.  
  20. # bail if we can't find ZFS's snapshot directory
  21. [ -d "$zfs_snaproot" ] || ( echo "ZFS snap dir $zfs_snaproot not found" ; exit )
  22.  
  23. while read snapdate;
  24. do
  25.   source_snapdir=""
  26.   target_snapdir="$target_snaproot/$snapdate"
  27.  
  28.   # determine which snapshot to symlink for a given date
  29.   # i.e. if an there is a yearly and monthly snapshot for the same date, use the yearly one
  30.   if [ -d "$zfs_snaproot/autosnap_${snapdate}_yearly" ] ; then
  31.     source_snapdir="$zfs_snaproot/autosnap_${snapdate}_yearly" ;
  32.   elif [ -d "$zfs_snaproot/autosnap_${snapdate}_monthly" ] ; then
  33.     source_snapdir="$zfs_snaproot/autosnap_${snapdate}_monthly" ;
  34.   elif [ -d "$zfs_snaproot/autosnap_${snapdate}_daily" ] ; then
  35.     source_snapdir="$zfs_snaproot/autosnap_${snapdate}_daily" ;
  36.   elif [ -d "$zfs_snaproot/autosnap_${snapdate}_hourly" ] ; then
  37.     source_snapdir="$zfs_snaproot/autosnap_${snapdate}_hourly" ;
  38.   fi;
  39.  
  40.   # this shouldn't happen - but check the next date if we
  41.   # don't find one of sanoid's snapdirs
  42.   [ -z "$source_snapdir" ] && continue;
  43.  
  44.   echo ln -sf "$source_snapdir" "$target_snapdir"
  45.   ln -sf "$source_snapdir" "$target_snapdir"
  46.  
  47. done < <( cd "$zfs_snaproot"; ls -1 -d autosnap* | perl -ne '/autosnap_(.+)_(monthly|hourly|yearly|daily)/; print "$1\n";' | sort -u )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement