Simple Linux backups using 7-zip (7z) and tar

Published on February 25th, 2024

In this post I want to look at the use of Linux terminal commands for a simple and effective backup routine on Linux.

We all know we should backup regularly, but it always feels like a pain to perform backups. As a result, most of us have been burnt by the loss of data from time to time, caused by a failure to back our data up.

Since moving to a pure Linux setup based on Ubuntu over a decade ago now, I’ve tried a series of applications and methods for backing data up, but I’ve settled on two particular methods as the most reliable ways of giving me flexible and simple backups.

Why I use 7-zip / tar backups rather than a dedicated solution

  • No proprietary dependencies – Applications dedicated to backups often use proprietary formats. This means you need to be able to access that application to access your backups. Having backups that go back many years means this quickly becomes a compatibility challenge with dedicated backup solutions. ZIP and TAR technologies are general formats used on Linux and Windows systems, providing high availability to extraction tools.
  • Secure offline nature – Simple offline backups do away with the risk of data being permanently online (like a cloud service), and therefore at risk of hacker attacks. Although offline copies can be stolen or destroyed in a fire, good practice of storing sensitive data in an encrypted form, and storing duplicate copies off-site mitigate these risks vastly better than a cloud service in my opinion.
  • Easily encrypted – Just adding the “p” parameter ensures your backup is encrypted.

I’m sure some will say this is more complex than installing a backup application. For me however, it enables simple backup files I can copy to USB sticks or an external drive, and keep a lot of history in a small physical box. The external drive method I use these days makes it easy to place a copy of backups away from your house (e.g. a work locker, or family members house) to protect from fire/theft at home.

The options

There are two options I use in-parallel for these backups:

  • 7z (you can also use the 7za command as it has the same options)
    • Compresses content using 7-zip compression (similar to tar)
    • Supports excluding some directories/files (similar to tar)
    • Supports encryption with a password (tar does not)
  • tar
    • Supports compressing content use g-zip compression (similar to 7z)
    • Supports excluding some directories/files (similar to 7z)
    • Supports capture of ownership/permission detail (7z does not)
    • Supports only choosing files to backup based on creation/modified date (7z does not)

These can be used standalone or together as necessary. I therefore choose which option(s) to use based on:

  • If I want to retain ownership/permission details then I start by using tar to capture them (using the -p parameter).
  • If I want to create a differential backup (a backup that only contains files created or modified since the last backup), I use tar (using the –newer-mtime parameter).
  • If the backup will contain some sensitive info then I will use 7z (part of the 7-zip suite). 7-zip’s -p parameter applies a password encryption.

Bear in-mind that if you choose to choose both (tar the contents, and then use 7z to encrypt it) then 7z is actually encrypting one file (the tarball that tar produced), so a full decryption is needed to view tarball contents. This is slow but I must admit I feel better encrypting all backups and accepting this slowness.


You may need to install 7-zip and tar for this. 7-zip is packaged as p7zip-full on Linux, tar is packaged simply as tar:

sudo apt install p7zip-full
sudo apt install tar

The simple backup commands I use

Pure 7z example

To backup a directory structure purely using 7z, I use commands similar to the below:

sudo 7z a -r -p "/media/james/backupdrive/2024_02_25-homedir-backup.7z" /home/james

Parameters explained

a = Add to archive
r = Recursive folder backup (backup everything within subfolders at the target too)
p = Add password (leave this blank and you’ll be prompted to provide one so it isn’t captured in shell logs)

It’s worth considering adding some “x” parameter arguments, to exclude any directories you don’t need to backup:

sudo 7z a -r -p -x!Music -x!Downloads "/media/james/backupdrive/2024_02_25-homedir-backup.7z" /home/james

Pure tar example

To backup a directory structure purely using tar, I use commands similar to the below:

sudo tar -cvpzf /media/james/backupdrive/2024_02_25-homedir.tar.gz /home/james

Parameters explained

c = Create a new backup file
v = Verbose mode: Forces more logging to be provided to the screen, so you can see which file is being processed etc..
p = Captures the permissions of files backed up
z = Compresses the backup file with “gzip”
f = specifies where to store the backup, 2024_02_25-homedir.tar.gz is the filename used in this example

It’s worth considering adding some “exclude” parameter arguments, to exclude any directories you don’t need to backup:

sudo tar -cvpzf /media/james/backupdrive/2024_02_25-homedir.tar.gz --exclude='james/Music' --exclude='james/Downloads' /home/james

You may also want to consider adding a “newer-mtime” parameter argument, if creating a differential backup. In the example below, only files created or modified since 21st December 2023 are included in the tarball archive tar will produce:

sudo tar -cvpzf /media/james/backupdrive/2024_02_25-homedir.tar.gz --exclude='james/Music' --exclude='james/Downloads' --newer-mtime='2023-12-21' /home/james

Combining 7z and tar

To combine the two in a single command, simply:

  • Remove the “f” parameter from the tar command (as you are not saving the tarball itself)
  • Optional: Remove the “z” parameter from the tar command. I recommend this as 7-zip will compress the data anyway. Doubling the compression is unlikely to save disk space, but increase the time to create and restore backups
  • Pipe (using “|”) the tar command output to the 7z command
  • Add the “si” parameter to the 7z command. This tells 7z to read the piped (stdin) data rather than expect direct file system input
  • Remove the “r” parameter from the 7z command if used. This is because 7z is not accessing the file system, so there is no data for 7z to recursively backup
  • Remove the target file system from the end of the 7z command. This is because 7z is not accessing the file system, but the piped (stdin) data

For example, using the last tar command example above, and piping to 7z for encryption and compression:

sudo tar -cvp --exclude='james/Music' --exclude='james/Downloads' --newer-mtime='2023-12-21' /home/james | 7z a -si -p "/media/james/backupdrive/2024_02_25-homedir-backup.7z"

I hope this proven useful. All the best with your backups!

Adblocker detected

Please bear in mind that ad blockers prevent this website covering its costs.

If you find this site useful, please consider supporting me by whitelisting this site, or making a £1 / $1 donation.