ZFS 快照(snapshot)

好风 发表于 2016-11-17T04:52:43.927087Z
引用地址:https://plus.ooclab.com/note/article/1296

简介

参考:

准备实验环境:

[root@localhost ~]# zpool create stor1 mirror vdb vdc
[root@localhost ~]# zpool status
  pool: stor1
 state: ONLINE
  scan: none requested
config:

        NAME        STATE     READ WRITE CKSUM
        stor1       ONLINE       0     0     0
          mirror-0  ONLINE       0     0     0
            vdb     ONLINE       0     0     0
            vdc     ONLINE       0     0     0

errors: No known data errors

快照(snapshot)基本操作

创建快照

创建文件系统 stor1/docs , 并创建快照 stor1/docs@version0

[root@localhost ~]# zfs create stor1/docs
[root@localhost ~]# zfs snapshot stor1/docs@version0
[root@localhost ~]# zfs list -t snapshot
NAME                  USED  AVAIL  REFER  MOUNTPOINT
stor1/docs@version0      0      -    19K  -

重命名快照

[root@localhost ~]# zfs rename stor1/docs@version0 stor1/docs@initial
[root@localhost ~]# zfs list -t snapshot
NAME                 USED  AVAIL  REFER  MOUNTPOINT
stor1/docs@initial      0      -    19K  -

比较快照差异

[root@localhost ~]# echo "Hello" > /stor1/docs/1.txt
[root@localhost ~]# zfs snapshot stor1/docs@version1
[root@localhost ~]# echo "World" > /stor1/docs/2.txt
[root@localhost ~]# zfs snapshot stor1/docs@version2
[root@localhost ~]# zfs diff stor1/docs@initial stor1/docs@version1                                                                                                                            
M       /stor1/docs/
+       /stor1/docs/1.txt
[root@localhost ~]# zfs diff stor1/docs@version1 stor1/docs@version2
M       /stor1/docs/
+       /stor1/docs/2.txt
[root@localhost ~]# zfs diff stor1/docs@initial stor1/docs@version2                                                                                                                          
M       /stor1/docs/
+       /stor1/docs/1.txt
+       /stor1/docs/2.txt
[root@localhost ~]# echo "你好!" >> /stor1/docs/2.txt                                                                                                                                         
[root@localhost ~]# zfs snapshot stor1/docs@version3
[root@localhost ~]# zfs diff stor1/docs@version2 stor1/docs@version3
M       /stor1/docs/2.txt

回滚/恢复快照

可以 rollback 到指定快照(如果一次回滚多个快照,需要使用 -r 选项)。

[root@localhost ~]# zfs rollback stor1/docs@version3
[root@localhost ~]# zfs rollback -r stor1/docs@version1
[root@localhost ~]# cat /stor1/docs/1.txt 
Hello

危险 回滚会丢弃指定快照之后的数据!!!

克隆(clone)快照

[root@localhost ~]# zfs clone stor1/docs@version1 stor1/docs_version1_clone
[root@localhost ~]# cat /stor1/docs_version1_clone/1.txt 
Hello
[root@localhost ~]# zfs list -t snapshot                                                                                                                                                       
NAME                  USED  AVAIL  REFER  MOUNTPOINT
stor1/docs@initial      9K      -    19K  -
stor1/docs@version1     9K      -  19.5K  -
[root@localhost ~]# zfs list
NAME                        USED  AVAIL  REFER  MOUNTPOINT
stor1                       151K   464M    19K  /stor1
stor1/docs                 37.5K   464M  19.5K  /stor1/docs
stor1/docs_version1_clone     9K   464M  19.5K  /stor1/docs_version1_clone

删除 clone (同删除文件系统一样) :

[root@localhost ~]# zfs destroy stor1/docs_version1_clone
[root@localhost ~]# zfs list
NAME         USED  AVAIL  REFER  MOUNTPOINT
stor1        142K   464M    19K  /stor1
stor1/docs  37.5K   464M  19.5K  /stor1/docs

发送-接收快照

[root@localhost ~]# zfs send stor1/docs@version1 | zfs receive stor1/docs_received
[root@localhost ~]# zfs list
NAME                  USED  AVAIL  REFER  MOUNTPOINT
stor1                 520K   463M    19K  /stor1
stor1/docs           37.5K   463M  19.5K  /stor1/docs
stor1/docs_received  19.5K   463M  19.5K  /stor1/docs_received

发送增量数据:

[root@localhost ~]# echo "增量数据" > /stor1/docs/2.txt
[root@localhost ~]# zfs snapshot stor1/docs@version2
[root@localhost ~]# zfs list -t snapshot
NAME                           USED  AVAIL  REFER  MOUNTPOINT
stor1/docs@initial               9K      -    19K  -
stor1/docs@version1              9K      -  19.5K  -
stor1/docs@version2               0      -    20K  -
stor1/docs_received@version1      0      -  19.5K  -
[root@localhost ~]# zfs send -i stor1/docs@version1 stor1/docs@version2 | zfs receive stor1/docs_received
[root@localhost ~]# ls -l /stor1/docs_received/
总用量 2
-rw-r--r-- 1 root root  6 11月 18 08:41 1.txt
-rw-r--r-- 1 root root 13 11月 18 08:54 2.txt

zfs send -i 发送增量数据,必要前提是:第一个快照(stor1/docs@version1)已经被 stor1/docs_received 接收过了。


参考 ZFS文件系统介绍 – 快照和克隆

可以通过SSH将快照流发送到远程系统

# zfs send reservoir/data@snap1 | ssh remote_system zfs receive reservoir/data_received

还可以将快照流压缩归档保存

# zfs send reservoir/data@snap1 | gzip > data.gz

然后可以接收压缩归档的快照流

# zfs gunzip -c data.gz | zfs receive reservoir/data_recv

删除快照

[root@localhost ~]# zfs destroy stor1/docs@version2
[root@localhost ~]# zfs destroy stor1/docs@version1
[root@localhost ~]# zfs destroy stor1/docs@initial
[root@localhost ~]# zfs list -t snapshot
NAME                           USED  AVAIL  REFER  MOUNTPOINT
stor1/docs_received@version1     9K      -  19.5K  -
stor1/docs_received@version2      0      -    20K  -

如何定期删除快照请查看 https://plus.ooclab.com/console/note/article/view/1417