How to Use `dump` and `restore` for Backups on FreeBSD Operating System
dump
and restore
for Backups on FreeBSD Operating SystemCategories:
6 minute read
Introduction
In the realm of system administration, ensuring the safety and integrity of data is paramount. One of the most critical tasks for any system administrator is creating and managing backups. FreeBSD, a robust and highly reliable Unix-like operating system, provides several tools for this purpose. Among these, the dump
and restore
utilities stand out as powerful and flexible solutions for backing up and restoring filesystems. This article will provide a comprehensive guide on how to use dump
and restore
for backups on FreeBSD, covering everything from basic usage to advanced techniques.
Understanding dump
and restore
What is dump
?
The dump
utility is a traditional Unix tool used to create backups of filesystems. It works by copying data from a specified filesystem to a backup medium, such as a tape drive, disk, or even a file. dump
is particularly useful because it can perform incremental backups, meaning it only backs up files that have changed since the last backup. This makes it an efficient tool for managing backups over time.
What is restore
?
The restore
utility is the counterpart to dump
. It is used to extract data from a backup created by dump
and restore it to a filesystem. restore
can be used to recover entire filesystems or individual files and directories, making it a versatile tool for data recovery.
Prerequisites
Before diving into the usage of dump
and restore
, ensure that you have the following:
- FreeBSD System: A working FreeBSD installation with root or superuser access.
- Backup Medium: A storage medium where the backup will be stored. This could be an external hard drive, a network storage device, or even a file on the local filesystem.
- Filesystem to Backup: Identify the filesystem or directory you want to back up. For this guide, we’ll assume you’re backing up the
/home
directory.
Basic Usage of dump
Creating a Full Backup
A full backup is the most comprehensive type of backup, capturing all data in the specified filesystem. To create a full backup using dump
, follow these steps:
Identify the Filesystem: Determine the device name of the filesystem you want to back up. For example, if you’re backing up
/home
, you might find that it is mounted on/dev/ada0s1f
.Run the
dump
Command: Use the following command to create a full backup:dump -0u -f /backup/home.dump /dev/ada0s1f
-0
: Specifies a full backup (level 0).-u
: Updates the/etc/dumpdates
file, which keeps track of the last backup date and level.-f /backup/home.dump
: Specifies the output file for the backup./dev/ada0s1f
: The device name of the filesystem to back up.
Verify the Backup: After the backup completes, verify that the backup file (
/backup/home.dump
) has been created and contains the expected data.
Creating an Incremental Backup
Incremental backups only back up data that has changed since the last backup. This can save time and storage space. To create an incremental backup:
Determine the Last Backup Level: Check the
/etc/dumpdates
file to see the last backup level and date for the filesystem.Run the
dump
Command: Use the following command to create an incremental backup:dump -1u -f /backup/home_incremental.dump /dev/ada0s1f
-1
: Specifies an incremental backup (level 1).-u
: Updates the/etc/dumpdates
file.-f /backup/home_incremental.dump
: Specifies the output file for the backup./dev/ada0s1f
: The device name of the filesystem to back up.
Verify the Backup: As with the full backup, verify that the incremental backup file has been created and contains the expected changes.
Basic Usage of restore
Restoring an Entire Filesystem
To restore an entire filesystem from a backup, follow these steps:
Mount the Target Filesystem: If the target filesystem is not already mounted, mount it to a temporary location:
mount /dev/ada0s1f /mnt
Run the
restore
Command: Use the following command to restore the filesystem:restore -rf /backup/home.dump
-r
: Specifies that the restore should rebuild the filesystem.-f /backup/home.dump
: Specifies the backup file to restore from.
Unmount the Filesystem: After the restore completes, unmount the filesystem:
umount /mnt
Verify the Restore: Check that the filesystem has been restored correctly and that all data is intact.
Restoring Individual Files or Directories
If you only need to restore specific files or directories, you can use the interactive mode of restore
:
Change to the Target Directory: Navigate to the directory where you want to restore the files:
cd /mnt
Run the
restore
Command in Interactive Mode:restore -if /backup/home.dump
-i
: Enters interactive mode.-f /backup/home.dump
: Specifies the backup file to restore from.
Select Files to Restore: In interactive mode, you can use commands like
ls
to list files andadd
to select files or directories for restoration. Once you’ve selected the files, use theextract
command to restore them.Exit Interactive Mode: After restoring the desired files, exit interactive mode by typing
quit
.
Advanced Techniques
Automating Backups with Cron
To ensure regular backups, you can automate the dump
command using cron
. Here’s an example of a cron job that performs a full backup every Sunday and incremental backups every other day:
Edit the Crontab: Open the crontab for editing:
crontab -e
Add the Cron Jobs: Add the following lines to the crontab:
0 2 * * 0 /sbin/dump -0u -f /backup/home_full.dump /dev/ada0s1f 0 2 * * 1-6 /sbin/dump -1u -f /backup/home_incremental.dump /dev/ada0s1f
0 2 * * 0
: Runs at 2:00 AM every Sunday.0 2 * * 1-6
: Runs at 2:00 AM from Monday to Saturday.
Save and Exit: Save the crontab and exit the editor. The cron jobs will now run automatically at the specified times.
Using dump
and restore
with Remote Storage
If you want to store your backups on a remote server, you can use SSH to transfer the backup files. Here’s how:
Create the Backup Locally: First, create the backup as usual:
dump -0u -f - /dev/ada0s1f | gzip > /backup/home.dump.gz
Transfer the Backup to the Remote Server: Use
scp
to transfer the backup file to the remote server:scp /backup/home.dump.gz user@remote_server:/remote/backup/
Restore from the Remote Backup: To restore from a remote backup, first transfer the backup file back to the local system, or use SSH to pipe the backup directly into
restore
:ssh user@remote_server "cat /remote/backup/home.dump.gz" | gunzip | restore -rf -
Conclusion
The dump
and restore
utilities are powerful tools for managing backups on FreeBSD. Whether you’re performing full backups, incremental backups, or restoring individual files, these utilities provide the flexibility and reliability needed to ensure your data is safe. By automating backups with cron
and leveraging remote storage options, you can create a robust backup strategy that meets your needs. With the knowledge gained from this article, you should be well-equipped to use dump
and restore
effectively on your FreeBSD system.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.