-->

2019/03/16

APFSのsnapshotをCLI操作で(Mojave)

snapshot_apfs

APFSのsnapshotをCLI操作するメモ(Mojave版)

※SnapshotはOS verup(10.12〜10.14)でコマンドが変わり*1、以下ではマニュアルに記載されていないコマンドオプションも使うので諸々注意!
ref *1 apfs_snapshotはもうない
USB_snapshot
ちなみにAPFSのsnapshotはファイルシステムの機能としてあるものなので例えばUSBメモリーのsnapshot(TimeMachine関係なく)を自身内部にとることで,そのUSBメモリーだけを持ち運び,他のMac機でもマウント利用することも出来る(他人のMacへの持ち出し/マウントも可能だ)
ふと...APFSなUSBメモリーをiPhone/iPadでマウントしてsnapshot使うと^^,,,


--目次---

1.snapshot対象ボリュームを確認する

2.snapshot対象ボリュームにする

3.snapshot対象ボリュームから外す

4.snapshotを取る

5.snapshotを確認する

6.snapshotをマウントする

7.snapshotをアンマウントする

8.snapshotを個別に削除する

9.snapshot容量を減らす


—内容---

1.snapshot対象ボリュームを確認する*2

対象の場合:Included,否対象の場合:Excludedを返す
$ tmutil isexcluded volume_mount_point | Path/to/Volume
[ Included|Excluded ] volume_mount_point | Path/to/Volume

2.snapshot対象ボリュームにする*2

指定はマウントポイントで..
$ sudo tmutil removeexclusion -v volume_mount_point
ref.*2 Mojaveではtmutilを介さずにsnapshotを取る方法がなさそうなので,ここではTimeMachineの対象ボリュームか?を確認/設定しています<-他の方法で出来る|知っている人教えてください

3.snapshot対象ボリュームから外す

指定はマウントポイントで..
$ sudo tmutil addexclusion -v volume_mount_point

4.snapthotを取る*3

実際にはTimeMachine対象の全APFSボリュームのsnapshotを取ることに..
$ tmutil localsnapshot

Created local snapshot with date: yyyy-mm-dd-hhmmss
Ref.*3 ボリュームを指定してsnapshotを取る方法がなさそうです<-出来る|知っている人教えてください

5.snapshotを確認する

-tmutilでやる場合
指定はマウントポイントで..
$ tmutil listlocalsnapshots volume_mount_point

com.apple.TimeMachine.yyyy-mm-dd-hhmmss
com.apple.TimeMachine.yyyy-mm-dd-hhmmss

...
-数/snapshot時刻だけなら..
$ tmutil listlocalsnapshotdates [volume_mount_point]

yyyy-mm-dd-hhmmss
yyyy-mm-dd-hhmmss
...
-diskutilでやる場合*3
こちらだとXIDが表示される(<-apfs_mount時に何時時点のSnapshotかが解る)
$ diskutil apfs listsnapshots <device | volume_mount_point>

Snapshots for hoge (xx found)
|
+-- Name: com.apple.TimeMachine.yyyy-mm-dd-hhmmss
| XID: xxxx
| NOTE: This snapshot sets the minimal allowed size of APFS Container hoge
|
+-- Name: com.apple.TimeMachine.yyyy-mm-dd-hhmmss
| XID: xxx
...
-apfs_stats*4で(も)数だけなら見れる
以下は全ボリューム表示
$ apfs_stats |grep -e snapshots -e Volume

    Volume VolumeName VolumeUUID on device
     Number of snapshots = Num    <---------- ココ!
...

ref.

*3 diskutil apfs < listSnapshots | listVolumeSnapshots | snapshots >のコマンドポプションは同じ??

*4 /System/Library/Filesystems/apfs.fs/Contents/Resources/下にある

6.snapshotをマウントする

-tmutilでやる場合
マウントポイントは/Volumes/com.apple.TimeMachine.localsnapshots/下に固定,ボリュームは指定できるが,全時点のスナップショットがマウントされる
マウントするボリューム指定はマウントポイントで..
$ tmutil mountlocalsnapshots volume_mount_point

Mounted local snapshots: (
"/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/RootVolume/yyyy-mm-dd-hhmmss/Volume",
"/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/RootVolume/yyyy-mm-dd-hhmmss/Volume",
...
)

