#! /usr/bin/env perl use strict; use warnings; my $clientBaseDirectory = '/var/www/clients/'; my $clientDirectoryPrefix = 'client'; my $dumpFileSuffix = 'dmp'; my $archiveDirectory = '/var/www/archives/'; my $client = shift; my $name = shift; my $rootPassword = shift; sub getLine { my $message = shift; print "$message: "; my $return = <>; chomp $return; return $return; } sub backupDatabases { my ( $client, $rootPassword, $targetDir ) = @_; print "mysqlshow -u root -p$rootPassword\n"; my @databases = `mysqlshow -u root -p$rootPassword`; chomp @databases; foreach my $thisdb ( @databases ) { next unless $thisdb =~ m/(c$client[a-z0-9_-]+)/; my $db = $1; print "Dumping database $db\n"; `mysqldump -u root -p$rootPassword $db > $targetDir/$db.$dumpFileSuffix`; } print `ls -ablph $targetDir/*.$dumpFileSuffix`; } $client = &getLine( "Enter the client Number" ) unless $client; $name = &getLine( "Enter an identifier for the output file" ) unless $name; $rootPassword = &getLine( "Enter the mysql password" ) unless $rootPassword; $rootPassword = "'$rootPassword'"; $clientBaseDirectory .= "$clientDirectoryPrefix$client"; `mkdir -p $archiveDirectory`; $archiveDirectory .= "$name\_$clientDirectoryPrefix$client.tar.xz"; die "call with client and name\n$0 client_number nameOfFile\n" unless $client and $name; die "Client with number $client does not exist as $clientBaseDirectory\n" unless -d $clientBaseDirectory; &backupDatabases( $client, $rootPassword, $clientBaseDirectory ); print "Creating archive, this may take a while\n"; `tar -cvp $clientBaseDirectory | xz --stdout --threads=0 --memlimit=50% > $archiveDirectory`; print "cleaning up database dump files\n"; `rm $clientBaseDirectory/*.$dumpFileSuffix`; print "Done, backup is now stored in $archiveDirectory\n"; 1;