FreeBSD 13.2 OpenZFS

From CompleteNoobs
Revision as of 14:53, 5 May 2023 by AwesomO (talk | contribs) (Created page with "==Safely Creating a ZFS Dataset in an Existing Directory== Assuming you have a username '''foo''' on your FreeBSD system, and your home directory is located at '''/usr/home/foo'''. Your ZFS pool, visible by running zfs list, is '''nuc/usr/home'''. Let's create a dataset named '''backup''' inside '''/usr/home/foo''' without overwriting any data. :* Identify the pool you want to create the dataset in. You can list your existing ZFS pools by running: <code>zfs list</c...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Please Select a Licence from the LICENCE_HEADERS page
And place at top of your page
If no Licence is Selected/Appended, Default will be CC0

Default Licence IF there is no Licence placed below this notice! When you edit this page, you agree to release your contribution under the CC0 Licence

LICENCE: More information about the cc0 licence can be found here:
https://creativecommons.org/share-your-work/public-domain/cc0

The person who associated a work with this deed has dedicated the work to the public domain by waiving all of his or her rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law.

You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

Licence:

Statement of Purpose

The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work").

Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others.

For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights.

1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following:

   the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work;
   moral rights retained by the original author(s) and/or performer(s);
   publicity and privacy rights pertaining to a person's image or likeness depicted in a Work;
   rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below;
   rights protecting the extraction, dissemination, use and reuse of data in a Work;
   database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and
   other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof.

2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose.

3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose.

4. Limitations and Disclaimers.

   No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document.
   Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law.
   Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work.
   Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work.

Safely Creating a ZFS Dataset in an Existing Directory

Assuming you have a username foo on your FreeBSD system, and your home directory is located at /usr/home/foo. Your ZFS pool, visible by running zfs list, is nuc/usr/home. Let's create a dataset named backup inside /usr/home/foo without overwriting any data.

  • Identify the pool you want to create the dataset in. You can list your existing ZFS pools by running:

zfs list

  • Before creating the dataset, ensure that the target directory for the new dataset does not exist. In this case, /usr/home/foo/backup should not exist yet.
  • Create the dataset in the desired ZFS pool. Replace nuc (which is the pool name) with the name of the ZFS pool you want to create the dataset in, and backup with the name you want to give the dataset:

sudo zfs create nuc/usr/home/foo_backup

  • Set the mountpoint for the dataset to the desired directory. Replace /usr/home/foo/backup with the path to the directory you want to use:

sudo zfs set mountpoint=/usr/home/foo/backup nuc/usr/home/foo_backup

  • Verify that the dataset is mounted at the correct location by running:

zfs list

You should see your dataset in the list with the correct mountpoint.

Now you have successfully created a ZFS dataset without wiping out an existing directory. The dataset is mounted at the specified directory, and you can use it as you normally would.

How to do it the Wrong Way and how to Recover
Doing it the Wrong way is not fun.... not fun at all

Assuming you have a username foo on your FreeBSD system, and your home directory is located at /usr/home/foo. Your ZFS pool, visible by running zfs list, is nuc/usr/home. Now let's assume you mistakenly try to create a dataset named backup inside /usr/home/foo without properly setting the mountpoint.

  • Create the dataset in the desired ZFS pool. Replace nuc (which is the pool name) with the name of the ZFS pool you want to create the dataset in, and backup with the name you want to give the dataset:

sudo zfs create nuc/usr/home/foo/backup

In this case, ZFS creates a dataset named backup inside the nuc/usr/home/foo dataset. By doing this, the existing /usr/home/foo directory will be replaced by the newly created nuc/usr/home/foo/backup dataset, causing the original contents of /usr/home/foo to be hidden.

Please note that the contents of /usr/home/foo are not technically wiped out, but they become inaccessible because the new dataset is now mounted on top of the /usr/home/foo directory. The original files still exist on the disk, but they are hidden by the new mountpoint.

To recover from this situation, you would need to unmount the newly created dataset, move its contents to another location (if needed), and then remove the dataset:

  • Unmount the new dataset:

sudo zfs unmount nuc/usr/home/foo/backup

  • If you added any content to the new dataset, move it to another location:

mv /usr/home/foo/backup/* /temporary/location/

  • Remove the new dataset:

sudo zfs destroy nuc/usr/home/foo/backup

After these steps, the original contents of /usr/home/foo should become accessible again.

To avoid such situations, always set the mountpoint for the new dataset explicitly, as shown in Safely Creating a ZFS Dataset in an Existing Directory.

Creating a snapshot and rolling back

Single DataSet

Creating a snapshot and rolling back to a previous snapshot are useful features of ZFS for data protection and recovery. Here's how to make a full snapshot and rollback/restore if something goes wrong:

Create a snapshot

To create a snapshot, you need to specify the dataset or ZFS pool you want to snapshot and give the snapshot a name. Use the following command:

sudo zfs snapshot pool_name/dataset_name@snapshot_name

Replace pool_name with the name of your ZFS pool, dataset_name with the name of the dataset you want to snapshot, and snapshot_name with a descriptive name for the snapshot.

For example, if you have a dataset named nuc/usr/home/foo_backup, you can create a snapshot called before_changes like this:

sudo zfs snapshot nuc/usr/home/foo_backup@before_changes

  • List snapshots:

To verify that the snapshot was created successfully, use the following command:

zfs list -t snapshot

This command will show you all the snapshots available on your system.

Rollback/restore a snapshot

  • View snapshot:

zfs list -t snapshot

If something goes wrong and you need to restore your dataset to a previous state, you can rollback to the snapshot you created earlier. Use the following command:

sudo zfs rollback pool_name/dataset_name@snapshot_name

Replace pool_name, dataset_name, and snapshot_name with the appropriate values for your snapshot.

For example, to rollback to the before_changes snapshot of the nuc/usr/home/foo_backup dataset, run:

sudo zfs rollback nuc/usr/home/foo_backup@before_changes

Please note that rolling back to a snapshot will destroy any changes made to the dataset after the snapshot was taken. Make sure you have backups of any important data before performing a rollback.

That's it! Now you know how to create a snapshot, list snapshots, and rollback to a previous snapshot in case something goes wrong. These features are part of what makes ZFS a powerful and flexible filesystem.

Recursively Create a Snapshot and Rollback

In this example, we will create a snapshot of the nuc/usr/home dataset and all its child datasets, and then roll back to this snapshot.

Recursively Create a Snapshot

Create a recursive snapshot of the nuc/usr/home dataset. Replace nuc/usr/home with the name of your dataset, and recursive_snapshot with a descriptive name for the snapshot:

sudo zfs snapshot -r nuc/usr/home@recursive_snapshot

The -r flag tells ZFS to create snapshots for all child datasets of nuc/usr/home.

  • Verify that the snapshots have been created successfully for the dataset and its child datasets:

zfs list -t snapshot

You should see the recursive_snapshot snapshot for the nuc/usr/home dataset and all its child datasets in the list.

Rollback to the Recursive Snapshot

Before rolling back, be aware that any changes made to the datasets after the snapshot was taken will be lost. Make sure you have backups of any important data before performing a rollback.

  • Roll back to the recursive_snapshot for the nuc/usr/home dataset and all its child datasets:

sudo zfs rollback -r nuc/usr/home@recursive_snapshot

This command will roll back the nuc/usr/home dataset and all its child datasets to the state they were in when the recursive_snapshot snapshot was taken.

  • Verify that the datasets have been rolled back to the snapshot:

zfs list -t snapshot

You should see the recursive_snapshot snapshot for the nuc/usr/home dataset and all its child datasets in the list, indicating that the rollback was successful.

Now you know how to create a recursive snapshot of a specific dataset, and roll back to that snapshot if necessary. Remember that rolling back to a snapshot will destroy any changes made to the datasets after the snapshot was taken, so always have backups of important data before performing a rollback.

Kind Of Full Recursive Backup and Restore

Backup - Snapshot

you can create snapshots for all datasets in your ZFS pool using a single command. In this case, the pool name is nuc. You can use the zfs list command to get a list of all datasets in your pool and then create a snapshot for each of them using a loop. Here's a step-by-step guide:

List all datasets in your ZFS pool

zfs list -o name -r nuc

This command lists the names of all datasets in the nuc pool, including child datasets.

Create a snapshot for each dataset in the pool
  • If using sh shell

zfs list -o name -r nuc | while read dataset; do sudo zfs snapshot "${dataset}@beforechange"; done

  • If using csh shell

zfs list -o name -r nuc | awk '{ system("sudo zfs snapshot "$1"@beforechange") }'

  • If using csh shell : If logged in as root and sudo not yet installed use:

zfs list -o name -r nuc | awk '{ system("zfs snapshot "$1"@beforechange") }'

This command uses a loop to create a snapshot named beforechange for each dataset in the nuc pool.

Verify that the snapshots have been created successfully

zfs list -t snapshot

You should see the beforechange snapshot for each dataset in the nuc pool in the list.

Keep in mind that this process will create snapshots for all datasets in the nuc pool. When rolling back, you will need to roll back each dataset individually.

Restore -Rollback

You can roll back all the datasets to their respective snapshots in one line using a loop, similar to the snapshot creation process. Here's how to roll back all datasets in the nuc pool to the beforechange snapshots:

Roll back all datasets to the beforechange snapshots
  • If using sh shell

zfs list -H -o name -t snapshot -r nuc | grep '@beforechange$' | while read snapshot; do sudo zfs rollback "$snapshot"; done

  • If using csh shell

zfs list -H -o name -t snapshot -r nuc | grep '@beforechange$' | awk '{ system("sudo zfs rollback "$1) }'

  • If using csh shell and logged in as root with sudo not installed - remove sudo from command:

zfs list -H -o name -t snapshot -r nuc | grep '@beforechange$' | awk '{ system("zfs rollback "$1) }'


This command lists all the beforechange snapshots in the nuc pool, and then uses a loop to roll back each dataset to its beforechange snapshot.

Keep in mind that rolling back to a snapshot will destroy any changes made to the datasets after the snapshot was taken. Make sure you have backups of any important data before performing a rollback.

Full system snapshot and rollback

First, make sure your entire system is on ZFS, including the root filesystem.

  • You are going to need to have a Live FreeBSD CD/DVD/USB or another bootable medium that has ZFS support. you can boot up.
    • This is necessary because you can't rollback the root filesystem while it's in use.

Backup - Snapshot full system

Step 1

  • Shutdown the computer and boot of the live FreeBSD USB

Step 2

  • Once in Shell Import/Mount the Root Pool with:

zpool import -R /mnt pool_name

  • Replace pool_name with the name of your pool
    • In my case the pool_name is called nuc

Step 3

  • Take a snapshot of all the datasets in your ZFS pool. For example, if your ZFS pool is called nuc, you can create a snapshot called full_system_backup like this:

zfs snapshot -r nuc@full_system_backup

The -r flag creates snapshots recursively for all child datasets.

Step 4

After taking the snapshot, export the ZFS pool:

zpool export pool_name

  • Replace pool_name with the name of your ZFS pool

Step 5

Reboot back into your system:

reboot

And thats your snapshot done, you can now reboot and go back to work, knowing you have a backup to roll back to.

Restore - Rollback full system

Step 1

  • Shutdown the computer and boot of the live FreeBSD USB

Step 2

  • Once in Shell Import/Mount the Root Pool with:

zpool import -R /mnt pool_name

  • Replace pool_name with the name of your pool
    • In my case the pool_name is called nuc

Step 3

  • Rollback all the datasets in your ZFS pool to the snapshot

zfs rollback -r nuc@full_system_backup

  • This command will rollback all datasets in the nuc pool to the full_system_backup snapshot.

Step 4

  • After the rollback is complete, export the ZFS pool

zpool export nuc

  • Replace nuc with the name of your ZFS pool

Step 5

  • Rebbot back into your system

reboot

Your system should now be restored to the state it was in when you took the full_system_backup snapshot.

Keep in mind that this process assumes your entire system, including the root filesystem, is on ZFS. If only specific parts of your system are on ZFS, you'll need to adjust the process accordingly.

Also, note that rolling back to a snapshot will destroy any changes made to the datasets after the snapshot was taken. Make sure you have backups of any important data before performing a rollback.