$ df
...
com.apple.TimeMachine.yyyy-mm-dd-hhmmss@/dev/diskXsX xxxxxx xxxxxx xxxxxx xx% xxx xxxxxxxxxxxx 0% /Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/RootVolumeName/yyyy-mm-dd-hhmms/Volume

com.apple.TimeMachine.yyyy-mm-dd-hhmmss@/dev/diskXsX xxxxxx xxxxxx xxxxxx xx% xxx xxxxxxxxxxxx 0% /Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/RootVolumeName/yyyy-mm-dd-hhmms/Volume

...

-mount_apfs*4でやる場合
tmutilと違って,どのボリュームのどの時点でのsnapshotをマウントするか?,また,どこにマウントするか?指定可能
以下は,com.apple.TimeMachine.yyyy-mm-dd-hhmmss時点の /Path/To/Volumeボリュームを volume_mount_pointにマウント
$ mkdir volume_mount_point
$ mount_apfs -s com.apple.TimeMachine.yyyy-mm-dd-hhmmss /Path/To/Volume volume_mount_point

mount_apfs: snapshot implicitly mounted readonly

$ df
...
com.apple.TimeMachine.yyyy-mm-dd-hhmmss@/dev/diskXsX xxxxxx xxxxxx xxxxxx xx% xxx xxxxxxxxxxxx 0% volume_mount_point
...
Remark! : mount_apfsの場合,Finder上では “VolumeName@snap-XID”で表示されるので注意! (XIDはdiskutil apfs listsnapshots <device | volume_mount_point>で日時と対応づける)

7.snapshotをアンマウントする

-tmutilの場合
$ tmutil unmountlocalsnapshots volume_mount_point

Unmounted local snapshots: (
"/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/RootVolume/yyyy-mm-dd-hhmmss/Volume",
"/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/RootVolume/yyyy-mm-dd-hhmmss/Volume",
...
)
-apfs_mountの場合
$ diskutil unmount volume_mount_point

Unmount successful for volume_mount_point

 

8.snapshotを個別に削除する

-tmutilの場合
snapshotはタイムスタンプ指定で..
$ tmutil deletelocalsnapshots yyyy-mm-dd-hhmmss

Deleted local snapshot 'yyyy-mm-dd-hhmmss' 


-diskutilの場合
デバイス名|マウントポイント と snapshot名|XID を指定して..
$ diskutil apfs deleteSnapshot

< device | volume_mount_point > <-xid XID | -name com.apple.TimeMachine.yyyy-mm-dd-hhmmss >

9.snapshot容量を減らす

ボリュームはマウントポイント指定,空ける容量:purge_bype,優先度(高1-4低):urgent_level
$ tmutil thinlocalsnapshots volume_mount_point [purge_byte] [urgent_level]

Thinned local snapshots:
com.apple.TimeMachine.yyyy-mm-dd-hhmmss
com.apple.TimeMachine.yyyy-mm-dd-hhmmss
...


--例---
$
Snapshot対象ボリュームを確認
$ tmutil isexcluded /Volumes/USB_APFS
[Excluded]    /Volumes/USB_APFS
$ tmutil isexcluded /
[Included]    /

ルートボリューム:対象,USBメモリー:否対象なので各々逆の設定にする
$ sudo tmutil removeexclusion -v /Volumes/USB_APFS
$ tmutil isexcluded /Volumes/USB_APFS
[Included]    /Volumes/USB_APFS
$ sudo tmutil addexclusion -v /
$ tmutil isexcluded /
[Excluded]    /

現時点でのsnapshotを調べる
$ diskutil apfs snapshots /Volumes/USB_APFS
Snapshots for disk4s1 (12 found)
|
+-- Name: com.apple.TimeMachine.2019-03-18-085140
|   XID:  3614
|
+-- Name: com.apple.TimeMachine.2019-03-18-102520
|   XID:  3836
|   NOTE: This snapshot sets the minimal allowed size of APFS Container disk4
|
+-- Name: com.apple.TimeMachine.2019-03-18-132018
|   XID:  3888
|
+-- Name: com.apple.TimeMachine.2019-03-18-132316
|   XID:  3893
|
+-- Name: com.apple.TimeMachine.2019-03-18-132337
|   XID:  3943
|
+-- Name: com.apple.TimeMachine.2019-03-18-132347
|   XID:  3949
|
+-- Name: com.apple.TimeMachine.2019-03-18-132358
|   XID:  3961
|
+-- Name: com.apple.TimeMachine.2019-03-18-132408
|   XID:  3973
|
+-- Name: com.apple.TimeMachine.2019-03-18-132418
|   XID:  3984
|
+-- Name: com.apple.TimeMachine.2019-03-18-132428
|   XID:  3986
|
+-- Name: com.apple.TimeMachine.2019-03-18-132439
|   XID:  3989
|
+-- Name: com.apple.TimeMachine.2019-03-18-132531
    XID:  4033

