-
Notifications
You must be signed in to change notification settings - Fork 7
/
BaNG-Trigger
executable file
·78 lines (64 loc) · 2.03 KB
/
BaNG-Trigger
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
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd qw( abs_path );
use File::Basename;
use File::Find::Rule;
use POSIX qw( strftime );
use lib dirname( abs_path($0) ) . '/lib';
use BaNG::Config;
my $dryrun = 0;
my $prefix_arg;
get_serverconfig($prefix_arg);
my @triggerfiles = _find_triggers( "*\_*\.trigger", "$serverconfig{path_trigger}");
foreach my $triggerfile (@triggerfiles) {
my ( $hostname, $groupname ) = _split_triggername($triggerfile);
_logit("found trigger file for Host: $hostname Group: $groupname");
_logit("delete triggerfile $serverconfig{path_trigger}/$triggerfile");
unlink("$serverconfig{path_trigger}/$triggerfile") unless $dryrun;
my $bang_cmd = "$prefix/BaNG -h $hostname -g $groupname";
_logit("start backup for Host: $hostname Group: $groupname, using following command: $bang_cmd");
$bang_cmd = "echo $bang_cmd" if $dryrun;
system("$bang_cmd");
};
exit 0;
###########################################
#
# Functions
#
sub _find_triggers {
my ( $query, $searchpath ) = @_;
my @files;
my $ffr_obj = File::Find::Rule->file()
->name($query)
->relative
->maxdepth(1)
->start($searchpath);
while ( my $file = $ffr_obj->match() ) {
push( @files, $file );
}
return @files;
}
sub _split_triggername {
my ($triggername) = @_;
my ($a, $b);
if ($triggername =~ /_/ ) {
( $a, $b ) = $triggername =~ /^([\w\d\.-]+)_([\w\d-]+)\.trigger/;
return ( $a, $b );
} else {
( $a ) = $triggername =~ /^([\w\d\.-]+)\.trigger/;
return ( $a );
}
}
sub _logit {
my ( $msg ) = @_;
my $timestamp = strftime '%b %d %H:%M:%S', localtime;
my $logmonth = strftime '%Y-%m', localtime;
my $logfile = "$serverconfig{path_logs}/Trigger.log";
my $logmessage = "$timestamp BaNG-Trigger : $msg";
$logmessage .= "\n" unless ( $logmessage =~ m/\n$/ );
open my $log, '>>', $logfile or print "ERROR opening logfile $logfile: $!\n";
print {$log} $logmessage;
close $log;
return 1;
}