App::BorgRestore − Restore paths from borg backups


use App::BorgRestore;
my $app = App::BorgRestore−>new();
# Update the cache (call after creating/removing backups)
# Restore a path from a backup that is at least 5 days old. Optionally
# restore it to a different directory than the original.
# Look at the implementation of this method if you want to know how the
# other parts of this module work together.
$app−>restore_simple($path, "5days", $optional_destination_directory);


App::BorgRestore is a restoration helper for borg.

It maintains a cache of borg backup contents (path and latest modification time) and allows to quickly look up backups that contain a path. It further supports restoring a path from an archive. The archive to be used can also be automatically determined based on the age of the path.

The cache has to be updated regularly, ideally after creating or removing backups.

borg− is a wrapper around this class that allows for simple CLI usage.

This package uses Log::Any for logging.




Returns a new instance. The following dependencies can be injected:

borg (optional)

This object is used to interact with the borg repository of the system. Defaults to App::BorgRestore::Borg

db (optional)

This object is used to store the extracted data (the cache). Defaults to App::BorgRestore::DB


Same as "new" except that this does not initialize unset dependencies with their default values. This is probably only useful for tests.

Public Methods

my $abs_path = $app−>resolve_relative_path($path);

Returns an absolute path for a given path.


my $path_in_backup = $app−>map_path_to_backup_path($abs_path);

Maps an absolute path from the system to the path that needs to be looked up in / extracted from the backup using @backup_prefixes from App::BorgRestore::Settings.

Returns the mapped path (string).


my $archives = $app−>find_archives($path);

Returns an arrayref of archives (hash with "modification_time" and "archive") from the database that contain a path. Duplicates are filtered based on the modification time of the path in the archives.


my $archives = $app−>get_all_archives();

Returns an arrayref of archives (hash with "modification_time" and "archive") from borg directly. This does not require the database to be populated. Instead it just fetches a list of archives from borg at runtime and returns it.

The returned data structure is the same as that returned by "find_archives".


if ($app−>cache_contains_data()) { ... }

Returns 1 if the cache contains any archive data, 0 otherwise.


my $archive = $app−>select_archive_timespec($archives, $timespec);

Returns one archive from $archives that is older than the value of $timespec.

timespec is a string of the form "<number><unit>" with unit being one of the following: s (seconds), min (minutes), h (hours), d (days), m (months = 31 days), y (year). Example: "5.5d"


$app−>restore($backup_path, $archive, $destination);

Restore a backup path (returned by "map_path_to_backup_path") from an archive (returned by "find_archives" or "get_all_archives") to a destination directory.

If the destination path ("$destination/$last_elem_of_backup_path") exists, it is removed before beginning extraction from the backup.

Warning: This method temporarily modifies the current working directory of the process during method execution since this is required by "`borg extract`".


$app−>restore_simple($path, $timespec, $destination);

Restores a $path based on a $timespec to an optional $destination. If $destination is not specified, it is set to the parent directory of $path so that $path is restored to its original place.

Refer to "select_archive_timespec" for an explanation of the $timespec variable.


my $paths = $app−>search_path($pattern)

Returns a arrayref of paths that match the pattern. The pattern is matched as an sqlite LIKE pattern. If no % occurs in the pattern, the patterns is automatically wrapped between two % so it may match anywhere in the path.


my $items = $app−>get_missing_items($have, $want);

Returns an arrayref of items that are part of $want, but not of $have.



Updates the database used by e.g. "find_archives".


Copyright (C) 2016−2018 Florian Pritz <>

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <>.

See LICENSE for the full license text.


Florian Pritz <>