The is the procedure for creating a quick backup of a directory, and the files in it, by creating an archive with TAR on FreeBSD.
What is TAR on FreeBSD?
TAR is an archive utility, that can be used to archive directories and files into a single archive file. TAR is a fast, safe and native utililty on FreeBSD. TAR works much like ZIP, which is not a native utility on FreeBSD. The benefit of no compression is faster speed and less risc of data corruption.
Creating a backup of a directory with TAR.
In the following example, the BIND DNS directory /usr/local/etc/namedb, and the directories and files in it, will be archived with TAR. The file name for the archive will be named after the ISO 8601 date and the directory. This naming convension is a good practice for quick backup archives. The archive will be stored in current directory, which by default is the home directory. It is a good practice to not leave archives in the active system directories, despite being convenient.
# tar -cf 2025-06-24-namedb.tar /usr/local/etc/namedb
The directory, and the directories and files in it, has now been archived and stored in current directory.
# ls *.tar
2025-06-24-namedb.tar
Listing files in a TAR archive.
In the following example, the contents of the archive is listed to standard out. The archived directories and files are relative to the current directory by default.
# tar -tf 2025-06-24-namedb.tar
usr/local/etc/namedb/
usr/local/etc/namedb/named.conf
Reading (extracting) a file in a TAR archive.
Reading a file in a TAR archive, which means extracting it to default out, can be used for inspecting without extracting to file system first. In the following example, the named.conf is extracted from the archive to standard out. This will show the contents of the file and the output can be piped to utilities, such as GREP, MORE, LESS and TAIL.
# tar -xOf 2025-06-24-namedb.tar usr/local/etc/namedb/named.conf
Extracting from a TAR archive.
In the following example, the directory, and its directories and files, is extracted from the archive. The directory will be extracted to the current directory by default.
# tar -xf 2025-06-24-namedb.tar
In the following example, the directory, and its directories and files, is extracted from the archive to an optional temporary directory.
# tar -xf 2025-06-24-namedb.tar -C tmp/
In the following example, the directory, and its directories and files, is extracted from the archive to the root directory. The will restore the backup to original location. Warning! This will overwrite the original files.
# tar -xf 2025-06-24-namedb.tar -C /
Encrypting a TAR archive with GPG.
The TAR archive, or any file, can be encrypted with a password by using GPG. In the following example, the an encrypted copy of the TAR archive is created with GPG.
# tar -cf - /usr/local/etc/namedb | gpg --symmetric --pinentry-mode loopback > 2025-06-24-namedb.tar.gpg"
Enter passphrase: Pencil
This will create an encrypted copy, that can only be decrypted with the same password. No GPG keys will be needed.
# ls *.gpg
2025-06-24-namedb.tar.gpg
The encrypted TAR archive can be decrypted with GPG and extracted with TAR.
# gpg --decrypt 2025-06-24-namedb.tar.gpg | tar -xf -
Please enter the passphrase for decryption.
Passphrase: Pencil