snapshotを取る
$ tmutil localsnapshot
Created local snapshot with date: 2019-03-18-134020

確認する
$ diskutil apfs snapshots /Volumes/USB_APFS
Snapshots for disk4s1 (13 found)
|
+-- Name: com.apple.TimeMachine.2019-03-18-085140
|   XID:  3614
|
+-- Name: com.apple.TimeMachine.2019-03-18-102520
|   XID:  3836
|   NOTE: This snapshot sets the minimal allowed size of APFS Container disk4
|
+-- Name: com.apple.TimeMachine.2019-03-18-132018
|   XID:  3888
|
+-- Name: com.apple.TimeMachine.2019-03-18-132316
|   XID:  3893
|
+-- Name: com.apple.TimeMachine.2019-03-18-132337
|   XID:  3943
|
+-- Name: com.apple.TimeMachine.2019-03-18-132347
|   XID:  3949
|
+-- Name: com.apple.TimeMachine.2019-03-18-132358
|   XID:  3961
|
+-- Name: com.apple.TimeMachine.2019-03-18-132408
|   XID:  3973
|
+-- Name: com.apple.TimeMachine.2019-03-18-132418
|   XID:  3984
|
+-- Name: com.apple.TimeMachine.2019-03-18-132428
|   XID:  3986
|
+-- Name: com.apple.TimeMachine.2019-03-18-132439
|   XID:  3989
|
+-- Name: com.apple.TimeMachine.2019-03-18-132531
|   XID:  4033
|
+-- Name: com.apple.TimeMachine.2019-03-18-134020
    XID:  4072
$ tmutil listlocalsnapshots /Volumes/USB_APFS
com.apple.TimeMachine.2019-03-18-085140
com.apple.TimeMachine.2019-03-18-102520
com.apple.TimeMachine.2019-03-18-132018
com.apple.TimeMachine.2019-03-18-132316
com.apple.TimeMachine.2019-03-18-132337
com.apple.TimeMachine.2019-03-18-132347
com.apple.TimeMachine.2019-03-18-132358
com.apple.TimeMachine.2019-03-18-132408
com.apple.TimeMachine.2019-03-18-132418
com.apple.TimeMachine.2019-03-18-132428
com.apple.TimeMachine.2019-03-18-132439
com.apple.TimeMachine.2019-03-18-132531
com.apple.TimeMachine.2019-03-18-134020
$ tmutil listlocalsnapshotdates /Volumes/USB_APFS
Snapshot dates for disk /Volumes/USB_APFS:
2019-03-18-085140
2019-03-18-102520
2019-03-18-132018
2019-03-18-132316
2019-03-18-132337
2019-03-18-132347
2019-03-18-132358
2019-03-18-132408
2019-03-18-132418
2019-03-18-132428
2019-03-18-132439
2019-03-18-132531
2019-03-18-134020
$ /System/Library/Filesystems/apfs.fs/Contents/Resources/apfs_stats |grep -e Volume -e snapshots
...
  Volume USB_APFS 0702E76A-6149-4E36-8161-51A60B650379 on disk4s1
    Number of snapshots = 13
...

