btrfs-snap-all 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/sh
  2. usage () {
  3. cat <<EOF>&2
  4. Usage: ${0##*/}
  5. Snapshot all Btrfs subvolumes older than 10 minutes to a read-only '.snapshots/SUBVOLUME/DATE'.
  6. Options:
  7. -n: No preview.
  8. -g: Run "git gc" on all .git folders.
  9. EOF
  10. }
  11. OPT_PREVIEW=true
  12. OPT_GIT_GC=false
  13. while getopts ":ghn" opt; do
  14. case $opt in
  15. g)
  16. OPT_GIT_GC=true ;;
  17. h)
  18. usage
  19. exit ;;
  20. n)
  21. OPT_PREVIEW=false ;;
  22. \?)
  23. usage
  24. exit 1 ;;
  25. esac
  26. done
  27. shift $(($OPTIND - 1))
  28. git_gc() { # $1=rootdir
  29. find "$1" -type d -name ".git" -print -exec git --git-dir {} gc \;
  30. }
  31. snap () { # $1=fs $2=subvol
  32. local fs=$1
  33. local subvol=$2
  34. local choice=N
  35. local last=$(find "$fs/.snapshots/$subvol" -maxdepth 1 | tail -1)
  36. $OPT_GIT_GC && git_gc "$fs/subvol"
  37. $OPT_PREVIEW && rmirror -i "$fs/$subvol" "$last"
  38. echo -n "==> Snapshot '$fs/${subvol}'? (y/N) "
  39. read -r choice
  40. echo ""
  41. case "$choice" in
  42. Y|y)
  43. btrfs-snap "$fs/$subvol" ;;
  44. esac
  45. }
  46. for fs in $(findmnt -l -t btrfs -n --output TARGET | grep /media); do
  47. for subvol in private public; do
  48. last=$(ls -1 "$fs"/.snapshots/"$subvol" | tail -1 | sed 's/_/ /')
  49. if [ -z "$last" ]; then
  50. continue
  51. fi
  52. last_date=$(date --date="$last" +%s)
  53. now=$(date +%s)
  54. if [ $(($now - $last_date)) -gt 600 ]; then
  55. snap "$fs" "$subvol"
  56. fi
  57. unset now
  58. unset last_date
  59. unset last
  60. done
  61. done