Btrfs is a file system based on the copy-on-write (COW) principle, initially designed at Oracle Corporation for use in Linux.
Overview
2TB HDD에 사진, 공문서 같은 개인적인 데이터를 포함하여 각종 데이터를 저장할 것이기 때문에 데이터 백업과 복원은 상당히 중요하다. btrfs(Butter file system or b-tree file system)은 스냅샷을 이용한 데이터 백업 및 복원, 자체 압축, 서브볼륨 관리 등이 장점이다.
btrfs는 자신의 볼륨을 여러개의 서브볼륨 형태로 나눌 수 있기 때문에 디스크 전체를 btrfs 파티션으로 생성하고 서브볼륨 단위로 나눈다. 파티션의 경우 크기을 정해주어야 하는 것과 달리 btrfs의 서브볼륨은 용량에 제한이 없다. 또한, 각각의 서브볼륨을 스냅샷으로 저장해두면 언제든지 저장시점으로 되돌릴 수 있다. 스냅샷 이름의 형식을 윈도우 파일 버전 관리 이름 형식으로 마춰준다면 윈도우 파일 탐색기를 통해서도 복원이 가능하다.
SMB를 통해 윈도우 파일 탐색기에서도 홈 서버의 파일을 이전 버전으로 되돌릴 수 있다.
복원시점인 스냅샷은 서브볼륨 단위로 생성하고 삭제할 수 있다. 스냅샷에는 해당 시점에 있는 모든 파일들이 담겨있어서 스냅샷을 생성해 두면 파일을 삭제하여도 언제든지 복원이 가능하다. 해당 스냅샷 안에 있는 파일을 현재 서브볼륨으로 복사하는 것으로도 개별 파일 복원이 가능하다.
어떤 파일을 완전히 삭제하기 위해선 서브볼륨과 서브볼륨으로 생성한 모든 스냅샷에서 해당 파일을 지워야 한다. 다시 말하면, 모든 스냅샷에서 해당 파일을 지우지 않는 이상 여전히 디스크의 용량을 차지하고 있게 된다. 주기적으로 생성되는 스냅샷은 디스크 용량이 부족하거나 정리가 필요할 때 과감하게 지울 수 있어야 한다. 서브볼륨을 적절히 나누어 스냅샷 삭제 시 다른 서브볼륨에 영향이 없도록 한다.
$ sudo fdisk -c /dev/sdb
Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help):
위와 같이 Command (m for help): 프롬프트가 떴으면 순서대로 진행한다.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-3906963455, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-3906963455, default 3906963455):
Created a new partition 1 of type 'Linux' and of size 1.8 TiB.
파티션이 생성되었다. P 명령어로 파티션 정보를 출력할 수 있다.
Command (m for help): p
Disk /dev/sdb: 1.8 TiB, 2000365289472 bytes, 3906963456 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00021365
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 3906963455 3906961408 1.8T 83 Linux
파티션 타입을 변경하기 위해 L로 파티션 타입 리스트를 출력한다.
Command (m for help): l
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
LVM으로 선택한다.
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
변경사항을 저장한다.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
생성된 서브볼륨 리스트를 확인한다. 서브볼륨의 ID과 top level, 경로가 출력된다.
$ sudo btrfs subvolume list /mnt/hdd1_btrfs
ID 265 gen 19 top level 5 path @workspace
ID 266 gen 20 top level 5 path @cloud
ID 267 gen 21 top level 5 path @private
ID 268 gen 22 top level 5 path @pictures
ID 269 gen 23 top level 5 path @music
ID 270 gen 24 top level 5 path @torrent
ID 271 gen 25 top level 5 path @public
btrfs의 서브볼륨은 블록 디바이스가 아닌 독립적이고 마운트 가능한 POSIX 파일트리이다. 즉 마운트 시 마운트 지점의 소유자는 서브볼륨의 소유자가 된다. 생성할 서브볼륨 리스트에서 보았듯이 서브볼륨의 소유자도 변경한다.
Enable or disable backing up of the device/partition (the command dump). This field is usually set to 0, which disables it.
pass num
Controls the order in which fsck checks the device/partition for errors at boot time. The root device should be 1. Other partitions should be 2, or 0 to disable checking.
$ sudo mkdir /mnt/hdd1_btrfs/snapshots
$ cd /mnt/hdd1_btrfs/snapshots
$ sudo mkdir workspace private pictures music torrent public cloud
스냅샷 저장 구조를 간략하면 아래와 같을 것이다.
top-level (volume root directory, to be mounted at /mnt/hdd1_btrfs)
+-- @workspace (subvolume root directory, to be mounted at /home/jkpark/workspace)
+-- @public (subvolume root directory, to be mounted at /public)
\-- snapshots (directory)
\-- workspace (directory)
+-- @workspace_2019-02-21 (root directory of snapshot of subvolume "@workspace")
+-- @workspace_2019-02-22 (root directory of snapshot of subvolume "@workspace")
\-- public (directory)
+-- @public_2019-02-21 (root directory of snapshot of subvolume "@public")
+-- @public_2019-02-22 (root directory of snapshot of subvolume "@public")
date +%Y.%m.%d_%H.%M.%S 는 연.월.일_시.분.초 형식으로 저장하라는 명령이다. 반드시 이대로 작성해야 samba를 통해 윈도우OS에서 스냅샷을 인식하고 복원할 수 있다.
스냅샷이 생성되었는지 확인한다.
$ sudo btrfs subvolume list /mnt/hdd1_btrfs
ID 265 gen 30 top level 5 path @workspace
ID 266 gen 20 top level 5 path @cloud
ID 267 gen 26 top level 5 path @private
ID 268 gen 26 top level 5 path @pictures
ID 269 gen 26 top level 5 path @music
ID 270 gen 24 top level 5 path @torrent
ID 271 gen 27 top level 5 path @public
ID 272 gen 30 top level 5 path snapshots/workspace/@workspace_2019.03.07_20.25.03
$ sudo /usr/local/etc/btrfs-snapshot.sh workspace
Create a snapshot of '/mnt/hdd1_btrfs/@workspace' in '/mnt/hdd1_btrfs/snapshots/workspace/@workspace_2019.03.07_20.26.53'
05 6 * * mon,fri,sat,sun root /usr/local/etc/btrfs-snapshot.sh cloud을 살펴보면 매주 월,금,토,일요일 6시 5분에 root 권한으로 /usr/local/etc/btrfs-snapshot.sh cloud을 실행한다는 의미이다. 왼쪽부터 분, 시, 일, 월, 요일, 사용자, 명령어 순이다.
# 2019-03-07 | jkpark | added below 5 lines는 주석이다. 이렇게 주석으로 수정내역을 적어 놓으면 서버 관리에 도움이 된다.