-
Notifications
You must be signed in to change notification settings - Fork 7
/
BaNG
executable file
·177 lines (140 loc) · 5.81 KB
/
BaNG
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
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd qw( abs_path );
use File::Basename;
use Getopt::Long qw( :config no_auto_abbrev );
use lib dirname( abs_path($0) ) . '/lib';
use BaNG::Config;
use BaNG::BackupServer;
use BaNG::Reporting;
use BaNG::TM_rsync;
use BaNG::TM_lts;
use BaNG::BTRFS;
use BaNG::Wipe;
my $version = '3.8-testing';
my $prefix_arg;
my ($host_arg, $group_arg, $nthreads_arg, $bkpmode_arg);
my ($cron, $wipe, $xymononly);
my ($initial, $force, $noreport, $missingonly);
my ($taskid, $taskmeta);
my ($verbose_arg, $vv_arg, $vvv_arg);
my $dryrun_arg;
#################################
# Main
#
parse_command_options();
get_serverconfig($prefix_arg);
cli_args_override_global_config();
print "BaNG run in $serverconfig{bkpmode} mode!\n" if $serverconfig{verbose};
$cron = $ENV{'BaNG_Cron'} || "0";
print "Run as Cron: $cron\n" if $serverconfig{verbose};
if (!-d "/run/BaNG") {
mkdir "/run/BaNG",0770 unless $serverconfig{dryrun};
}
$taskid = create_timeid( $host_arg, $group_arg );
if ( $serverconfig{bkpmode} eq "rsync") {
bangstat_set_taskmeta( $taskid, $host_arg, $group_arg, $cron, $taskmeta) unless ( $wipe || $xymononly );
get_host_config( $host_arg, $group_arg );
foreach my $config ( sort keys %hosts ) {
my $host = $hosts{$config}->{hostname};
my $group = $hosts{$config}->{group};
if ($wipe) {
wipe( $host, $group, $taskid, $force );
} elsif ($xymononly) {
my %RecentBackups = bangstat_recentbackups($host);
xymon_report( $taskid, $host, $group, %RecentBackups );
} else {
next unless pre_queue_checks( $taskid, $host_arg, $group_arg, $host, $group, $initial, $missingonly, $noreport );
queue_rsync_backup( $host_arg, $group_arg, $host, $group, $initial, $missingonly, $noreport, $taskid, $dryrun_arg, $cron );
}
}
if ( !@queue ) {
print "Exit because queue is empty.\n" if ( $serverconfig{verbose} && !$wipe );
exit 0;
}
reorder_queue_by_priority( $taskid, $host_arg, $group_arg );
run_rsync_threads($host_arg, $group_arg, $nthreads_arg, $dryrun_arg, $cron);
logit( $taskid, $host_arg, $group_arg, "Task $taskid finished!" );
exit 0;
} elsif ( $serverconfig{bkpmode} eq "lts" ) {
print "LTS mode work\n" if $serverconfig{verbose};
get_lts_config( $group_arg );
queue_lts_backup( $group_arg, $noreport, $taskid );
if ( !@queue ) {
print "Exit because queue is empty.\n" if $serverconfig{verbose};
exit 0;
}
run_lts_threads($taskid, $group_arg, $nthreads_arg, $dryrun_arg);
exit 0;
}
#################################
# Command line arguments
#
sub parse_command_options {
GetOptions(
'help' => sub { usage('') },
'version' => sub { usage("Current version number: $version") },
'v|verbose' => \$verbose_arg,
'vv' => \$vv_arg,
'vvv' => \$vvv_arg,
'bkpmode=s' => \$bkpmode_arg,
'n|dry-run' => \$dryrun_arg,
'g|group=s' => \$group_arg,
'h|host=s' => \$host_arg,
'p|prefix=s' => \$prefix_arg,
't|threads=i' => \$nthreads_arg,
'w|wipe' => \$wipe,
'initial' => \$initial,
'force' => \$force,
'missingonly' => \$missingonly,
'label=s' => \$taskmeta,
'xymon' => \$xymononly,
'noreport' => \$noreport,
) or usage('Invalid commmand line options.');
usage('You must provide some arguments') unless ( $host_arg || $group_arg );
usage('Number of threads must be positive') if ( $nthreads_arg && $nthreads_arg <= 0 );
usage('Wrong Backup Mode, please use rsync or lts') unless ( !defined $bkpmode_arg || ($bkpmode_arg eq "rsync" || $bkpmode_arg eq "lts" ));
$verbose_arg = 1 if ( $dryrun_arg || $vv_arg || $vvv_arg );
return 1;
}
sub cli_args_override_global_config {
$serverconfig{bkpmode} = $bkpmode_arg if $bkpmode_arg;
$serverconfig{verbose} = $verbose_arg if $verbose_arg;
$serverconfig{dryrun} = $dryrun_arg if $dryrun_arg;
$serverconfig{verboselevel} = 2 if $vv_arg;
$serverconfig{verboselevel} = 3 if $vvv_arg;
return 1;
}
sub usage {
my ($message) = @_;
if ( defined $message && length $message ) {
$message .= "\n"
unless $message =~ /\n$/;
}
my $command = $0;
$command =~ s#^.*/##;
print <<"EOF";
$message
Usage Examples:
$command -h <host> -g <group> # back up given host and group
$command -h <host> # back up all groups of given host
$command -g <group> # back up all hosts of given group
$command --help # show this help message
$command --version # show version number and help
Optional Arguments:
-t <nr> # number of threads, default: 1
-p <path> # override path to folder containing etc/
-w | --wipe # wipe the backup
-w --force # forcing wipe the backup (override auto_wipe_limit)
--bkpmode <rsync|lts> # select backup mode (rsync / lts), default: rsync
--initial # create nonexisting backup targets
--missingonly # backup only hosts that were not backed up recently
--label <string> # set a label for this task
--xymon # send only xymon report
--noreport # do not send any report
-v | -vv | -vvv # verbose mode to include debugging messages of level 1-3
-n # dry-run without making changes (implies verbose)
EOF
exit 0;
}