BTRFS scripts I made to work with snapper

If it doesn't relate to Debian, but you still want to share it, please do it here
Post Reply
Message
Author
User avatar
bester69
Posts: 1900
Joined: 2015-04-02 13:15
Been thanked: 1 time

BTRFS scripts I made to work with snapper

#1 Post by bester69 »

fstab must use subvolume name (rootsys) like this:

Code: Select all

/dev/sda2 /  btrfs   subvol=rootsys,defaults,noatime,space_cache,autodefrag 0 1
rootsys : root subvolume
/dev/sda2 (System partition)
/home/$USER/tt (Work directory)

btrlist
List all subvolumens

Code: Select all

#!/bin/sh
clear
sudo umount /home/$USER/tt
rmdir /home/$USER/tt
mkdir xZZ
sudo mount -t btrfs -o subvolid=0 /dev/sda2 ./xZZ
sudo btrfs subvolume list ./xZZ
sudo umount ./xZZ
rmdir xZZ
btrm
List all subvolumens and leave default subvolume mounted so you can operate them

Code: Select all

#!/bin/sh
#
clear
cd
mkdir tt
sudo mount -t btrfs -o subvolid=0 /dev/sda2 /home/$USER/tt
cd /home/$USER/tt
sudo btrfs subvolume list ./
btrdel : btrdel <<subvolume path>>
Delete subvolume using path

Code: Select all

#!/bin/sh
#
clear
cd
mkdir tt
sudo mount -t btrfs -o subvolid=0 /dev/sda2 /home/$USER/tt
sudo btrfs subvolume delete tt/"$1"
sudo umount tt
rmdir tt
btrlock: btrlock <<snapper id snapshot>>
Lock subvolume (id_snapper snapshot) by using chattr, and create a flag snapshot with label LOCKED, to know that id_snapper cant be deleted.

Code: Select all

#!/bin/sh
#
clear
cd /home/$USER/
mkdir tt
sudo mount -t btrfs -o subvolid=0 /dev/sda2 /home/$USER/tt
cd /home/$USER/tt
cd rootsys/.snapshots/$1/
sudo btrfs subvolume snapshot ./snapshot ./LOCKED
sudo btrfs subvolume delete ./LOCKED/snapshot
cd /home/$USER/tt
sudo chattr  +i rootsys/.snapshots/$1
sudo btrfs subvolume list .
cd /home/$USER/
sudo umount tt
rmdir  tt
btrunlock: btrunlock <<snapper id snapshot>>
Unkock subvolume (id_snapper snapshot) by using chattr, and remove flag snapshot labeled as LOCKED.

Code: Select all

#!/bin/sh
#
clear
cd /home/$USER/
mkdir tt
sudo mount -t btrfs -o subvolid=0 /dev/sda2 /home/$USER/tt
cd /home/$USER/tt
sudo chattr  -i rootsys/.snapshots/$1
sudo btrfs subvolume delete rootsys/.snapshots/$1/LOCKED
sudo btrfs subvolume list .
cd /home/$USER/
sudo umount tt
rmdir  tt
btrsys: btrsys << btrfs ID>>
Clone the subvolume selected << btrfs ID>> as new rootsys for using in the next boot, and rename, and move the active subvolume to a trash folder labeled "./del", so in the next boot might be purged with "btrpurge" script.

Code: Select all

#!/bin/sh
#
clear
cd /home/$USER/
mkdir tt
sudo mount -t btrfs -o subvolid=0 /dev/sda2 /home/$USER/tt
cd /home/$USER/tt
sudo mkdir mnt
sudo mount -t btrfs -o subvolid=$1 /dev/sda2 ./mnt
sudo btrfs subvolume snapshot ./mnt newrootsys
sudo umount ./mnt
sudo mv rootsys/.snapshots newrootsys/
sudo mkdir ./del
sudo mv rootsys ./del/rootsys-`date +%Y-%m-%d_%H:%M:%S`.log
sudo mv newrootsys rootsys
sudo btrfs subvolume list .
cd
sudo umount /home/$USER/tt
btrpurge
Purge all subvolumes stored in trash folder "./del". If btrsys applied, !!!NOT Apply before one reboot.

Code: Select all

#!/bin/sh
#
clear
cd /home/$USER/
mkdir tt
sudo mount -t btrfs -o subvolid=0 /dev/sda2 /home/$USER/tt
cd /home/$USER/tt/del
for i in *.log; do sudo btrfs subvolume delete "$i" ;done
sudo btrfs subvolume list .
cd
sudo umount /home/$USER/tt
EXAMPLES------------------------------------------------------------------

btrlist (Show all subvolumes)
ID 285 gen 112320 top level 404 path rootsys/.snapshots
ID 314 gen 107178 top level 333 path home/.snapshots
ID 333 gen 112320 top level 5 path home
ID 373 gen 112251 top level 285 path rootsys/.snapshots/1/snapshot
ID 404 gen 112319 top level 5 path rootsys
ID 406 gen 112251 top level 285 path rootsys/.snapshots/1/LOCKED
ID 407 gen 112320 top level 285 path rootsys/.snapshots/8/snapshot
373 and 406 are the same, and are both protected with chattr attribute (LOCKED flag indicator)

btrdel rootsys/.snapshots/8/snapshot (Delete subvolume by absolute path)
Delete subvolume (no-commit): '/home/myuser/tt/rootsys/.snapshots/8/snapshot'

btrunlock 1 (Unlock by Snapper ID; delete snapshot "LOCKED flag indicator", and remove chattr attribute.)
Delete subvolume (no-commit): '/home/myuser/tt/rootsys/.snapshots/1/LOCKED'
ID 285 gen 112327 top level 404 path rootsys/.snapshots
ID 314 gen 107178 top level 333 path home/.snapshots
ID 333 gen 112331 top level 5 path home
ID 373 gen 112251 top level 285 path rootsys/.snapshots/1/snapshot
ID 404 gen 112331 top level 5 path rootsys

btrsys 373 Create a snapshot of ID=373 (408), and set it as rootsys subvolume so in next boot the system will use it. Furthermore, rename and move to trash folder (del/rootsys-2017-12-30_06:04:27.log) the before one, so they can be purged eventually; not before the rebooting as we would be destroyng the active system subvolume.
ID 285 gen 112332 top level 408 path rootsys/.snapshots
ID 314 gen 107178 top level 333 path home/.snapshots
ID 333 gen 112340 top level 5 path home
ID 373 gen 112340 top level 285 path rootsys/.snapshots/1/snapshot
ID 404 gen 112340 top level 5 path del/rootsys-2017-12-30_06:04:27.log
ID 408 gen 112340 top level 5 path rootsys

btrpurge Delete all subvolumes moved to trash folder "./del"
Delete subvolume (no-commit): '/home/myuser/tt/del/rootsys-2017-12-30_06:04:27.log'
ID 285 gen 112332 top level 408 path rootsys/.snapshots
ID 314 gen 107178 top level 333 path home/.snapshots
ID 333 gen 112415 top level 5 path home
ID 373 gen 112340 top level 285 path rootsys/.snapshots/1/snapshot
ID 408 gen 112415 top level 5 path rootsys
bester69 wrote:You wont change my mind when I know Im right, Im not an ...

User avatar
bester69
Posts: 1900
Joined: 2015-04-02 13:15
Been thanked: 1 time

Re: BTRFS scripts I made to work with snapper

#2 Post by bester69 »

Updating snapper's subvolumes.:

Update Home subvolume.:
btrhomeupd.sh

Code: Select all

#!/bin/bash
#
pathsnap=/media/cache/.snapflag
touch /media/cache/.snapflag
snapper -c home delete  $(cat "$pathsnap")
A=$(snapper -c home create -p -d HomeActu)
echo "HomeActualiza con Numero: $A"
echo "$A" > "$pathsnap"
Update system subvolume.:
btrsysupd.sh

Code: Select all

#!/bin/bash
#
pathsnap=/media/cache/.snasyspflag
touch /media/cache/.snasyspflag

read -p "Actualiza sistema snapshot (y/n)?" choice
case "$choice" in 
  y|Y ) 
  echo "yes"
snapper -c System delete  $(cat "$pathsnap")
A=$(snapper -c System create -p -d system)
echo "System Actualiza con Numero: $A"
echo "$A" > "$pathsnap"
  ;;
  n|N ) echo "no";;
  * ) echo "invalid";;
esac
bester69 wrote:You wont change my mind when I know Im right, Im not an ...

kanliot
Posts: 36
Joined: 2013-09-02 08:00

Re: BTRFS scripts I made to work with snapper

#3 Post by kanliot »

Hi. I would appreciate a shorter post:

1. what you needed to do
2. how you chose to solve it
3. the btrfs feature you used to solve it.

thank you sir!

Post Reply