#!/bin/bash

# - bof - #



# -  - #
# - When this error happen on ssh command - #

#  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#  @    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
#  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#  # -  - #
#  ssh-keygen -R 192.168.122.3;



# -  - #
# - Documentation - #
https://work-work.work/blog/2018/12/01/ubuntu-1804-btrfs.html
https://askubuntu.com/questions/1298465/create-btrfs-snapshots-of-home-when-its-not-set-up-as-a-subvolume



# :a
# -  - #
# - Convert system to subvolumes - #
# Your / is /dev/sda2
# Boot from a LiveUSB
# Moved / to @ subvolume & /home to @home subvolume

sudo mount /dev/vda3 /mnt;
sudo btrfs sub create /mnt/@;
sudo btrfs sub create /mnt/@home;
sudo btrfs sub create /mnt/@var;
sudo btrfs sub create /mnt/@.snapshots;

sudo btrfs subvolume list .;


sudo mv /mnt/home/* /mnt/@home/;

#sudo cp -r /mnt/home/* /mnt/@home/;
#sudo rm -rf /mnt/home;

sudo mv /mnt/* /mnt/@/;

#grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB --recheck;
#grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB --recheck;


# -  - #
# - Mount subvolumes - #
sudo mount -o noatime,compress=lzo,space_cache=v2,subvol=@ /dev/vda3 /mnt;
sudo mount /dev/vda1 /mnt/boot/efi;

sudo mkdir -p /mnt/{boot,home,var,.snapshots};
sudo mount -o noatime,compress=lzo,space_cache=v2,subvol=@home /dev/vda3 /mnt/home;

mount -o noatime,compress=lzo,space_cache=v2,subvol=@var /dev/sda3 /mnt/var;
mount -o noatime,compress=lzo,space_cache=v2,subvol=@.snapshots /dev/sda3 /mnt/.snapshots;

# - commit: Periodic interval (second) data is synchronized to permanent storage - #
#mount -o noatime,commit=120,compress=lzo,space_cache=v2,subvol=@ /dev/sda3 /mnt;

# -  - #
# - Create Data subvolume - #
mkdir -p /mnt/Data;
mount /dev/sda4 /mnt/Data;
btrfs subvolume create /mnt/Data/@Data;
umount /mnt/Data;

# -  - #
# - Mount Data subvolume - #
mount -o noatime,compress=lzo,space_cache=v2,subvol=@Data /dev/sda4 /mnt/Data;

# -  - #
# - Efi mount (Arch alone install) - #
sudo mount /dev/sda1 /mnt/boot;






# :b
# -  - #
# - Edit /etc/fstab - #
# Add subvol=@ to where / is mounted
# Add subvol=@home to where /home is mounted 

UUID=4b284d4f-8fed-45d0-b692-218ecf3e9279 /     btrfs subvol=@     0 0
UUID=4b284d4f-8fed-45d0-b692-218ecf3e9279 /home btrfs subvol=@home 0 0



# /etc/fstab: static file system information.
# <file system>                          <dir>     <type> <options> <dump> <pass>

UUID=3CB5-D565                            /boot/efi vfat   defaults  0      1
UUID=6087584a-03a5-43bb-8e10-47174074977b none      swap   sw        0      0

UUID=4b284d4f-8fed-45d0-b692-218ecf3e9279 /         btrfs  defaults  0      1
UUID=35dcf391-10d8-4f03-844e-58efb8097078 /Data     btrfs  defaults  0      1



# /etc/fstab: static file system information.
# <file system>                          <dir>       <type> <options>       <dump>  <pass>
UUID=0548-C74E                            /boot/efi   vfat   umask=0077      0       1
UUID=c38b8134-b158-43aa-a414-dd8dceda2db7 none        swap   sw              0       0

UUID=91d6e1d7-c53d-4e19-9e3b-b40eff5857ec /           btrfs  defaults,subvol=@,ssd,noatime,compress=zstd,space_cache=v2,discard=async,autodefrag              0  0
UUID=91d6e1d7-c53d-4e19-9e3b-b40eff5857ec /home       btrfs  defaults,subvol=@home,ssd,noatime,compress=zstd,space_cache=v2,discard=async,autodefrag          0  0
UUID=91d6e1d7-c53d-4e19-9e3b-b40eff5857ec /.snapshots btrfs  defaults,subvol=/@/.snapshots,ssd,noatime,compress=zstd,space_cache=v2,discard=async,autodefrag  0  0
UUID=02bd707e-697a-4a2e-8c49-d0e1d6de3e5d /Data       btrfs  defaults,subvol=@Data,ssd,noatime,compress=zstd,space_cache=v2,discard=async,autodefrag          0  0


# :c
# -  - #
# - Update grub - #
#grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB --recheck;
#grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB --recheck;

sudo mount --bind /dev /mnt/dev;
sudo mount --bind /proc /mnt/proc;
sudo mount --bind /sys  /mnt/sys;

sudo chroot /mnt;
sudo chroot /mnt update-grub;

sudo update-grub;
exit;
sudo umount /mnt;
sudo shutdown now;













# -  - #
# - Documentation - #
https://linuxhint.com/use-btrfs-snapshots/



# :a
# -  - #
# - Create btrfs snapshots - #
sudo mkdir -vp /Data/.snapshots;
sudo btrfs subvolume snapshot /Data /Data/.snapshots/Data_2022-01-14;
sudo btrfs subvolume snapshot /Data /Data/.snapshots/Data_$(date +%Y-%m-%d_%H-%m-%S);
sudo btrfs subvolume list /Data;
sudo btrfs subvolume show /Data/.snapshots/Data_2022-01-14;
tree -a /Data;

# -  - #
# - Create /home btrfs snapshots - #
sudo mkdir -vp /home/.snapshots;
sudo btrfs subvolume snapshot /home /home/.snapshots/Data_$(date +%Y-%m-%d_%H-%m-%S);
sudo btrfs subvolume list /home;

# :b
# -  - #
# - Recover a folder content - #
cp -vr /Data/.snapshots/Data_2022-01-14/Documents/Pictures/* /Data/Documents/Pictures;

# :c
# -  - #
# - Recover complete snapshot - #
rsync -avz /Data/.snapshots/Data_2022-01-14/ /Data;

# - Don't use - #
# - Deletes the snapshot as well - #
#rsync -avz --delete /Data/.snapshots/Data_2022-01-14/ /Data;

# :d
# -  - #
# - Update snapshot - #
sudo cp -vr /Data/Documents/Pictures/Backgrounds_3.txt /Data/.snapshots/Data_2022-01-14/Documents/Pictures/;

# :e
# -  - #
# - Create a "ReadOnly" snapshot - #
sudo btrfs subvolume snapshot -r /Data /Data/.snapshots/Data_2022-01-14_2;

sudo btrfs subvolume list /Data;
sudo btrfs subvolume show /Data;
sudo btrfs subvolume show /Data/.snapshots/Data_2022-01-14;
sudo btrfs subvolume show /Data/.snapshots/Data_2022-01-14_2;

# :f
# -  - #
# - Delete a snapshot - #
sudo btrfs subvolume delete /Data/.snapshots/Data_2022-01-14;
sudo btrfs subvolume list /Data;
tree -a /Data;



# :z
# -  - #
# - Manage btrfs subvolumes - #
sudo btrfs subvolume list /;
sudo btrfs subvolume list / -apt;
sudo btrfs subvolume list .;
sudo btrfs subvolume list /Data;

sudo btrfs subvolume show /;
sudo btrfs subvolume show /home;
sudo btrfs filesystem show | awk '/ path /{print $NF}'
sudo btrfs filesystem show;
sudo btrfs filesystem show /;
sudo btrfs filesystem show | awk '/ path /{print $NF}';
sudo btrfs filesystem show | grep devid;

sudo btrfs filesystem label /home;
sudo btrfs filesystem df /;
sudo btrfs filesystem df /root;
sudo btrfs filesystem df /home;
sudo btrfs filesystem df /Data;
btrfs device usage /;

sudo systemctl list-units --type=mount --all;

df -h /;
df -h /Data;

# -  - #
# - Show mounted subvolumes - #
df -ht btrfs;
findmnt -nt btrfs;
findmnt -nt btrfs;
findmnt -nt btrfs --output TARGET;
mount | grep btrfs;
sudo btrfs check --force /dev/vda4;

sudo btrfs filesystem label
sudo btrfs subvolume snapshot / /snap/root



https://www.christitus.com/btrfs-guide

Create Snapshot

sudo btrfs subv snapshop /home /home/.snapshots/2020-01-13

Using this you can revert the snapshop by simply editing the /etc/fstab and changing the subvol=2020-01-13 or the corresponding subvolid you get from sudo btrfs subv list /home


Restore Snapshot
Restore Snapshop after reboot and successful rollback

sudo btrfs subvolume delete /home
sudo btrfs subvolume snapshot /home/.snapshots/2020-01-13 /home



# - eof - #
