A simple utility to delete paths from archives to save space in Borg repositories.
Warning
While I have used it a few times on one of my repositories it still should be considered untested. There is NO guarantee that it's bug-free and should be treated like that. Be careful, take measures, keep multiple backups.
I managed to backup several hundred GBs of podman layers over months... (I should monitor my backups more closely..) So I needed a tool to delete the whole podman directory from my archives, while also ensuring I deleted only the files I expected to be deleted.
The workflow of the script is:
- Retrieve all archives in the specified repository.
- Keep only the archives matching the glob
- Present the user with a list of archives which matched
- Loop through each archive
a. Use
borg list
to get all files matching the exclude pattern b. Present the user with the count and size of files (skip if 0) c. Ask the user if they want to recreate the archive excluding the files listed earlier- Using
p
the user can preview the files. - Using
y
the user can accept and the archive will be recreated without those files (usingborg recreate
) - Using
a
the user can accept this and all subsequent recreation requests (ie. it will go through all the matched archvies and recreate them)
- Using
[BORG=<borg-executable-path>] ./borg-delete-paths.sh <repository> [archive_glob] <exclude_pattern1> [<exclude_pattern2> ...]
Description:
Deletes specified paths from one or more Borg archives using exclude patterns.
Arguments:
BORG Optional. Path to a Borg executable (default: "borg").
<repository> Target Borg repository. Use "::" for the default repository.
[archive_glob] Optional. Glob pattern to match archive names (e.g., "MyArchive-*").
If omitted, all archives in the repository will be considered.
NOTE: This option is detected by checking for the exitence of a wildcard in the 2nd argument.
Horrible I know, maybe a future version uses GNU options.
<exclude_patternN> One or more path patterns to delete from the matched archives.
Example:
BORG=borg-job-safe ./borg-delete-paths.sh :: Archy-safe-2025* persistent/safe/home/sherex/{.cache,containers}
./borg-delete-paths.sh :: Archy-safe-2025-01-01* persistent/safe/home/sherex/.cache persistent/safe/home/sherex/.cargo