A cute little PowerShell backup script that uses 7-Zip to back up a directory or file, complete with atomic overwrite (new backup will overwrite the previous one, but will not delete it before the new one is produced) and Event Viewer logging. I only tested this on Windows, so it will spit errors about failing to register the Event Viewer log at best and won't perform its intended purpose at worst.
powershell.exe Backup-Location -Source <source> -Destination <destination> [<other parameters>]
Will grab the directory or file at <source>
and create an archive at <destination>
that overwrites it. The process is as follows:
- The file at
<destination>
with a.tmp
suffix is checked. If one exists, it's renamed to just<destination>
and a warning with event ID 4 is emitted to the event log that notes down the presence of an unsuccessful previous backup.- If the move fails, an error with event ID 2 is logged and the backup is aborted.
- The 7-Zip command line tool is invoked to create a new backup at
<destination>.tmp
. - If that succeeds, the file at
<destination>
, if any, is deleted (which logs an information event with ID 1), and<destination.tmp>
is renamed to<destination>
.- If deletion fails, an error with event ID 3 is logged and the backup is aborted at this stage. The new backup is then available at
<destination>.tmp
, and<destination>
has the old backup. - If the move fails, an error with event ID 2 is logged similarly to step 1.
- If deletion fails, an error with event ID 3 is logged and the backup is aborted at this stage. The new backup is then available at
- If everything succeeds, an information event with ID 0 is logged.
Before all of this, the script checks whether the default (or the specified custom) event log and source exist, and if not, attempts to register them. That requires administrative privileges. Run the script in a shell with administrative privileges with the -OnlyRegisterLog
switch to register those, although the script will still attempt registration without that switch and will display the error and continue if that fails.
-Source
— specifies the source file or directory to be backed up. Mandatory unless-OnlyRegisterLog
is specified.-Destination
— specifies the path and filename of the final backup archive. Mandatory unless-OnlyRegisterLog
is specified.-7ZipPath
— overrides the path to the7z
command. Optional, defaults to7z
, which looks inPATH
.-CompressionMethod
— overrides the compression method. The default is to create a.7z
archive (regardless of the extension) and to use multithreading. Optional, defaults to-t7z -mmt=on
.-AdditionalFlags
— empty by default. Use to specify additional flags on top of-CompressionMethod
, which overrides the defaults. Optional, defaults to nothing.- -
EventLogName
— overrides the name of the event log. Changing requires re-registration. Optional, defaults toBackup
. - -
EventLogSource
— overrides the name of the event source. You could use this to distinguish different backups. Changing requires re-registration. Optional, defaults toBackup-Location
. -OnlyRegisterLog
— if passed, registers the event log and source and exits without performing a backup. Optional, disabled by default.
Snatch this without credit if you want, I couldn't care less. Just don't tell people you made this, you'd look really stupid, and writing 122 whole lines of PowerShell code is not exactly a huge achievement, especially if you're not the one who did it. For legal purposes, consider this in the public domain or under the Unlicense or something.
:ferrisOwO: