2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-20 19:36:02 +00:00

98_backup.pm: post trigger if run in the background (Forum #53349)

git-svn-id: https://svn.fhem.de/fhem/trunk@11465 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2016-05-16 11:56:10 +00:00
parent dbe1d0c4c6
commit 32178514d9
2 changed files with 31 additions and 17 deletions

View File

@ -182,7 +182,8 @@ createArchiv($$)
$tarOpts = "chf"; $tarOpts = "chf";
} }
# prevents tar's output of "Removing leading /" and return total bytes of archive # prevents tar's output of "Removing leading /" and return total bytes of
# archive
$cmd = "tar -$tarOpts - \"$pathlist\" |gzip > $backupdir/FHEM-$dateTime.tar.gz"; $cmd = "tar -$tarOpts - \"$pathlist\" |gzip > $backupdir/FHEM-$dateTime.tar.gz";
} else { } else {
@ -191,7 +192,13 @@ createArchiv($$)
} }
Log 2, "Backup with command: $cmd"; Log 2, "Backup with command: $cmd";
if($cl && ref($cl) eq "HASH" && $cl->{TYPE} && $cl->{TYPE} eq "FHEMWEB") { if($cl && ref($cl) eq "HASH" && $cl->{TYPE} && $cl->{TYPE} eq "FHEMWEB") {
system("($cmd; echo Backup done) 2>&1 &"); use Blocking;
our $BC_telnetDevice;
BC_searchTelnet("backup");
my $tp = $defs{$BC_telnetDevice}{PORT};
system("($cmd; echo Backup done;".
"$^X $0 localhost:$tp 'trigger global backup done')2>&1 &");
return "Started the backup in the background, watch the log for details"; return "Started the backup in the background, watch the log for details";
} }
$ret = `($cmd) 2>&1`; $ret = `($cmd) 2>&1`;

View File

@ -25,18 +25,15 @@ sub BlockingExit();
sub BlockingKill($); sub BlockingKill($);
sub BlockingInformParent($;$$); sub BlockingInformParent($;$$);
my $telnetDevice; our $BC_telnetDevice;
my $telnetClient; my $telnetClient;
sub sub
BlockingCall($$@) BC_searchTelnet($)
{ {
my ($blockingFn, $arg, $finishFn, $timeout, $abortFn, $abortArg) = @_; my ($blockingFn) = @_;
# Look for the telnetport. Must be done before forking to be able to create a $BC_telnetDevice = undef;
# temporary device. Do it each time, as the old telnet may got a password
$telnetDevice = undef;
foreach my $d (sort keys %defs) { # foreach my $d (sort keys %defs) { #
my $h = $defs{$d}; my $h = $defs{$d};
next if(!$h->{TYPE} || $h->{TYPE} ne "telnet" || $h->{SNAME}); next if(!$h->{TYPE} || $h->{TYPE} ne "telnet" || $h->{SNAME});
@ -47,23 +44,33 @@ BlockingCall($$@)
my %cDev = ( SNAME=>$d, TYPE=>$h->{TYPE}, NAME=>$d.time() ); my %cDev = ( SNAME=>$d, TYPE=>$h->{TYPE}, NAME=>$d.time() );
next if(Authenticate(\%cDev, undef) == 2); # Needs password next if(Authenticate(\%cDev, undef) == 2); # Needs password
$telnetDevice = $d; $BC_telnetDevice = $d;
last; last;
} }
# If not suitable telnet device found, create a temporary one # If not suitable telnet device found, create a temporary one
if(!$telnetDevice) { if(!$BC_telnetDevice) {
$telnetDevice = "telnetForBlockingFn_".time(); $BC_telnetDevice = "telnetForBlockingFn_".time();
my $ret = CommandDefine(undef, "-temporary $telnetDevice telnet 0"); my $ret = CommandDefine(undef, "-temporary $BC_telnetDevice telnet 0");
if($ret) { if($ret) {
$ret = "BlockingCall ($blockingFn): ". $ret = "BlockingCall ($blockingFn): ".
"No telnet port found and cannot create one: $ret"; "No telnet port found and cannot create one: $ret";
Log 1, $ret; Log 1, $ret;
return undef; return undef;
} }
$attr{$telnetDevice}{room} = "hidden"; # no red ?, Forum #46640 $attr{$BC_telnetDevice}{room} = "hidden"; # no red ?, Forum #46640
$attr{$telnetDevice}{allowfrom} = "127.0.0.1"; $attr{$BC_telnetDevice}{allowfrom} = "127.0.0.1";
} }
}
sub
BlockingCall($$@)
{
my ($blockingFn, $arg, $finishFn, $timeout, $abortFn, $abortArg) = @_;
# Look for the telnetport. Must be done before forking to be able to create a
# temporary device. Do it each time, as the old telnet may got a password
BC_searchTelnet($blockingFn);
# do fork # do fork
my $pid = fhemFork; my $pid = fhemFork;
@ -74,7 +81,7 @@ BlockingCall($$@)
if($pid) { if($pid) {
Log 4, "BlockingCall ($blockingFn): created child ($pid), ". Log 4, "BlockingCall ($blockingFn): created child ($pid), ".
"uses $telnetDevice to connect back"; "uses $BC_telnetDevice to connect back";
my %h = ( pid=>$pid, fn=>$blockingFn, finishFn=>$finishFn, my %h = ( pid=>$pid, fn=>$blockingFn, finishFn=>$finishFn,
abortFn=>$abortFn, abortArg=>$abortArg ); abortFn=>$abortFn, abortArg=>$abortArg );
if($timeout) { if($timeout) {
@ -104,7 +111,7 @@ BlockingInformParent($;$$)
# Write the data back, calling the function # Write the data back, calling the function
if(!$telnetClient) { if(!$telnetClient) {
my $addr = "localhost:$defs{$telnetDevice}{PORT}"; my $addr = "localhost:$defs{$BC_telnetDevice}{PORT}";
$telnetClient = IO::Socket::INET->new(PeerAddr => $addr); $telnetClient = IO::Socket::INET->new(PeerAddr => $addr);
if(!$telnetClient) { if(!$telnetClient) {
Log 1, "BlockingInformParent ($informFn): Can't connect to $addr: $@"; Log 1, "BlockingInformParent ($informFn): Can't connect to $addr: $@";