#!/bin/bash make_subvol() { local usesudo local uid local gid [[ $EUID -ne 0 ]] && usesudo=sudo uid=${2:-$USER} gid=${3:-$uid} echo $usesudo btrfs subvolume create $1 $usesudo btrfs subvolume create $1 echo $usesudo chown $uid:$gid $1 $usesudo chown $uid:$gid $1 } make_base_subvols() { svols="shell admin opt data docker images temp" BTRFSDATAROOT=${BTRFSDATAROOT:-/mnt/data} # cd $BTRFSDATAROOT || exit for svol in $svols; do make_subvol $BTRFSDATAROOT/$svol $@ done } #ssudo mount -o subvol=opt /dev/sda4 /test alias btvl="sudo btrfs subvolume list" snapshot_restore () { local src=$1 local dest=$2 local name=${3:-$(echo $src| rev | cut -f 2- -d '.' | rev)} local usesudo echo copying $src at $PWD to $2 then renaming to $name # TODO check for snapshot, strip volume from source path, remove extra snapshot when done [[ $EUID -ne 0 ]] && usesudo=sudo $usesudo btrfs send $src | $usesudo btrfs -q receive $dest # cd $2 || return $usesudo btrfs subvolume snapshot $dest/$src $dest/$name # todo check for snapshot then delete transfered one } # ALL ALL = (root) NOPASSWD:/bin/btrfs subvolume_size () { local all; [[ $1 == "-a" ]] && all=true if [[ $all ]]; then sudo btrfs qgroup show "${2:-$PWD}" --kbytes | tail -n +3 str=$(sudo btrfs qgroup show "${2:-$PWD}" --kbytes | tail -n +3); else sudo btrfs qgroup show "${2:-$PWD}" --kbytes | grep $1 str=$(sudo btrfs qgroup show "${2:-$PWD}" --kbytes | grep $1) fi # echo $str subvolumeFolderSize=0; while read line; do FIELDS=( $line ) thisLineKb="${FIELDS[2]/'.00KiB'/''}"; # echo $thisLineKb subvolumeFolderSize=$((subvolumeFolderSize+thisLineKb)); done <<< "$str" [[ $all ]] && echo "size of entire filesystem at ${2:-$PWD}" || echo size of subvolumes/snapshots $1 at "${2:-$PWD}" echo $subvolumeFolderSize KB echo "~ $((subvolumeFolderSize/1024)) MB" echo "~ $((subvolumeFolderSize/1024/1024)) GB" } find_subvolumes () { sudo /bin/find $1 -maxdepth $2 -type d -exec sh -c ' for d do sudo btrfs subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$d" done' find-sh {} + } # sudo find / -type d -exec sh -c ' # for d do # btrfs subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$d" # done' find-sh {} + # You may want to exclude paths that are beyond suspicion. The following code excludes /proc, /sys and /dev: # sudo find / -type d \( \ # \( -path /proc -prune \) -o \ # \( -path /sys -prune \) -o \ # \( -path /dev -prune \) -o \ # \( -exec sh -c ' # for d do # btrfs subvolume show "$d" >/dev/null 2>&1 && printf "%s\n" "$d" # done # ' find-sh {} + \) \) mount_subvolume () { mkdir -p $3 echo msudo mount $1 -o subvol=$2 $3 sudo mount $1 -o subvol=$2 $3 }