Skip to content

Instantly share code, notes, and snippets.

@stil
Created March 2, 2014 00:35
Show Gist options
  • Save stil/9299946 to your computer and use it in GitHub Desktop.
Save stil/9299946 to your computer and use it in GitHub Desktop.
Differential backup with 7zip and Powershell
# CONFIGURATION
$dirToBackup = "C:\Users\John" # path to directory we back up (no following backslash)
$outputDir = "E:\bak" # path directory we store our backups (no following backslash)
$params = '-t7z', '-r', '-ms=off', '-mx1'
# THE SCRIPT
$fullBackup = $outputDir + "\full.7z"
if (Test-Path ($fullBackup)) { # Let's check whether full backup exists
Write-Host "Full backup already exists"
$args = ,'u' + $params
$args += '-u-', "-up0q3r2x2y2z0w2!`"$($outputDir)\diff-$(Get-Date -format "yyyyMMdd-HHmmss").7z`""
$args += $fullBackup, $dirToBackup
} else {
$args = , ('a') + $params + $fullBackup + $dirToBackup
}
& 7z $args
@mkomputes
Copy link

Thanks for the inspiration!

Note: Having $args in an array is essential to having the !difffile passed to 7z with quotes around it. Otherwise the parser will strip them off and 7z will throw an error.

@fn5
Copy link

fn5 commented Oct 28, 2022

<#
https://sevenzip.osdn.jp/chm/cmdline/switches/method.htm

-t7z - use a .7z archive
-r - recurse subdirectories
-ms=off - disable solid mode for quicker compression
-mx1 - level 1 compression LZMA2	64 KB	32	HC4	BCJ	Fastest compressing

#>

<#
-u update options
    p0 - if exists in archive, ignore
    q3 - if exists in archive BUT NOT DISK, item that will note deletion
    r2 - if NOT IN ARCHIVE but on disk, compress
    x2 - if NEWER in archive, compress
    y2 - if NEWER in disk, compress
    z0 - if same, ignore
    w2 - if cannot decide which is newer, compress
    !

#> 

@f1mishutka
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment