81 |
rodolico |
1 |
#! /usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
use strict;
|
|
|
4 |
use warnings;
|
|
|
5 |
|
|
|
6 |
my $clientBaseDirectory = '/var/www/clients/';
|
|
|
7 |
my $clientDirectoryPrefix = 'client';
|
|
|
8 |
my $dumpFileSuffix = 'dmp';
|
|
|
9 |
my $archiveDirectory = '/var/www/archives/';
|
|
|
10 |
|
|
|
11 |
my $client = shift;
|
|
|
12 |
my $name = shift;
|
|
|
13 |
my $rootPassword = shift;
|
|
|
14 |
|
|
|
15 |
sub getLine {
|
|
|
16 |
my $message = shift;
|
|
|
17 |
print "$message: ";
|
|
|
18 |
my $return = <>;
|
|
|
19 |
chomp $return;
|
|
|
20 |
return $return;
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
sub backupDatabases {
|
|
|
24 |
my ( $client, $rootPassword, $targetDir ) = @_;
|
|
|
25 |
print "mysqlshow -u root -p$rootPassword\n";
|
|
|
26 |
my @databases = `mysqlshow -u root -p$rootPassword`;
|
|
|
27 |
chomp @databases;
|
|
|
28 |
foreach my $thisdb ( @databases ) {
|
|
|
29 |
next unless $thisdb =~ m/(c$client[a-z0-9_-]+)/;
|
|
|
30 |
my $db = $1;
|
|
|
31 |
print "Dumping database $db\n";
|
|
|
32 |
`mysqldump -u root -p$rootPassword $db > $targetDir/$db.$dumpFileSuffix`;
|
|
|
33 |
}
|
|
|
34 |
print `ls -ablph $targetDir/*.$dumpFileSuffix`;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
$client = &getLine( "Enter the client Number" ) unless $client;
|
|
|
39 |
$name = &getLine( "Enter an identifier for the output file" ) unless $name;
|
|
|
40 |
$rootPassword = &getLine( "Enter the mysql password" ) unless $rootPassword;
|
|
|
41 |
$rootPassword = "'$rootPassword'";
|
|
|
42 |
|
|
|
43 |
$clientBaseDirectory .= "$clientDirectoryPrefix$client";
|
|
|
44 |
|
|
|
45 |
`mkdir -p $archiveDirectory`;
|
|
|
46 |
$archiveDirectory .= "$name\_$clientDirectoryPrefix$client.tar.xz";
|
|
|
47 |
|
|
|
48 |
die "call with client and name\n$0 client_number nameOfFile\n" unless $client and $name;
|
|
|
49 |
die "Client with number $client does not exist as $clientBaseDirectory\n" unless -d $clientBaseDirectory;
|
|
|
50 |
|
|
|
51 |
&backupDatabases( $client, $rootPassword, $clientBaseDirectory );
|
|
|
52 |
|
|
|
53 |
print "Creating archive, this may take a while\n";
|
|
|
54 |
`tar -cvp $clientBaseDirectory | xz --stdout --threads=0 --memlimit=50% > $archiveDirectory`;
|
|
|
55 |
print "cleaning up database dump files\n";
|
|
|
56 |
`rm $clientBaseDirectory/*.$dumpFileSuffix`;
|
|
|
57 |
|
|
|
58 |
print "Done, backup is now stored in $archiveDirectory\n";
|
|
|
59 |
1;
|
|
|
60 |
|