zfs 快照发送与接收

9 minute read

背景

zfs具备将快照输出到stdout,以及从stdout接收快照的功能。

这两个功能分别通过zfs send和zfs receive命令来完成。

例子

1. zfs send

有 -n -v -P 参数时,仅仅输出统计信息,不产生数据流。统计信息输出到stdout。

没有 -n -v -P 参数时,产生数据流,数据流输出到stdout,统计信息输出到stderr。

全量发送统计信息预览

#zfs send -v -P -n zp1/data01@2015123101  
full    zp1/data01@2015123101   1120712615896  
size    1120712615896  

跳跃增量发送统计信息预览

#zfs send -v -P -n -i zp1/data01@2015123101 zp1/data01@2015123103  
incremental     2015123101      zp1/data01@2015123103   106612682112  
size    106612682112  

递归增量发送统计信息预览

#zfs send -v -P -n -I zp1/data01@2015123101 zp1/data01@2015123103  
incremental     2015123101      zp1/data01@2015123102   78172555224  
incremental     2015123102      zp1/data01@2015123103   34534252176  
size    112706807400  

发送到文件

# zfs send zp1/data03@2015010301 > /u02/dege.zzz/zfs_back/abc  

2. zfs send

从标准输入接收快照信息,并在本地创建该快照。

有 -n -p 不创建快照,只输出统计信息

没有 -n -p 创建快照

例子:

# cat /u02/dege.zzz/zfs_back/abc | zfs receive -n -v -d zp1/data03  
would receive full stream of zp1/data03@2015010301 into zp1/data03/data03@2015010301  

abc存储的是全量快照 zp1/data03@2015010301

-d表示取名时删除第一个元素zp1,剩余的 data03@2015010301 写入目标zp1/data03后面,得到 zp1/data03/data03@2015010301

如果不使用-d,并且目标和源冲突的话,会报错如下:

# cat /u02/dege.zzz/zfs_back/abc | zfs receive -n -v zp1/data03  
cannot receive new filesystem stream: destination 'zp1/data03' exists  
must specify -F to overwrite it  

接收增量快照时,增量快照的前置快照必须已经在本地了。才能顺利根据增量快照,生成新的快照。

例如:

HOST A:

zfs snapshot zp1/data03@01  
zfs snapshot zp1/data03@02  
zfs snapshot zp1/data03@03  
zfs snapshot zp1/data03@04  
zfs send zp1/data03@01 > 01  
zfs send -i zp1/data03@01 zp1/data03@04  > 01_04  

HOST B:

假设host b 上没有zp1/data03的zfs filesystem,那么下面会自动创建 zp1/data03

cat 01 | zfs receive -d zp1  

这一步将生成

zp1/data03@01  
df -h|grep data03  
zp1/data03            3.1T  192K  3.1T   1% /zp1/data03  

然后接收增量

cat 01_04 | zfs receive -d zp1  

自动创建快照 zp1/data03@04

#zfs list -t snapshot|grep data03  
zp1/data03@01            16K      -   192K  -  
zp1/data03@04              0      -   192K  -  

参考

1. man zfs

       zfs send [-DnPpRveL] [-[iI] snapshot] snapshot  
  
       zfs send [-eL] [-i snapshot|bookmark] filesystem|volume|snapshot  
  
       zfs receive | recv [-vnFu] filesystem|volume|snapshot  
  
       zfs receive | recv [-vnFu] [-d|-e] filesystem  

