Backup and Restore Strategies for FreeBSD Systems

Jul 19, 2023 • FreeBSDSoftware

As you start working on FreeBSD systems, one of the fundamental practices you need to establish is backing up data. This approach will guarantee that your valuable data remains intact even in case of an accidental deletion, hardware failure, or system compromise. Concurrently, it would be best if you also had a reliable restore strategy in place to retrieve data effectively when you need it. This post will give you an in-depth understanding of Backup and Restore Strategies for FreeBSD Systems.

Before we delve into the details, you can brush up your basics about FreeBSD with our post on Understanding Basic Commands in FreeBSD.

Creating Backups

Creating backups on FreeBSD is a straightforward task thanks to the availability of several easy-to-use tools. We will discuss two widely used options: dump and rsync.

Using dump Tool

The dump command-line tool is natively available in FreeBSD. It can back up an entire filesystem or selected directories. To use dump, start by determining the partition you want to back up using the df command. The dump tool uses the -L flag to correctly handle file systems that are currently in use. Here’s a basic usage example:

# dump -0uan -f - /usr | gzip -2 | dd of=/mnt/backup/usr-backup.gz bs=64k

The -0uan option ensures the backup is full and not incremental. The -f - option helps write the backup to standard output. You can recover these backups using the restore command.

Using rsync Tool

Unlike dump, rsync enables incremental backups, meaning it only copies the changes made since the last backup. This feature saves both time and storage space.

It’s not installed by default, so use the pkg tool to install it.

# pkg install rsync

Here’s an example of using rsync to backup the /usr directory:

# rsync -a /usr /mnt/backup

In this case, /usr indicates the source directory, while /mnt/backup represents the backup directory.

If you want to understand more about FreeBSD’s package management, do visit our post on Package Management for FreeBSD.

Restoring Backups in FreeBSD

After taking backups, it’s vital to know how to restore them during emergencies.

Restoring with restore Tool

The restore command works in conjunction with the dump command to restore data. If you backed up data using dump, you could restore it using restore. Here’s an example:

# cd /mnt/backup/
# gzip -dc usr-backup.gz | restore -rf -

Restoring with rsync Tool

rsync can also restore backups. The process is similar to creating backups: you need to reverse the source and destination.

# rsync -a /mnt/backup /usr

Automating Backups in FreeBSD

For more consistent backups, FreeBSD allows for the automation of the backup process with Cron jobs. For more details on managing services and daemons in FreeBSD, check out our post on managing services and daemons.

Here’s an example of a Cron job for a backup:

0 3 * * * root /usr/sbin/dump -0uan -h 0 -f- /usr | gzip -2 | dd of=/mnt/backup/usr-backup-$(date +%Y%m%d).gz bs=64k

This job will run every day at 3:00 am.

Knowing how to backup and restore your FreeBSD system is crucial for system administration. Whether it’s for system recovery, migrating data, or protecting against accidental deletion - these strategies are invaluable. For more articles on FreeBSD, stay tuned to our blog updates in categories like system administration and more.


Checkout these related ports: