Script: Backup/sync files with 7-Zip CLI

Windows | Linux | 7-Zip Command Line Interface

1. Foreword / Setting / Requirements

We want a quick and tiny script to initially backup our files secondarly to update our archive for example on a daily basis. We want to modify our archive only in case a file/folder has been changed.

As of today virtualization is in fashion because VM’s are quite easy to handle and they support nice things like snapshots and an easy migration for instance. So the possibility is quite high we have already a machine which is running an hypervisor somewhere at home or in the office. Therefore this cheatsheet is dealing with a VM.

Mandatory for this tutorial:
• 7-Zip CLI executable (7za.exe)
• basic knowledge about Windows’ Command Line

 

2. The Script (line)

Short and beautiful. It’s so short I wouldn’t necessarily call it even a script 😉

path/to/7za.exe a -up0q0r2x1y1z1w2 -mx1 "path/to/archive.7z" "path/to/source"

 

3. Explenation

• path/to/7za.exe: Your place where 7za.exe is stored.
• a: Command to ADD files/folders to an archive.
• -up0q0r2x1y1z1w2: Options which actually enable 7-Zip to work as expected. This holy string consists of:
• -u parameter UDPATE
• p0 condition P: What to do if file exists in archive, but is not matched with wildcard? 0 = Ignore file (don’t create item in new archive for this file).
• q0 condition Q: NOT in source, but IN archive > 0 = Ignore file.
• r2 condition R: IN source, but NOT in archive > add and compress file.
• x1 condition X: in source OLDER, than in archive > copy file from old to new archive.
• y1 condition Y: in source NEWER, than in archive > copy file from old to new archive.
• z1 condition Z: file is identical > copy file from old to new archive.
• w2 condition W: same time but different size > add and compress file.

By the way: There is a very good documentation for 7-Zip out there.

-mx1 I prefer to determine the level of compression. It’s saving some time for data which is already highly compressed like movies, pictures and music.
Possible Values: 0 (none) | 1 (fast) | 3 (fast) | 5 (normal) | 7 (maximum) | 9 (ultra). Newer benchmarks are showing the time/compression ratio is best at level 3.

“path/to/archive.7z” Where the archive has to be stored. If your path contains any spaces, be sure to use quotation marks.

“path/to/source” Your file/folder which should be copied (recursively). Use quotation marks if there are spaces inside your path.

4. Et voila!