This script allows you to create backups of your Windows Subsystem for Linux (WSL) home directory to a Windows location with custom filter options, deletion methods, and backup storage management. It uses the rjrssync utility for faster file synchronization.
Important: Although this script uses rjrssync, it can not perform backups to a remote location. It is only meant to be used for local backups. As a workaround you can use rjrssync to sync the backup output folder to a remote location.
- Back up your WSL home directory from a specific distribution and user.
- Specify output folders and regex filters for customization.
- Manage previous backups by compressing, deleting oldest or random backups, based on settings.
- Utilizes
rjrssync
for improved performance even using/mnt/
and to preserve symlinks properly
- Windows Subsystem for Linux (WSL) installed and configured with a desired Linux distribution.
- PowerShell 5.0 or later.
- rjrssync installed and sourced in your shell (https://github.com/Robert-Hughes/rjrssync).
- Copy the
backup_wsl.ps1
script to a desired location on your Windows machine. - Open PowerShell and navigate to the script location.
- Run the script and provide the required parameters. Examples:
.\backup_wsl.ps1 Ubuntu user c:\backups +projects.* 1 10 deleteRandom
\backup_wsl.ps1 -DistroName Ubuntu -UserName user -OutputFolder c:\backups -RegexFilters "+projects.*" -SavePreviousBackup $true -MaxBackupCount 10 -IfBackupFull deleteOlder
Parameters:
-
-DistroName
: The name of the WSL distribution you want to create a backup of. -
-UserName
: The user whose home directory you want to back up. -
-OutputFolder
: The folder where the backup will be stored. -
-RegexFilters
: Regular expression filters for files and folders you want to include or exclude from the backup. Each filter is a '+' or '-' character followed by a regular expression. The '+'/'-' indicates if this filter includes (+) or excludes (-) matching entries. Separate multiple filters with a semicolon. If the first filter is an include (+), then only those entries matching this filter will be synced. If the first filter is an exclude (-), then entries matching this filter will not be synced. Further filters can then override this decision.The regexes are matched against a 'normalized' path relative to the root path of the source/dest:
-
Forward slashes are always used as directory separators, even on Windows platforms
-
There are never any trailing slashes
-
Matches are done against the entire normalized path - a substring match is not sufficient
-
'+.*\.txt;-subfolder'
Syncs all files with the extension .txt, but not insidesubfolder
-
-
-SavePreviousBackup
: Whether to save previous backups by compressing them. Set$true
to save,$false
to discard previous backups. -
-maxBackupCount
: Maximum number of compressed backups to be stored. -
-ifBackupFull
: What to do when the backup folder is full. Valid values are "deleteOldest", "deleteRandom", and "doNothing". Note: When using the "deleteRandom" option, the script will never delete the oldest backup.
- rjrssync is known to be faster when used on remote connections, but it will still be more performant than alternative tools using the usual mounting methods like
\mnt\
. - Ensure that rjrssync is installed and sourced within your shell before running the script.