Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

BTRFS scripts I made to work with snapper

Off-Topic discussions about science, technology, and non Debian specific topics.
Post Reply
Message
Author
User avatar
bester69
Posts: 2072
Joined: 2015-04-02 13:15
Has thanked: 24 times
Been thanked: 14 times

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:STOP 2030 globalists demons, keep the fight for humanity freedom against NWO...

User avatar
bester69
Posts: 2072
Joined: 2015-04-02 13:15
Has thanked: 24 times
Been thanked: 14 times

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:STOP 2030 globalists demons, keep the fight for humanity freedom against NWO...

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