2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 12:49:34 +00:00

configDB: move dump processing to backend

git-svn-id: https://svn.fhem.de/fhem/trunk@16218 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
betateilchen 2018-02-18 19:23:23 +00:00
parent e9e32a4e84
commit 696cd29900
2 changed files with 123 additions and 57 deletions

View File

@ -59,64 +59,66 @@ sub CommandConfigdb($$) {
}
when ('dump') {
my ($dbconn,$dbuser,$dbpass,$dbtype) = _cfgDB_readConfig();
my ($dbname,$dbhostname,$dbport,$gzip,$mp,$ret,$size,$source,$target,$ts);
$ts = strftime('%Y-%m-%d_%H-%M-%S',localtime);
$mp = $configDB{attr}{'dumpPath'};
$mp //= AttrVal('global','modpath','.').'/log';
$target = "$mp/configDB_$ts.dump";
if (lc($param1) eq 'unzipped') {
$gzip = '';
} else {
$gzip = '| gzip -c';
$target .= '.gz';
}
if ($dbtype eq 'SQLITE') {
(undef,$source) = split (/=/, $dbconn);
my $dumpcmd = "echo '.dump fhem%' | sqlite3 $source $gzip > $target";
Log 4,"configDB: $dumpcmd";
$ret = qx($dumpcmd);
return $ret if $ret; # return error message if available
} elsif ($dbtype eq 'MYSQL') {
($dbname,$dbhostname,$dbport) = split (/;/,$dbconn);
$dbport //= '=3306';
(undef,$dbname) = split (/=/,$dbname);
(undef,$dbhostname) = split (/=/,$dbhostname);
(undef,$dbport) = split (/=/,$dbport);
my $dbtables = "fhemversions fhemconfig fhemstate fhemb64filesave";
my $dumpcmd = "mysqldump --user=$dbuser --password=$dbpass --host=$dbhostname --port=$dbport -Q $dbname $dbtables $gzip > $target";
Log 4,"configDB: $dumpcmd";
$ret = qx($dumpcmd);
return $ret if $ret;
$source = $dbname;
} elsif ($dbtype eq 'POSTGRESQL') {
($dbname,$dbhostname,$dbport) = split (/;/,$dbconn);
$dbport //= '=5432';
(undef,$dbname) = split (/=/,$dbname);
(undef,$dbhostname) = split (/=/,$dbhostname);
(undef,$dbport) = split (/=/,$dbport);
my $dbtables = "-t fhemversions -t fhemconfig -t fhemstate -t fhemb64filesave";
my $dumpcmd = "PGPASSWORD=$dbpass pg_dump -U $dbuser -h $dbhostname -p $dbport $dbname $dbtables $gzip > $target";
Log 4,"configDB: $dumpcmd";
$ret = qx($dumpcmd);
return $ret if $ret;
$source = $dbname;
} else {
return "configdb dump not supported for $dbtype!";
}
$size = -s $target;
$size //= 0;
$ret = "configDB dumped $size bytes\nfrom: $source\n to: $target";
return $ret;
return _cfgDB_dump($param1);
}
# my ($dbconn,$dbuser,$dbpass,$dbtype) = _cfgDB_readConfig();
# my ($dbname,$dbhostname,$dbport,$gzip,$mp,$ret,$size,$source,$target,$ts);
# $ts = strftime('%Y-%m-%d_%H-%M-%S',localtime);
# $mp = $configDB{attr}{'dumpPath'};
# $mp //= AttrVal('global','modpath','.').'/log';
# $target = "$mp/configDB_$ts.dump";
#
# if (lc($param1) eq 'unzipped') {
# $gzip = '';
# } else {
# $gzip = '| gzip -c';
# $target .= '.gz';
# }
#
# if ($dbtype eq 'SQLITE') {
# (undef,$source) = split (/=/, $dbconn);
# my $dumpcmd = "echo '.dump fhem%' | sqlite3 $source $gzip > $target";
# Log 4,"configDB: $dumpcmd";
# $ret = qx($dumpcmd);
# return $ret if $ret; # return error message if available
#
# } elsif ($dbtype eq 'MYSQL') {
# ($dbname,$dbhostname,$dbport) = split (/;/,$dbconn);
# $dbport //= '=3306';
# (undef,$dbname) = split (/=/,$dbname);
# (undef,$dbhostname) = split (/=/,$dbhostname);
# (undef,$dbport) = split (/=/,$dbport);
# my $dbtables = "fhemversions fhemconfig fhemstate fhemb64filesave";
# my $dumpcmd = "mysqldump --user=$dbuser --password=$dbpass --host=$dbhostname --port=$dbport -Q $dbname $dbtables $gzip > $target";
# Log 4,"configDB: $dumpcmd";
# $ret = qx($dumpcmd);
# return $ret if $ret;
# $source = $dbname;
#
# } elsif ($dbtype eq 'POSTGRESQL') {
# ($dbname,$dbhostname,$dbport) = split (/;/,$dbconn);
# $dbport //= '=5432';
# (undef,$dbname) = split (/=/,$dbname);
# (undef,$dbhostname) = split (/=/,$dbhostname);
# (undef,$dbport) = split (/=/,$dbport);
# my $dbtables = "-t fhemversions -t fhemconfig -t fhemstate -t fhemb64filesave";
# my $dumpcmd = "PGPASSWORD=$dbpass pg_dump -U $dbuser -h $dbhostname -p $dbport $dbname $dbtables $gzip > $target";
# Log 4,"configDB: $dumpcmd";
# $ret = qx($dumpcmd);
# return $ret if $ret;
# $source = $dbname;
#
# } else {
# return "configdb dump not supported for $dbtype!";
# }
#
# $size = -s $target;
# $size //= 0;
# $ret = "configDB dumped $size bytes\nfrom: $source\n to: $target";
# return $ret;
# }
when ('diff') {
return "\n Syntax: configdb diff <device> <version>" if @a != 3;
Log3('configdb', 4, "configdb: diff requested for device: $param1 in version $param2.");

View File

@ -126,7 +126,8 @@
#
# 2017-08-31 - changed improve table_info for migration check
#
# 2018-02-17 - changed. remove experimenatal cache functions
# 2018-02-17 - changed remove experimenatal cache functions
# 2018-02-18 - changed move dump processing to backend
#
##############################################################################
=cut
@ -182,6 +183,7 @@ sub _cfgDB_Rotate($$);
sub _cfgDB_Search($$;$);
sub _cfgDB_Uuid();
sub _cfgDB_table_exists($$);
sub _cfgDB_dump($);
##################################################
# Read configuration file for DB connection
@ -1150,6 +1152,68 @@ sub _cfgDB_type() {
return "$cfgDB_dbtype (b64)";
}
sub _cfgDB_dump($) {
my ($param1) = @_;
$param1 //= '';
my ($dbconn,$dbuser,$dbpass,$dbtype) = _cfgDB_readConfig();
my ($dbname,$dbhostname,$dbport,$gzip,$mp,$ret,$size,$source,$target,$ts);
$ts = strftime('%Y-%m-%d_%H-%M-%S',localtime);
$mp = $configDB{attr}{'dumpPath'};
$mp //= AttrVal('global','modpath','.').'/log';
$target = "$mp/configDB_$ts.dump";
if (lc($param1) eq 'unzipped') {
$gzip = '';
} else {
$gzip = '| gzip -c';
$target .= '.gz';
}
if ($dbtype eq 'SQLITE') {
(undef,$source) = split (/=/, $dbconn);
my $dumpcmd = "echo '.dump fhem%' | sqlite3 $source $gzip > $target";
Log 4,"configDB: $dumpcmd";
$ret = qx($dumpcmd);
return $ret if $ret; # return error message if available
} elsif ($dbtype eq 'MYSQL') {
($dbname,$dbhostname,$dbport) = split (/;/,$dbconn);
$dbport //= '=3306';
(undef,$dbname) = split (/=/,$dbname);
(undef,$dbhostname) = split (/=/,$dbhostname);
(undef,$dbport) = split (/=/,$dbport);
my $dbtables = "fhemversions fhemconfig fhemstate fhemb64filesave";
my $dumpcmd = "mysqldump --user=$dbuser --password=$dbpass --host=$dbhostname --port=$dbport -Q $dbname $dbtables $gzip > $target";
Log 4,"configDB: $dumpcmd";
$ret = qx($dumpcmd);
return $ret if $ret;
$source = $dbname;
} elsif ($dbtype eq 'POSTGRESQL') {
($dbname,$dbhostname,$dbport) = split (/;/,$dbconn);
$dbport //= '=5432';
(undef,$dbname) = split (/=/,$dbname);
(undef,$dbhostname) = split (/=/,$dbhostname);
(undef,$dbport) = split (/=/,$dbport);
my $dbtables = "-t fhemversions -t fhemconfig -t fhemstate -t fhemb64filesave";
my $dumpcmd = "PGPASSWORD=$dbpass pg_dump -U $dbuser -h $dbhostname -p $dbport $dbname $dbtables $gzip > $target";
Log 4,"configDB: $dumpcmd";
$ret = qx($dumpcmd);
return $ret if $ret;
$source = $dbname;
} else {
return "configdb dump not supported for $dbtype!";
}
$size = -s $target;
$size //= 0;
$ret = "configDB dumped $size bytes\nfrom: $source\n to: $target";
return $ret;
}
##################################################
# functions used for file handling
# called by 98_configdb.pm