-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup.pl
More file actions
53 lines (46 loc) · 1.73 KB
/
Copy pathbackup.pl
File metadata and controls
53 lines (46 loc) · 1.73 KB
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
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use YAML::XS 'LoadFile';
use DBI;
use POSIX 'strftime';
no warnings 'experimental::smartmatch';
#clear ouput folder
`rm -rf out && mkdir out`;
#read values from config.yaml
my $config = LoadFile('config.yaml');
my $webdavUrl = $config->{webdav}->{url};
my $webdavUsername = $config->{webdav}->{username};
my $webdavPassword = $config->{webdav}->{password};
my $backupDir = $config->{webdav}->{backupDir};
my $mysqlUsername = $config->{mysql}->{username};
my $mysqlPassword = $config->{mysql}->{password};
my $blacklist = $config->{blacklist};
my $mountPoint = "/mnt/sqlbackup";
# write to temp file to pipe username/password to mount
open (OUTFILE, '>>cred');
print OUTFILE "$webdavUsername\n$webdavPassword";
close (OUTFILE);
# create mountpoint
`mkdir $mountPoint`;
`cat cred | mount -t davfs $webdavUrl $mountPoint`;
unlink('cred');
my $mountPrefix = $mountPoint."/".$backupDir."/".POSIX::strftime('%Y/%m/%d', localtime);
`mkdir -p $mountPrefix`;
#connect to mysql
my $connection = DBI->connect("DBI:mysql:information_schema:localhost:", $mysqlUsername, $mysqlPassword);
my $sql = $connection->prepare("show databases");
$sql->execute();
#loop over databases and execute dump command
while ((my $databaseName) = $sql->fetchrow_array()){
if (not $databaseName ~~ $blacklist) {
#use smartwatch to exclude items from blacklist
print $databaseName."\n";
#run dump command
`mysqldump --force --opt --user=$mysqlUsername --password=$mysqlPassword --databases $databaseName > out/$databaseName.sql`;
# gzip
`gzip -f out/$databaseName.sql`;
`rm -f out/$databaseName.sql`;
`cp out/$databaseName.sql.gz $mountPrefix/$databaseName.sql.gz`
}
}