-
Notifications
You must be signed in to change notification settings - Fork 7
/
BaNGadm
executable file
·354 lines (288 loc) · 10.9 KB
/
BaNGadm
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/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 POSIX qw( strftime );
use YAML::Tiny;
use lib dirname( abs_path($0) ) . '/lib';
use BaNG::Config;
use BaNG::BackupServer;
use BaNG::Wipe;
use BaNG::Reporting;
my $version = '0.1';
my $add_arg;
my $delete_arg;
my $failed_arg;
my $cleanup_arg;
my $initial_arg;
my $dump_arg;
my $tidy_arg;
my $croncreate;
my $croncheck;
my $setprop;
my $verbose_arg;
my $vvv_arg,
my $dryrun_arg;
my $group_arg;
my $host_arg;
my $prefix_arg;
my $showgroups;
#################################
# Main
#
parse_command_options();
get_serverconfig($prefix_arg);
cli_args_override_global_config();
if ($croncheck) {
my $diff = status_cron();
print "$diff\n";
exit 0;
}
if ($croncreate) {
my $cron = generate_cron();
if ( $serverconfig{dryrun} || $serverconfig{cron_type} == 1 ) {
print "$cron\n";
} else {
print "write cronjob file to /etc/cron.d/BaNG\n";
open CRON, '>', '/etc/cron.d/BaNG'
or die('Could not open /etc/cron.d/BaNG');
print CRON $cron;
close CRON;
chmod 0644,'/etc/cron.d/BaNG';
}
exit 0;
}
if ($showgroups) {
get_group_config();
print "All available Backup Groups:\n";
print "----------------------------\n";
foreach my $group ( sort keys %groups ) {
print "$group \n";
}
exit 0;
}
if ( $initial_arg ) {
print "Initialize target structure for $host_arg $group_arg\n";
get_host_config( $host_arg, $group_arg );
return unless check_target_exists( $host_arg, $group_arg, 0, 1 );
print "Target structure for $host_arg\_$group_arg is ready for use!\n";
exit 0;
}
if ($failed_arg) {
my @failed_folders;
my $host = $host_arg || '*';
my $group = $group_arg || '*';
get_host_config( $host, $group );
foreach my $hostgroup (sort keys %hosts ) {
unless ( $hosts{$hostgroup}->{hostconfig}->{BKP_TARGET_HOST} ne $servername ) {
print "Check $hostgroup for failed backups\n" if $serverconfig{verbose};
@failed_folders = get_backup_folders($hosts{$hostgroup}->{hostname},$hosts{$hostgroup}->{group}, 1);
if ( scalar( @failed_folders ) > 0 ) {
print "\nFound following folders for $hosts{$hostgroup}->{hostname} - $hosts{$hostgroup}->{group}\n";
foreach my $failed_folder ( sort @failed_folders ) {
print "\t$failed_folder";
}
print "\n";
if ( $cleanup_arg ) {
print "Purge failed backup folders for $hostgroup\n" if $serverconfig{verbose};
wipe_worker( $hosts{$hostgroup}->{hostname}, $hosts{$hostgroup}->{group}, 0 , @failed_folders );
}
}
}
}
exit 0;
}
if ($setprop) {
get_host_config( $host_arg, $group_arg );
my @backup_folders = get_backup_folders( $host_arg, $group_arg, 2);
if ($setprop eq "list") {
print "Snapshot property (rw/ro) of $host_arg - $group_arg\n";
foreach my $folder (@backup_folders) {
chomp $folder;
my $result = `$serverconfig{path_btrfs} property get -ts $folder`;
chomp $result;
if ( $result =~ /^ro=true$/ ) {
$result = "Read-Only";
} else {
$result = "=> Read-Write !!!";
}
print "$folder $result\n";
}
} elsif ($setprop eq "ro") {
print "Set snapshots property of $host_arg - $group_arg to Read-Only\n";
foreach my $folder (@backup_folders) {
chomp $folder;
my $result = `$serverconfig{path_btrfs} property set -ts $folder ro true`;
print "set $folder to Read-Only\n";
}
} elsif ($setprop eq "rw") {
print "Set snapshots property of $host_arg - $group_arg to Read-Write\n";
foreach my $folder (@backup_folders) {
chomp $folder;
my $result = `$serverconfig{path_btrfs} property set -ts $folder ro false`;
print "set $folder to Read-Write\n";
}
}
exit 0;
}
if ($add_arg) {
my $timestamp = strftime( '%Y/%m/%d %H:%M:%S', localtime );
my $createdby = 'root with CLI';
my $configtype;
if ( $host_arg && $group_arg ) {
$configtype = 'host';
} elsif ( !$host_arg && $group_arg ) {
$host_arg = '0';
$configtype = 'group';
}
my $settings;
$settings->{'COMMENT'} = "Created by $createdby at $timestamp";
my ( $return_code, $return_msg ) = write_config( $configtype, 'add', $host_arg, $group_arg, $settings );
if ( $return_code eq '1' ) {
print "Configfile $return_msg created\n";
get_host_config( $host_arg, $group_arg );
return unless check_target_exists( $host_arg, $group_arg, 0, 1) ;
print "Target structure for $host_arg\_$group_arg is ready for use!\n";
} else {
print "$return_msg\n";
}
exit 0;
}
if ($delete_arg) {
my $configtype;
my $file;
if ( $host_arg && $group_arg ) {
$configtype = 'host';
$file = $host_arg . '_' . "$group_arg.yaml";
} elsif ( !$host_arg && $group_arg ) {
$configtype = 'group';
$file = "$group_arg.yaml";
}
my ( $return_code, $return_msg ) = delete_config( $configtype, $file );
print "$return_msg\n";
exit 0;
}
if ($dump_arg) {
my $timestamp = strftime( '%Y%m%d_%H%M%S', localtime );
my $yaml = YAML::Tiny->read( $serverconfig{config_bangstat} );
my $DBhostname = $yaml->[0]{DBhostname};
my $DBusername = $yaml->[0]{DBusername};
my $DBdatabase = $yaml->[0]{DBdatabase};
my $DBpassword = $yaml->[0]{DBpassword};
my $mysqldump = "mysqldump -h $DBhostname -u $DBusername -p$DBpassword $DBdatabase statistic";
if ( !-d $serverconfig{path_db_dumps}) {
print "Create missing folder $serverconfig{path_db_dumps}\n";
system("mkdir -p $serverconfig{path_db_dumps}") unless ($serverconfig{dryrun});
}
my $dumpfile = "$serverconfig{path_db_dumps}/${DBdatabase}_statistic_$timestamp.sql";
print "Create dump of table statistic to $dumpfile\n";
system("$mysqldump > $dumpfile") unless ($serverconfig{dryrun});
print "Dump done.\n";
}
if ($tidy_arg) {
my $conn = bangstat_db_connect( $serverconfig{config_bangstat} );
return '' unless $conn;
my $sth;
my $rows = 0;
print "Transfer records older than 100 days to the archive.\n";
$sth = $bangstat_dbh->prepare("
INSERT INTO statistic_archive
SELECT *
FROM statistic
WHERE Start < date_sub(now(), interval 100 day);
");
$sth->execute();
$rows = $sth->rows;
print "Total transfered records: $rows\n";
$sth->finish();
print "Delete records older than 100 days from table statistic.\n";
$sth = $bangstat_dbh->prepare("
DELETE
FROM statistic
WHERE Start < date_sub(now(), interval 100 day);
");
$sth->execute();
$rows = $sth->rows;
print "Total deleted records: $rows\n";
$sth->finish();
print "tidy up done.\n";
}
#################################
# Command line arguments
#
sub parse_command_options {
GetOptions(
'help' => sub { usage('') },
'version' => sub { usage("Current version number: $version") },
'v|verbose' => \$verbose_arg,
'vvv' => \$vvv_arg,
'n|dry-run' => \$dryrun_arg,
'add' => \$add_arg,
'delete' => \$delete_arg,
'cleanup' => \$cleanup_arg,
'failed' => \$failed_arg,
'initialize' => \$initial_arg,
'db_archive' => \$tidy_arg,
'db_dump' => \$dump_arg,
'g|group=s' => \$group_arg,
'h|host=s' => \$host_arg,
'cron-create' => \$croncreate,
'cron-check' => \$croncheck,
'p|prefix=s' => \$prefix_arg,
'showgroups' => \$showgroups,
'setprop=s' => \$setprop,
) or usage('Invalid commmand line options.');
usage('Do not use together!') if ( $croncreate && $showgroups );
usage('Check the arguments!') unless (
( $add_arg || $delete_arg ) && (( $host_arg && $group_arg ) || $group_arg ) ||
( $initial_arg && ( $host_arg && $group_arg ) ) ||
( $initial_arg && ( $host_arg && $group_arg && ( $prefix_arg || $dryrun_arg ))) ||
( $croncreate || $croncheck || $failed_arg || $showgroups || $tidy_arg || $dump_arg ) ||
( $host_arg && $group_arg && $setprop )
);
$verbose_arg = 1 if ( $dryrun_arg || $vvv_arg );
return 1;
}
sub cli_args_override_global_config {
$serverconfig{verbose} = $verbose_arg if $verbose_arg;
$serverconfig{dryrun} = $dryrun_arg if $dryrun_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 --add -h <host> -g <group> # create a new host config
$command --add -g <group> # create a new group config
$command --delete -h <host> -g <group> # delete a existing host config
$command --delete -g <group> # delete a existing group config
$command --initialize -h <host> -g <group> # create target folder structure for defined host
$command --failed # show all failed backups
$command --failed -h <host> -g <group> # show failed backups of defined hosts and/or group
$command --failed --cleanup # purge failed backup folders
$command --setprop list -h <host> -g <group> # show Read-Write/Read-Only status of all snapshots
$command --setprop rw|ro -h <host> -g <group> # set all snapshots to Read-Write/Read-Only
$command --db_dump # create database dump
$command --db_archive # move records from table statistic to statistic_archive
where older than 100 days
$command --cron-create # generate and write cronjob file or print crontab to standard out
$command --cron-check # check if cronjob file / crontab up-to-date
$command --showgroups # show available groups
$command --help # show this help message
$command --version # show version number and help
Optional Arguments:
-v | -vvv # verbose mode to include debugging messages of level 1 and 3
-n # dry-run without making changes (implies verbose)
EOF
exit 0;
}