From 34a2c7390ea9662d33258d384e72fff1912343ff Mon Sep 17 00:00:00 2001 From: Darkelarious Date: Sat, 13 May 2017 14:18:28 +0200 Subject: revised synchronization methods, config settings and bug fixes --- lib/MasterServer/Database/SQLite/dbCore.pm | 38 ++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'lib/MasterServer/Database/SQLite/dbCore.pm') diff --git a/lib/MasterServer/Database/SQLite/dbCore.pm b/lib/MasterServer/Database/SQLite/dbCore.pm index f58d535..0772a4e 100755 --- a/lib/MasterServer/Database/SQLite/dbCore.pm +++ b/lib/MasterServer/Database/SQLite/dbCore.pm @@ -1,11 +1,11 @@ - package MasterServer::Database::SQLite::dbCore; use strict; use warnings; +use POSIX qw/strftime/; use Exporter 'import'; -our @EXPORT = qw| database_login |; +our @EXPORT = qw| database_login dump_database |; ################################################################################ ## login to the database with credentials provided in the config file. @@ -19,7 +19,10 @@ sub database_login { # get db info my @db_type = split(':', $self->{dblogin}->[0]); - + + # inform what db we try to load + $self->log("info","Database: $db_type[1], $db_type[2]"); + # check if database file exists my $db_file = [split(':', $self->{dblogin}->[0])]->[2]; $db_file =~ s/dbname=//i; @@ -31,10 +34,7 @@ sub database_login { # end program $self->halt(); } - - # inform what DB we try to load - # $self->log("info","Database: $db_type[1]"); - + # create the dbi object my $dbh = DBI->connect(@{$self->{dblogin}}, {PrintError => 1}); @@ -72,4 +72,28 @@ sub database_login { return undef; } +################################################################################ +## Dump the database in the data/dump folder. +## useful for backups, historical data +################################################################################ +sub dump_database { + my $self = shift; + + # filename / time + my $time = strftime('%Y-%m-%d-%H-%M',localtime); + + # read db credentials from db login + my @db_type = split ':', $self->{dblogin}->[0]; + $db_type[2] =~ s/dbname=//; + + # split db path + my @db_path = split '/', $db_type[2]; + + # use pg_dump to dump Postgresql databases + system("cp $db_type[2] $self->{root}/data/dumps/SQLite-$time-$db_path[-1]"); + + # log + $self->log("dump", "Dumping database to /data/dumps/SQLite-$time-$db_path[-1]"); +} + 1; -- cgit v1.2.3