mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-03-10 03:06:37 +00:00
Added new command backup.
git-svn-id: https://svn.fhem.de/fhem/trunk@1586 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
1e85097ff8
commit
7a8652f2c7
@ -33,6 +33,7 @@
|
||||
- change: FHEMWEB support for the new www/pgm2 directroy added (M. Fischer)
|
||||
- change: Makefile support for for the new www/pgm2 directroy and new targets
|
||||
backup and uninstall added. More verbose output. (M. Fischer)
|
||||
- feature: new command backup added (M. Fischer)
|
||||
|
||||
- 2011-12-31 (5.2)
|
||||
- bugfix: applying smallscreen attributes to firefox/opera
|
||||
|
197
fhem/FHEM/99_backup.pm
Normal file
197
fhem/FHEM/99_backup.pm
Normal file
@ -0,0 +1,197 @@
|
||||
################################################################
|
||||
# $Id: $
|
||||
#
|
||||
# (c) 2012 Copyright: Martin Fischer (m_fischer at gmx dot de)
|
||||
# All rights reserved
|
||||
#
|
||||
# This script free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# The GNU General Public License can be found at
|
||||
# http://www.gnu.org/copyleft/gpl.html.
|
||||
# A copy is found in the textfile GPL.txt and important notices to the license
|
||||
# from the author is found in LICENSE.txt distributed with these scripts.
|
||||
#
|
||||
# This script is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
################################################################
|
||||
|
||||
package main;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
sub CommandBackup($$);
|
||||
sub parseConfig($);
|
||||
sub readModpath($$);
|
||||
sub createArchiv($);
|
||||
|
||||
my @pathname;
|
||||
|
||||
#####################################
|
||||
sub
|
||||
backup_Initialize($$)
|
||||
{
|
||||
my %hash = ( Fn => "CommandBackup",
|
||||
Hlp => ",create a backup of fhem configuration, state and modpath" );
|
||||
$cmds{backup} = \%hash;
|
||||
|
||||
$modules{Global}{AttrList} .= " backupsymlink";
|
||||
|
||||
### remove me start
|
||||
# my $dateTime = TimeNow();
|
||||
# $dateTime =~ s/ /_/g;
|
||||
# $dateTime =~ s/(:|-)//g;
|
||||
# my $ret = `(cp /usr/share/fhem/FHEM/99_backup.pm /home/martin/src/fhem/build/backup/99_backup.pm-$dateTime) 2>&1`;
|
||||
# Log 0, "==> copy 99_backup.pm to /home/martin/src/fhem/build/backup/99_backup.pm-$dateTime";
|
||||
### remove me end
|
||||
}
|
||||
|
||||
#####################################
|
||||
sub
|
||||
CommandBackup($$)
|
||||
{
|
||||
my ($cl, $param) = @_;
|
||||
my $modpath = $attr{global}{modpath};
|
||||
my $configfile = (!defined($attr{global}{configfile}) ? undef : $attr{global}{configfile});
|
||||
my $statefile = (!defined($attr{global}{statefile}) ? undef : $attr{global}{statefile});
|
||||
my $msg;
|
||||
my $ret;
|
||||
|
||||
# set backupdir
|
||||
my $backupdir;
|
||||
if (!defined($attr{global}{backupdir})) {
|
||||
$backupdir = "$modpath/backup";
|
||||
} else {
|
||||
if ($attr{global}{backupdir} =~ m/^\/.*/) {
|
||||
$backupdir = $attr{global}{backupdir};
|
||||
} elsif ($attr{global}{backupdir} =~ m/^\.+\/.*/) {
|
||||
$backupdir = "$modpath/$attr{global}{backupdir}";
|
||||
} else {
|
||||
$backupdir = "$modpath/$attr{global}{backupdir}";
|
||||
}
|
||||
}
|
||||
|
||||
# create backupdir if not exists
|
||||
if (!-d $backupdir) {
|
||||
Log 4, "backup create backupdir: '$backupdir'";
|
||||
$ret = `(mkdir -p $backupdir) 2>&1`;
|
||||
if ($ret) {
|
||||
chomp $ret;
|
||||
$msg = "backup: $ret";
|
||||
return $msg;
|
||||
}
|
||||
}
|
||||
|
||||
# get pathnames to archiv
|
||||
push @pathname, $configfile;
|
||||
Log 4, "backup include: '$configfile'";
|
||||
$ret = parseConfig($configfile);
|
||||
push @pathname, $statefile;
|
||||
Log 4, "backup include: '$statefile'";
|
||||
$ret = readModpath($modpath,$backupdir);
|
||||
|
||||
# create archiv
|
||||
$ret = createArchiv($backupdir);
|
||||
|
||||
@pathname = [];
|
||||
undef @pathname;
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
sub
|
||||
parseConfig($)
|
||||
{
|
||||
my $configfile = shift;
|
||||
my $fh;
|
||||
my $msg;
|
||||
my $ret;
|
||||
|
||||
if (!open($fh,$configfile)) {
|
||||
$msg = "Can't open $configfile: $!";
|
||||
Log 1, "backup $msg";
|
||||
return $msg;
|
||||
}
|
||||
|
||||
while (my $l = <$fh>) {
|
||||
$l =~ s/[\r\n]//g;
|
||||
if ($l =~ m/^\s*include\s+(\S+)\s*.*$/) {
|
||||
if (-e $1) {
|
||||
push @pathname, $1;
|
||||
Log 4, "backup include: '$1'";
|
||||
$ret = parseConfig($1);
|
||||
} else {
|
||||
Log 1, "backup configfile: '$1' does not exists! File not included."
|
||||
}
|
||||
}
|
||||
}
|
||||
close $fh;
|
||||
return $ret;
|
||||
}
|
||||
|
||||
sub
|
||||
readModpath($$)
|
||||
{
|
||||
my ($modpath,$backupdir) = @_;
|
||||
my $msg;
|
||||
my $ret;
|
||||
|
||||
if (!opendir(DH, $modpath)) {
|
||||
$msg = "Can't open $modpath: $!";
|
||||
Log 1, "backup $msg";
|
||||
return $msg;
|
||||
}
|
||||
my @files = <$modpath/*>;
|
||||
foreach my $file (@files) {
|
||||
if ($file eq $backupdir && (-d $file || -l $file)) {
|
||||
Log 4, "backup exclude: '$file'";
|
||||
} else {
|
||||
Log 4, "backup include: '$file'";
|
||||
push @pathname, $file;
|
||||
}
|
||||
}
|
||||
return $ret;;
|
||||
}
|
||||
|
||||
sub
|
||||
createArchiv($)
|
||||
{
|
||||
my $backupdir = shift;
|
||||
my $symlink = (!defined($attr{global}{backupsymlink}) ? "no" : $attr{global}{backupsymlink});
|
||||
my $tarOpts;
|
||||
my $msg;
|
||||
my $ret;
|
||||
|
||||
my $dateTime = TimeNow();
|
||||
$dateTime =~ s/ /_/g;
|
||||
$dateTime =~ s/(:|-)//g;
|
||||
|
||||
if (lc($symlink) eq "no") {
|
||||
$tarOpts = "cf";
|
||||
} else {
|
||||
$tarOpts = "chf";
|
||||
}
|
||||
|
||||
# prevents tar's output of "Removing leading /" and return total bytes of archive
|
||||
$ret = `(tar -$tarOpts - @pathname | gzip > $backupdir/FHEM-$dateTime.tar.gz) 2>&1`;
|
||||
if($ret) {
|
||||
chomp $ret;
|
||||
Log 1, "backup $ret";
|
||||
}
|
||||
if (-e "$backupdir/FHEM-$dateTime.tar.gz") {
|
||||
my $size = -s "$backupdir/FHEM-$dateTime.tar.gz";
|
||||
$msg = "backup done: FHEM-$dateTime.tar.gz ($size Bytes)";
|
||||
Log 1, $msg;
|
||||
$ret .= "\n".$msg;
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
# vim: ts=2:et
|
||||
|
||||
1;
|
@ -496,3 +496,7 @@
|
||||
|
||||
- So May 20 2012 (M. Fischer)
|
||||
- Added support for a cleaner installation of pgm2 via updatefhem.
|
||||
|
||||
- Sa May 26 2012 (M. Fischer)
|
||||
- Added new command backup to separate this feature from updatefhem.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user