FreeBSD 13.2 OpenZFS: Difference between revisions
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..." |
(No difference)
|
Latest revision as of 14:53, 5 May 2023
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.
- 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.
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
- Replace pool_name with the name of your pool
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
- Replace pool_name with the name of your pool
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.