マウントする(tmutil)
$ tmutil mountlocalsnapshots /Volumes/USB_APFS/
Mounted local snapshots: (
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-085140/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132428/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132531/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132418/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132439/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-134020/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132347/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132018/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132408/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132316/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132337/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132358/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-102520/USB_APFS"
)
$ tmutil unmountlocalsnapshots /Volumes/USB_APFS/
Unmounted local snapshots: (
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132531/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132358/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132428/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132018/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132418/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132316/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-085140/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132337/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132439/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132408/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-102520/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-134020/USB_APFS",
    "/Volumes/com.apple.TimeMachine.localsnapshots/Backups.backupdb/Suzumebachi/2019-03-18-132347/USB_APFS"
)
マウントする(mount_apfs)
$ mkdir ~/tmp
$ mount_apfs -s com.apple.TimeMachine.2019-03-18-134020 /Volumes/USB_APFS/ ~/tmp/
mount_apfs: snapshot implicitly mounted readonly
$ df -h
...
com.apple.TimeMachine.2019-03-18-134020@/dev/disk4s1  7.1Gi  1.6Gi  5.4Gi    24%       96 9223372036854775711    0%   /Users/hoge/tmp
...
$ mount
...
/dev/disk4s1 on /Volumes/USB_APFS (apfs, local, nodev, nosuid, journaled, noowners)
com.apple.TimeMachine.2019-03-18-134020@/dev/disk4s1 on /Users/hoge/tmp (apfs, local, nodev, nosuid, read-only, journaled, mounted by hoge)
...
$ diskutil apfs unmount ~/tmp
Unmount successful for /Users/hoge/tmp
$ df -h |grep TimeMachine
$ mount |grep TimeMachine

削除する(tmutil)
$ tmutil deletelocalsnapshots com.apple.TimeMachine.2019-03-18-134020
Deleted local snapshot '2019-03-18-134020'
$ tmutil listlocalsnapshots /Volumes/USB_APFS
com.apple.TimeMachine.2019-03-18-085140
com.apple.TimeMachine.2019-03-18-102520
com.apple.TimeMachine.2019-03-18-132018
com.apple.TimeMachine.2019-03-18-132316
com.apple.TimeMachine.2019-03-18-132337
com.apple.TimeMachine.2019-03-18-132347
com.apple.TimeMachine.2019-03-18-132358
com.apple.TimeMachine.2019-03-18-132408
com.apple.TimeMachine.2019-03-18-132418
com.apple.TimeMachine.2019-03-18-132428
com.apple.TimeMachine.2019-03-18-132439
com.apple.TimeMachine.2019-03-18-132531
削除する(diskutil)
$ diskutil apfs deletesnapshot /Volumes/USB_APFS/ -name com.apple.TimeMachine.2019-03-18-132531
Deleting APFS Snapshot XID 4033 "com.apple.TimeMachine.2019-03-18-132531" from APFS Volume disk4s1
Started APFS operation
Finished APFS operation
$ diskutil apfs deletesnapshot /Volumes/USB_APFS/ -xid 3989
Deleting APFS Snapshot XID 3989 "com.apple.TimeMachine.2019-03-18-132439" from APFS Volume disk4s1
Started APFS operation
Finished APFS operation
$ diskutil apfs deletesnapshot disk4s1 -name com.apple.TimeMachine.2019-03-18-13242
Deleting APFS Snapshot XID 3986 "com.apple.TimeMachine.2019-03-18-132428" from APFS Volume disk4s1
Started APFS operation
Finished APFS operation
$ diskutil apfs deletesnapshot disk4s1 -xid 3984
Deleting APFS Snapshot XID 3984 "com.apple.TimeMachine.2019-03-18-132418" from APFS Volume disk4s1
Started APFS operation
Finished APFS operation
$ diskutil listlocalsnapshots /Volumes/USB_APFS/
com.apple.TimeMachine.2019-03-18-085140
com.apple.TimeMachine.2019-03-18-102520
com.apple.TimeMachine.2019-03-18-132018
com.apple.TimeMachine.2019-03-18-132316
com.apple.TimeMachine.2019-03-18-132337
com.apple.TimeMachine.2019-03-18-132347
com.apple.TimeMachine.2019-03-18-132358
com.apple.TimeMachine.2019-03-18-132408

snapshot容量を減らす
$ tmutil thinlocalsnapshots /Volumes/USB_APFS/ 2147483648 1
Thinned local snapshots:
com.apple.TimeMachine.2019-03-18-085140
com.apple.TimeMachine.2019-03-18-102520
com.apple.TimeMachine.2019-03-18-132018
com.apple.TimeMachine.2019-03-18-132316
com.apple.TimeMachine.2019-03-18-132337
com.apple.TimeMachine.2019-03-18-132347
com.apple.TimeMachine.2019-03-18-132358
com.apple.TimeMachine.2019-03-18-132408
$ tmutil listlocalsnapshots /Volumes/USB_APFS/
$ 全て削除された...
$

ref.
tmutil(1)

mount_apfs(1)

diskutil(1)

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。

フォロワー