вівторок, 28 вересня 2021 р.

Backup btrfs. Transfer snapshots to files on an external drive.


 And yes, we have:

  1. SanDisk 64GB usb3.1 USB stick with exFat partition with the LABEL "sunbeam".
  2. My not very old LinuxBox with usb-port usb2.0.
  3.  /.snapshots/root-2021-09-27 - Snapshot of the partition with the root of the operating system.
  4.  /.snapshots/var-2021-09-27 - Snapshot of the /var partition with OS variables.

To transfer a snapshot to a file on an external USB drive, use the command:


% sudo sh -c "btrfs send /.snapshots/root-2021-09-27 >| /run/media/user/sunbeam/root-2021-09-27.bin ; sync"


or


% sudo sh -c "btrfs send -f /run/media/user/sunbeam/root-2021-09-27.bin /.snapshots/root-2021-09-27 ; sync"


And for a snapshot of the /var partition:


% sudo sh -c "btrfs send /.snapshots/var-2021-09-27 >| /run/media/user/sunbeam/var-2021-09-27.bin ; sync"


or


% sudo sh -c "btrfs send -f /run/media/user/sunbeam/var-2021-09-27.bin /.snapshots/var-2021-09-27 ; sync"


After the operation, we check the availability of the files we need (In my case, transferring a snapshot

 of a root partition to external media took 45 minutes.) :


% ls -lah /run/media/user/sunbeam

-rwxr-xr-x 1 user users 7.7G Sep 27 21:38 root-2021-09-27.bin

-rwxr-xr-x 1 user users 421M Sep 28 10:53 var-2021-09-27.bin


You can now delete these snapshots from linuxbox using the appropriate btrfs subvolume delete 

command. And if necessary, restore them with the command:


% sudo sh -c "btrfs receive -f /run/media/user/sunbeam/root-2021-09-27.bin /.snapshots/ ; sync"


or


% sudo sh -c "cat /run/media/user/sunbeam/root -2021-09-27.bin |btrfs receive /.snapshots/ ; sync"


And accordingly for a /var snapshot:


% sudo sh -c "btrfs receive -f /run/media/user/sunbeam/var-2021-09-27.bin /.snapshots/ ; sync"


or


% sudo sh -c "cat /run/media/user/sunbeam/var-2021-09-27.bin |btrfs receive /.snapshots/ ; sync"