发送快照到stdout

       zfs send [-DnPpRveL] [-[iI] snapshot] snapshot  
  
           Creates  a  stream  representation  of the second snapshot, which is written to standard output. The output can be redirected to a file or to a different system (for example, using ssh(1). By default, a  
           full stream is generated.  
  
           -i snapshot  
  
               Generate an incremental stream from the first snapshot (the incremental source) to the second snapshot (the incremental target).  The incremental source can be specified as the last component of the  
               snapshot name (the @ character and following) and it is assumed to be from the same file system as the incremental target.  
  
               If the destination is a clone, the source may be the origin snapshot, which must be fully specified (for example, pool/fs@origin, not just @origin).  
  
           -I snapshot  
  
               Generate  a  stream package that sends all intermediary snapshots from the first snapshot to the second snapshot. For example, -I @a fs@d is similar to -i @a fs@b; -i @b fs@c; -i @c fs@d. The incre-  
               mental source may be specified as with the -i option.  
  
           -R  
  
               Generate a replication stream package, which will replicate the specified filesystem, and all descendent file systems, up to the named snapshot. When received, all properties, snapshots,  descendent  
               file systems, and clones are preserved.  
  
               If  the  -i or -I flags are used in conjunction with the -R flag, an incremental replication stream is generated. The current values of properties, and current snapshot and file system names are set  
               when the stream is received. If the -F flag is specified when this stream is received, snapshots and file systems that do not exist on the sending side are destroyed.  
  
           -D  
  
               Generate a deduplicated stream. Blocks which would have been sent multiple times in the send stream will only be sent once. The receiving system must also support this feature to receive a  dedupli-  
               cated stream.  This flag can be used regardless of the dataset’s dedup  property, but performance will be much better if the filesystem uses a dedup-capable checksum (eg.  sha256).  
  
           -L  
  
               Generate  a  stream which may contain blocks larger than 128KB.  This flag has no effect if the large_blocks pool feature is disabled, or if the recordsize property of this filesystem has never been  
               set above 128KB.  The receiving system must have the large_blocks pool feature enabled as well.  See zpool-features(5) for details on ZFS feature flags and the large_blocks feature.  
  
           -e  
  
               Generate a more compact stream by using WRITE_EMBEDDED records for blocks which are stored more compactly on disk by the embedded_data pool feature.  This flag has no  effect  if  the  embedded_data  
               feature is disabled.  The receiving system must have the embedded_data feature enabled.  If the lz4_compress feature is active on the sending system, then the receiving system must have that feature  
               enabled as well. See zpool-features(5) for details on ZFS feature flags and the embedded_data feature.  
  
           -p  
  
               Include the dataset’s properties in the stream.  This flag is implicit when -R is specified.  The receiving system must also support this feature.  
  
           -n  
  
               Do a dry-run ("No-op") send.  Do not generate any actual send data.  This is useful in conjunction with the -v or -P flags to determine what data will be sent.  In this case, the verbose output will  
               be written to standard output (contrast with a non-dry-run, where the stream is written to standard output and the verbose output goes to standard error).  
  
           -P  
  
               Print machine-parsable verbose information about the stream package generated.  
  
           -v  
  
  
               Print verbose information about the stream package generated.  This information includes a per-second report of how much data has been sent.  
  
           The format of the stream is committed. You will be able to receive your streams on future versions of ZFS.  

发送快照到stdout

       zfs send [-eL] [-i snapshot|bookmark] filesystem|volume|snapshot  
  
           Generate  a  send  stream,  which may be of a filesystem, and may be incremental from a bookmark.  If the destination is a filesystem or volume, the pool must be read-only, or the filesystem must not be  
           mounted.  When the stream generated from a filesystem or volume is received, the default snapshot name will be "--head--".  
  
           -i snapshot|bookmark  
  
               Generate an incremental send stream.  The incremental source must be an earlier snapshot in the destination’s history. It will commonly be an earlier snapshot in  the  destination’s  filesystem,  in  
               which case it can be specified as the last component of the name (the # or @ character and following).  
  
               If the incremental target is a clone, the incremental source can be the origin snapshot, or an earlier snapshot in the origin’s filesystem, or the origin’s origin, etc.  
  
           -L  
  
               Generate  a  stream which may contain blocks larger than 128KB.  This flag has no effect if the large_blocks pool feature is disabled, or if the recordsize property of this filesystem has never been  
               set above 128KB.  The receiving system must have the large_blocks pool feature enabled as well.  See zpool-features(5) for details on ZFS feature flags and the large_blocks feature.  
  
           -e  
  
               Generate a more compact stream by using WRITE_EMBEDDED records for blocks which are stored more compactly on disk by the embedded_data pool feature.  This flag has no  effect  if  the  embedded_data  
               feature is disabled.  The receiving system must have the embedded_data feature enabled.  If the lz4_compress feature is active on the sending system, then the receiving system must have that feature  
               enabled as well. See zpool-features(5) for details on ZFS feature flags and the embedded_data feature.  

从stdin接收数据,并转存到zfs快照中

       zfs receive [-vnFu] filesystem|volume|snapshot  
       zfs receive [-vnFu] [-d|-e] filesystem  
  
           Creates a snapshot whose contents are as specified in the stream provided on standard input. If a full stream is received, then a new file system is created as well. Streams are created  using  the  zfs  
           send subcommand, which by default creates a full stream. zfs recv can be used as an alias for zfs receive.  
  
           If an incremental stream is received, then the destination file system must already exist, and its most recent snapshot must match the incremental stream’s source. For zvols, the destination device link  
           is destroyed and recreated, which means the zvol cannot be accessed during the receive operation.  
  
           When a snapshot replication package stream that is generated by using the zfs send -R command is  received, any snapshots that do not exist on the sending location are destroyed by using the zfs destroy  
           -d command.  
  
           The name of the snapshot (and file system, if a full stream is received) that this subcommand creates depends on the argument type and the use of the -d or -e options.  
  
           If the argument is a snapshot name, the specified snapshot is created. If the argument is a file system or volume name, a snapshot with the same name as the sent snapshot is created within the specified  
           filesystem or volume.  If neither of the -d or -e options are specified, the provided target snapshot name is used exactly as provided.  
  
           The -d and -e options cause the file system name of the target snapshot to be determined by appending a portion of the sent snapshot’s name to the specified target filesystem. If the -d option is speci-  
           fied, all but the first element of the sent snapshot’s file system path (usually the pool name) is used and any required intermediate file systems within the specified one are created.  If the -e option  
           is specified, then only the last element of the sent snapshot’s file system name (i.e. the name of the source file system itself) is used as the target file system name.  
  
           -d  
  
               Discard the first element of the sent snapshot’s file system name, using the remaining elements to determine the name of the target file system for the new snapshot as  described  in  the  paragraph  
               above.  
  
           -e  
  
               Discard all but the last element of the sent snapshot’s file system name, using that element to determine the name of the target file system for the new snapshot as described in the paragraph above.  
  
           -u  
  
               File system that is associated with the received stream is not mounted.  
  
           -v  
  
               Print verbose information about the stream and the time required to perform the receive operation.  
  
           -n  
  
               Do not actually receive the stream. This can be useful in conjunction with the -v option to verify the name the receive operation would use.  
  
           -F  
  
               Force a rollback of the file system to the most recent snapshot before performing the receive operation. If receiving an incremental replication stream (for example, one generated  by  zfs  send  -R  
               -[iI]), destroy snapshots and file systems that do not exist on the sending side.  

例子

       Example 12 Remotely Replicating ZFS Data  
  
       The following commands send a full stream and then an incremental stream to a remote machine, restoring them into poolB/received/fs@aand poolB/received/fs@b, respectively. poolB must contain the file system  
       poolB/received, and must not initially contain poolB/received/fs.  
  
         # zfs send pool/fs@a | \  
            ssh host zfs receive poolB/received/fs@a  
         # zfs send -i a pool/fs@b | ssh host \  
            zfs receive poolB/received/fs  
  
       Example 13 Using the zfs receive -d Option  
  
       The  following  command  sends  a full stream of poolA/fsA/fsB@snap to a remote machine, receiving it into poolB/received/fsA/fsB@snap. The fsA/fsB@snap portion of the received snapshot’s name is determined  
       from the name of the sent snapshot. poolB must contain the file system poolB/received. If poolB/received/fsA does not exist, it is created as an empty file system.  
  
         # zfs send poolA/fsA/fsB@snap | \  
            ssh host zfs receive -d poolB/received  
  
       Example 24 Creating a bookmark  
  
       The following example create a bookmark to a snapshot. This bookmark can then be used instead of snapshot in send streams.  
  
         # zfs bookmark rpool@snapshot rpool#bookmark  

Flag Counter

digoal’s 大量PostgreSQL文章入口