Make your backups with 7-zip on Windows


This time, we are not talking about Python or databases, but about backups, a very important aspect that does not get all the attention it deserves, until you have a problem, and then, it is when you take action. What you are about to read is how to automate the task of making your backups using the free 7-zip compressor, under Windows systems.

If this entry makes your life easier and saves you from losing your information, I encourage you to make a donation 💰 to keep improving it.

First step: verify that 7-zip can be used from console

the first task is install 7z, of course, but next one is to verify that you can use 7-zip from a MS-DOS (Start / Execute / cmd).

7z desde consola
7zip from console

If you get an error, then you must set the environment variables and add the directory where you have 7-zip installed (in my case, in c:\Program Files). If you don’t know how to do it, you can follow these instructions or these.

Preparing the 7-zip command for the console

We can now access 7-zip from the MS-DOS console, so to test that it works, we will follow the instructions that you can find in the 7-zip help. Imagine that you are in the root of a drive, and you want to compress the GAE directory, and create gae.7z. The command is as follows:

7z a gae.7z gae

Let’s break the command into pieces:

  • 7z, is the call to 7-zip, that is, the executable
  • a, is the mandatory parameter indicating the option to perform (others are e for extract, d for delete, or l for list. You can see the list of options in the help)
  • gae.7z, is the name of the compressed file to generate
  • gae, is the name of the directory or file to be compressed (in my case, it’s a directory)

Here you have it:

7z gae
7zip comand

with the job done:

7z gae
Directory compressed by using 7-zip

And we see that the created file gae.7z, from the GUI version of 7-zip, contains the expected contents:

7z GUI
7z GUI

Preparing a text file for doing with multiple dirs and files

To include in a compressed file more than one directory, it is recommended to create a text file with the paths, where each one must be separated by carriage return. In my case, I want to include several directories, so (I follow the recommendation of the help) I create a file with the paths (if the path has blank spaces, it is important to put the path between double quotes ). We save the file as backup_list.txt:

listado directorios para Backup
List of files and dirs to backup

Let’s try the following command:

7z a backup.7z @backup_lista.txt

And you can see that everything has done as expected:

7z Backup con listado
7zip using a text file with several paths

And we load into the 7-zip GUI to check that again, all the intended directories have been stored in backup.7z:

7z gui
7z GUI

Scheduling everything in a .bat process

Surely, you will want to perform all this operation with more than one directory, and separating in several files. Well, the best thing to do is to program a backup.bat file, which runs under MS-DOS, where we include the command to execute.

preparando BAT
Preparing the .bat file

Now, we can execute the process either by clicking on the backup.bat file, or by launching it from the MS-DOS console, getting our backup of the important directories. If you have the file in a specific location, you can put it followed by the @”l:\path with spaces\backup_list.txt”.

And finally, if you want to customize the path so that each day it creates a file for you, you must extend your batch process like this:

SET fecha=_%DATE:~6,4%%DATE:~3,2%%DATE:~0,2% 
7z a backup_v%fecha%.7z @"l:\backup\lista_backup_v.txt"

Now, every time you run the process, a file will be generated every day, allowing you to store copies for several days!

Execution of backups at login

One of the options to further automate backups by using 7-zip command is to create a link and include it in the Start menu, so that when you log in, everything in that directory is executed, and therefore, the script will also be launched. This solution is one of the ones I use most frequently.

And with this, now you will not have problems with your most sensitive data and files, and you will be a little safer with your backups using 7-zip!