summaryrefslogtreecommitdiffstats
path: root/lib/App/BorgRestore/Settings.pm
blob: bc4fbd02d9418515f4843a54ae847ce946c2c32e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package App::BorgRestore::Settings;
use v5.14;
use strictures 2;

use App::BorgRestore::Helper;

use autodie;
use Function::Parameters;
use Sys::Hostname;

=encoding utf-8

=head1 NAME

App::BorgRestore::Settings - Settings package

=head1 DESCRIPTION

App::BorgRestore::Settings searches for configuration files in the following locations in
order. The first file found will be used, any later ones are ignored. If no
files are found, defaults are used.

=over

=item * $XDG_CONFIG_HOME/borg-restore.cfg

=item * /etc/borg-restore.cfg

=back

=head2 Configuration Options

You can set the following options in the config file.

Note that the configuration file is parsed as a perl script. Thus you can also
use any features available in perl itself.

Also note that it is important that the last statement of the file is positive
because it is used to check that running the config went well. You can simply
use "1;" on the last line as shown in the example config.

=over

=item C<$borg_repo>

This specifies the URL to the borg repo as used in other borg commands. If you
use the $BORG_REPO environment variable set this to an empty string. Default:
"backup:borg-".hostname;

=item C<$backup_prefix>

This specifies that only archives with the prefix should be considered. For
example, if you back up multiple things (file system and database) into
differntly named archives (fs-* and db-*), this can be used to only consider
file system archives to keep the database size small. In the example you'd set
the setting to "fs-". An empty string considers all archives. Default: ""

=item C<$cache_path_base>

This defaults to "C<$XDG_CACHE_HOME>/borg-restore.pl". It contains the lookup database.

=item C<@backup_prefixes>

This is an array of prefixes that need to be added or removed when looking up a
file in the backup archives. If you use filesystem snapshots and the snapshot
for /home is located at /mnt/snapshots/home, you have to add the following:

# In the backup archives, /home has the path /mnt/snapshots/home
{regex => "^/home/", replacement => "mnt/snapshots/home/"},

The regex must always include the leading slash and it is suggested to include
a tailing slash as well to prevent clashes with directories that start with the
same string. The first regex that matches for a given file is used. This
setting only affects lookups, it does not affect the creation of the database
with --update-database.

If you create a backup of /home/user only, you will need to use the following:

# In the backup archives, /home/user/foo has the path foo
{regex => "^/home/user", replacement => ""},

=item C<$sqlite_cache_size>

Default: 102400

The size of the in-memory cache of sqlite in kibibytes. Increasing this may
reduce disk IO and improve performance on certain systems when updating the
cache.

=item C<$prepare_data_in_memory>

Default: 0

When new archives are added to the cache, the modification time of each parent
directory for a file's path are updated. If this setting is set to 1, these
updates are done in memory before data is written to the database. If it is set
to 0, any changes are written directly to the database. Many values are updated
multiple time, thus writing directly to the database is slower, but preparing
the data in memory may require a substaintial amount of memory.

New in version 3.2.0. Deprecated in v3.2.0 for future removal possibly in v4.0.0.

=back

=head2 Example Configuration

 $borg_repo = "/path/to/repo";
 $backup_prefix = "";
 $cache_path_base = "/mnt/somewhere/borg-restore.pl-cache";
 @backup_prefixes = (
 	{regex => "^/home/", replacement => "mnt/snapshots/home/"},
 	# /boot is not snapshotted
 	{regex => "^/boot/", replacement => "boot"},
 	{regex => "^/", replacement => "mnt/snapshots/root/"},
 );
 $sqlite_cache_size = 2097152;
 $prepare_data_in_memory = 0;

 1; #ensure positive return value

=head1 LICENSE

Copyright (C) 2016-2018  Florian Pritz E<lt>bluewind@xinu.atE<gt>

Licensed under the GNU General Public License version 3 or later.
See LICENSE for the full license text.

=cut

method new($class: $deps = {}) {
	return $class->new_no_defaults($deps);
}

our $borg_repo = "backup:borg-".hostname;
our $cache_path_base;
our @backup_prefixes = (
	{regex => "^/", replacement => ""},
);
our $sqlite_cache_size = 102400;
our $prepare_data_in_memory = 0;
our $backup_prefix = "";

method new_no_defaults($class: $deps = {}) {
	my $self = {};
	bless $self, $class;
	$self->{deps} = $deps;


	if (defined $ENV{XDG_CACHE_HOME} or defined $ENV{HOME}) {
		$cache_path_base = sprintf("%s/borg-restore.pl", $ENV{XDG_CACHE_HOME} // $ENV{HOME} ."/.cache");
	}

	load_config_files();

	if (not defined $cache_path_base) {
		die "Error: \$cache_path_base is not defined. This is most likely because the\n"
		."environment variables \$HOME and \$XDG_CACHE_HOME are not set. Consider setting\n"
		."the path in the config file or ensure that the variables are set.";
	}

	$cache_path_base = App::BorgRestore::Helper::untaint($cache_path_base, qr/.*/);

	return $self;
}

method get_config() {
	return {
		borg => {
			repo => $borg_repo,
			backup_prefix => $backup_prefix,
			path_prefixes => [@backup_prefixes],
		},
		cache => {
			base_path => $cache_path_base,
			database_path => "$cache_path_base/v3/archives.db",
			prepare_data_in_memory => $prepare_data_in_memory,
			sqlite_memory_cache_size => $sqlite_cache_size,
		}
	};
}

fun load_config_files() {
	my @configfiles;

	if (defined $ENV{XDG_CONFIG_HOME} or defined $ENV{HOME}) {
		push @configfiles, sprintf("%s/borg-restore.cfg", $ENV{XDG_CONFIG_HOME} // $ENV{HOME}."/.config");
	}
	push @configfiles, "/etc/borg-restore.cfg";

	for my $configfile (@configfiles) {
		$configfile = App::BorgRestore::Helper::untaint($configfile, qr/.*/);
		if (-e $configfile) {
			unless (my $return = do $configfile) {
				die "couldn't parse $configfile: $@" if $@;
				die "couldn't do $configfile: $!"    unless defined $return;
				die "couldn't run $configfile"       unless $return;
			}
		}
	}
}

method get_cache_base_dir_path($path) {
	return "$cache_path_base/$path";
}

1;

__END__