Incremental backup with 7zip
I have googled and searched, but can't find the command that 7zip command line utility uses for making incremental backups. So can someone please share the command ?
Thanks
btw I found this link: http://wmug.co.uk/wmug/b/sean/archive/2009/03/20/powershell-amp-7zip-incremental-backup-solution.aspx . But it seems to be for differential backups, even though it says incremental.
Should be simple, use this to create and incrementally update the archive:
7zr u -up0q3r2x2y2z1w2 {archive}.7z {path}
This page offers a reference for the update options.
They are translated as follows:
-
p0
- If "File exists in archive, but is not matched with wildcard" then remove the file from the archive. -
q3
- If "File exists in archive, but doesn't exist on disk" then remove the file from the archive and remove it from the filesystem upon extraction. -
r2
- If "File doesn't exist in archive, but exists on disk" then pack the file into the archive. -
x2
- If "File in archive is newer than the file on disk" then "compress file from disk to new archive". -
y2
- If "File in archive is older than the file on disk" then pack the newer file into the archive. -
z1
- If "File in archive is same as the file on disk" then reuse the packed version of the file. -
w2
- If file size is different then pack the modified file into the archive.
Note that only the compression is incremental: that is, 7-Zip will only compress the updates, reusing compressed files that were not updated. The file with the archive will still be re-created by 7-Zip.
If you were to do an incremental backup, you would need to provide 7-zip with the list of the files modified (with -i@fileList
), and you would need to elaborate such list somehow. At the archive.org mirror of removed question Offline incremantal backup via thumbdrive you can find a Unix command line using md5 signatures to create the fileList.
The 7-zip update operation allows to create a secondary archive with the differences (including deleted files) occurring since the base/primary archive. That is properly named a differential backup (as stated in the question itself).
I've found an excellent article on this topic at WPCTips "Differential Backups with 7-zip"(archived). They recommend either using a GUI program (Toucan), or use this recipe for the command line:
7z u {base archive.7z} -u- -"up0q3r2x2y2z0w2!{differential.7z}" {folder to archive}
This is a bit different from the 7zr u -up0q3r2x2y2z1w2 {archive}.7z {path}
proposed by ArtemGr:
-
-u-
tells the main archive should not be modified -
"-up0q3r2x2y2z0w2!{differential.7z}"
specifies the target differential archive, and what action to do for each file for each condition/state: add files which are new or modified in the filesystem, remove files which are only in the 7zip archive, ignore the rest.
Notice that the "!" character will be intercepted bybash
unless it is quoted.
Just in case you are curious about the specifics of that cryptic p0q3r2x2y2z0w2
<state> | State condition
p | File exists in archive, but is not matched with wildcard. Exists, but is not matched
q | File exists in archive, but doesn't exist on disk.
r | File doesn't exist in archive, but exists on disk.
x | File in archive is newer than the file on disk.
y | File in archive is older than the file on disk.
z | File in archive is same as the file on disk
w | Can not be detected what file is newer (times are the same, sizes are different)
<action> | Description
0 | Ignore file (don't create item in new archive for this file)
1 | Copy file (copy from old archive to new)
2 | Compress (compress file from disk to new archive)
3 | Create Anti-item (item that will delete file or directory during extracting). This feature is supported only in 7z format.
You can easily do an incremental backup via changing the direction in time. i.e. you always keep the latest backup as a full copy and keep differential files into the past.
# create the difference step into the past
7z u {base archive.7z} {folder to archive} -mx=9 -u- -up1q1r3x1y1z0w1!{decrement.7z}
# update the Archive to the latest files
7z u {base archive.7z} {folder to archive} -mx=9 -up0q0x2
The base Archive always contains the latest version and via applying the "decrements" step by step you can recreate older Versions. With a little bit scripting you can apply the right numbering to the decremental files.