Skip to content

Commit 31a62d8

Browse files
committed
borgbackup: use borg list --json --last 1 for time_since_last_modified
- Use borg list --json --last 1 to get the timestamp of the last archive - Only calculate time_since_last_modified if borg info succeeds (no error, not locked), skipping the value entirely if the repo can't be accessed - Fix missing my declarations for command variables
1 parent 32961a6 commit 31a62d8

1 file changed

Lines changed: 50 additions & 31 deletions

File tree

snmp/borgbackup

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Totaled info is in the hash .totals.
174174
- .totals.locked_for :: Longest time any repo has been locked.
175175
- Type :: seconds
176176
177-
- .totals.time_since_last_modified :: Largest time - mtime for the repo directory
177+
- .totals.time_since_last_modified :: Largest time since last archive was created
178178
- Type :: seconds
179179
180180
- .total.total_chunks :: Total number of checks between all repos.
@@ -205,7 +205,7 @@ Each repo then has it's own hash under .repo .
205205
locked. If it is not locked this is undef.
206206
- Type :: seconds
207207
208-
- .repo.$repo.time_since_last_modified :: time - mtime for the repo directory
208+
- .repo.$repo.time_since_last_modified :: time since last archive was created
209209
- Type :: seconds
210210
211211
- .repo.$repo.total_chunks :: Total number of checks for the repo.
@@ -239,6 +239,7 @@ use MIME::Base64;
239239
use IO::Compress::Gzip qw(gzip $GzipError);
240240
use String::ShellQuote;
241241
use Pod::Usage;
242+
use Time::Local;
242243

243244
our $output_dir = '/var/cache/borgbackup_extend';
244245
my $config_file = '/usr/local/etc/borgbackup_extend.ini';
@@ -519,38 +520,12 @@ foreach my $repo (@repos) {
519520
}
520521
}
521522

522-
if ($process) {
523+
if ($process) {
523524
if ($verbose) {
524525
print STDERR " Repo path: " . $config->{$repo}{repo} . "\n";
525526
}
526527

527-
my $nonce_file = $config->{$repo}{repo} . '/nonce';
528-
my $mtime;
529-
if ( -e $nonce_file ) {
530-
if ($verbose) {
531-
print STDERR " Checking nonce file mtime: $nonce_file\n";
532-
}
533-
( undef, undef, undef, undef, undef, undef, undef, undef, undef, $mtime, undef, undef, undef ) = stat($nonce_file);
534-
} else {
535-
if ($verbose) {
536-
print STDERR " No nonce file found, using repo directory mtime: " . $config->{$repo}{repo} . "\n";
537-
}
538-
( undef, undef, undef, undef, undef, undef, undef, undef, undef, $mtime, undef, undef, undef ) = stat( $config->{$repo}{repo} );
539-
}
540-
541-
my $time_diff = time - $mtime;
542-
$repo_info->{time_since_last_modified} = $time_diff;
543-
if ($verbose) {
544-
print STDERR " Time since last modified: $time_diff seconds\n";
545-
}
546-
547-
# if we don't have a largest time diff or if it is larger than then
548-
# the old one save the time diff
549-
if ( !defined( $to_return->{data}{totals}{time_since_last_modified} )
550-
|| $to_return->{data}{totals}{time_since_last_modified} < $time_diff )
551-
{
552-
$to_return->{data}{totals}{time_since_last_modified} = $time_diff;
553-
}
528+
my $timeout = $config->{$repo}{timeout} || $default_timeout;
554529

555530
if ( defined( $config->{$repo}{passcommand} ) ) {
556531
$ENV{BORG_PASSCOMMAND} = $config->{$repo}{passcommand};
@@ -575,7 +550,6 @@ foreach my $repo (@repos) {
575550
}
576551
}
577552

578-
my $timeout = $config->{$repo}{timeout} || $default_timeout;
579553
if ($verbose) {
580554
print STDERR " Timeout: " . ($timeout > 0 ? $timeout . " seconds" : "disabled") . "\n";
581555
}
@@ -642,6 +616,51 @@ foreach my $repo (@repos) {
642616
}
643617
}
644618

619+
if ( !defined( $repo_info->{error} ) && !$repo_info->{locked} ) {
620+
my $list_cmd = 'borg list ' . shell_quote( $config->{$repo}{repo} ) . ' --json --last 1 2>&1';
621+
if ($timeout > 0) {
622+
$list_cmd = 'timeout ' . $timeout . ' ' . $list_cmd;
623+
}
624+
if ($verbose) {
625+
print STDERR " List Command: $list_cmd\n";
626+
}
627+
my $list_start = time;
628+
my $list_output = `$list_cmd`;
629+
my $list_elapsed = time - $list_start;
630+
if ($verbose) {
631+
print STDERR " List Runtime: $list_elapsed seconds\n";
632+
}
633+
634+
my $list_info;
635+
eval { $list_info = decode_json($list_output); };
636+
if (!$@ && defined($list_info->{archives}) && scalar(@{$list_info->{archives}}) > 0) {
637+
my $archive = $list_info->{archives}[0];
638+
my $archive_time = $archive->{time};
639+
if ($archive_time =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/) {
640+
my $archive_timestamp = timelocal($6, $5, $4, $3, $2-1, $1-1900);
641+
my $time_diff = time - $archive_timestamp;
642+
$repo_info->{time_since_last_modified} = $time_diff;
643+
if ($verbose) {
644+
print STDERR " Last archive: $archive_time\n";
645+
print STDERR " Time since last modified: $time_diff seconds\n";
646+
}
647+
if ( !defined( $to_return->{data}{totals}{time_since_last_modified} )
648+
|| $to_return->{data}{totals}{time_since_last_modified} < $time_diff )
649+
{
650+
$to_return->{data}{totals}{time_since_last_modified} = $time_diff;
651+
}
652+
}
653+
} else {
654+
if ($verbose) {
655+
print STDERR " Could not get archive list, skipping time_since_last_modified\n";
656+
}
657+
}
658+
} else {
659+
if ($verbose) {
660+
print STDERR " Repo has error or is locked, skipping time_since_last_modified\n";
661+
}
662+
}
663+
645664
for my $total (@totals) {
646665
$to_return->{data}{totals}{$total} = $to_return->{data}{totals}{$total} + $repo_info->{$total};
647666
}

0 commit comments

Comments
 (0)