98_backup.pm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. ################################################################
  2. # $Id: 98_backup.pm 11984 2016-08-19 12:47:50Z rudolfkoenig $
  3. # vim: ts=2:et
  4. #
  5. # (c) 2012 Copyright: Martin Fischer (m_fischer at gmx dot de)
  6. # All rights reserved
  7. #
  8. # This script free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # The GNU General Public License can be found at
  14. # http://www.gnu.org/copyleft/gpl.html.
  15. # A copy is found in the textfile GPL.txt and important notices to the license
  16. # from the author is found in LICENSE.txt distributed with these scripts.
  17. #
  18. # This script is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. # GNU General Public License for more details.
  22. #
  23. ################################################################
  24. package main;
  25. use strict;
  26. use warnings;
  27. sub CommandBackup($$);
  28. sub parseConfig($);
  29. sub readModpath($$);
  30. sub createArchiv($$);
  31. my @pathname;
  32. #####################################
  33. sub
  34. backup_Initialize($$)
  35. {
  36. my %hash = ( Fn => "CommandBackup",
  37. Hlp => ",create a backup of fhem configuration, state and modpath" );
  38. $cmds{backup} = \%hash;
  39. }
  40. #####################################
  41. sub
  42. CommandBackup($$)
  43. {
  44. my ($cl, $param) = @_;
  45. my $modpath = AttrVal("global", "modpath","");
  46. my $configfile = AttrVal("global", "configfile", "");
  47. my $statefile = AttrVal("global", "statefile", "");
  48. # prevent duplicate entries in backup list for default config, forum #54826
  49. $configfile = '' if ($configfile eq 'fhem.cfg' || configDBUsed());
  50. $statefile = '' if ($statefile eq "./log/fhem.save");
  51. my $msg;
  52. my $ret;
  53. Log 1, "NOTE: make sure you have a database backup!" if(configDBUsed());
  54. # set backupdir
  55. my $backupdir;
  56. if (!defined($attr{global}{backupdir})) {
  57. $backupdir = "$modpath/backup";
  58. } else {
  59. if ($attr{global}{backupdir} =~ m/^\/.*/) {
  60. $backupdir = $attr{global}{backupdir};
  61. } elsif ($attr{global}{backupdir} =~ m/^\.+\/.*/) {
  62. $backupdir = "$modpath/$attr{global}{backupdir}";
  63. } else {
  64. $backupdir = "$modpath/$attr{global}{backupdir}";
  65. }
  66. }
  67. # create backupdir if not exists
  68. if (!-d $backupdir) {
  69. Log 4, "backup create backupdir: '$backupdir'";
  70. $ret = `(mkdir -p $backupdir) 2>&1`;
  71. if ($ret) {
  72. chomp $ret;
  73. $msg = "backup: $ret";
  74. return $msg;
  75. }
  76. }
  77. if(configDBUsed()) {
  78. # add configDB configuration file
  79. push @pathname, 'configDB.conf';
  80. Log 4, "backup include: 'configDB.conf'";
  81. } else {
  82. # get pathnames to archiv
  83. push @pathname, $configfile if($configfile);
  84. Log 4, "backup include: '$configfile'";
  85. $ret = parseConfig($configfile);
  86. push @pathname, $statefile if($statefile);
  87. Log 4, "backup include: '$statefile'";
  88. }
  89. $ret = readModpath($modpath,$backupdir);
  90. # create archiv
  91. $ret = createArchiv($backupdir, $cl);
  92. @pathname = [];
  93. undef @pathname;
  94. return $ret;
  95. }
  96. sub
  97. parseConfig($)
  98. {
  99. my $configfile = shift;
  100. # we need default value to read included files
  101. $configfile = $configfile ? $configfile : 'fhem.cfg';
  102. my $fh;
  103. my $msg;
  104. my $ret;
  105. if (!open($fh,$configfile)) {
  106. $msg = "Can't open $configfile: $!";
  107. Log 1, "backup $msg";
  108. return $msg;
  109. }
  110. while (my $l = <$fh>) {
  111. $l =~ s/[\r\n]//g;
  112. if ($l =~ m/^\s*include\s+(\S+)\s*.*$/) {
  113. if (-e $1) {
  114. push @pathname, $1;
  115. Log 4, "backup include: '$1'";
  116. $ret = parseConfig($1);
  117. } else {
  118. Log 1, "backup configfile: '$1' does not exists! File not included."
  119. }
  120. }
  121. }
  122. close $fh;
  123. return $ret;
  124. }
  125. sub
  126. readModpath($$)
  127. {
  128. my ($modpath,$backupdir) = @_;
  129. my $msg;
  130. my $ret;
  131. if (!opendir(DH, $modpath)) {
  132. $msg = "Can't open $modpath: $!";
  133. Log 1, "backup $msg";
  134. return $msg;
  135. }
  136. my @files = <$modpath/*>;
  137. foreach my $file (@files) {
  138. if ($file eq $backupdir && (-d $file || -l $file)) {
  139. Log 4, "backup exclude: '$file'";
  140. } else {
  141. Log 4, "backup include: '$file'";
  142. push @pathname, $file;
  143. }
  144. }
  145. return $ret;
  146. }
  147. sub
  148. createArchiv($$)
  149. {
  150. my ($backupdir,$cl) = @_;
  151. my $backupcmd = (!defined($attr{global}{backupcmd}) ? undef : $attr{global}{backupcmd});
  152. my $symlink = (!defined($attr{global}{backupsymlink}) ? "no" : $attr{global}{backupsymlink});
  153. my $tarOpts;
  154. my $msg;
  155. my $ret;
  156. my $dateTime = TimeNow();
  157. $dateTime =~ s/ /_/g;
  158. $dateTime =~ s/(:|-)//g;
  159. my $pathlist = join( "\" \"", @pathname );
  160. my $cmd="";
  161. if (!defined($backupcmd)) {
  162. if (lc($symlink) eq "no") {
  163. $tarOpts = "cf";
  164. } else {
  165. $tarOpts = "chf";
  166. }
  167. # prevents tar's output of "Removing leading /" and return total bytes of
  168. # archive
  169. $cmd = "tar -$tarOpts - \"$pathlist\" |gzip > $backupdir/FHEM-$dateTime.tar.gz";
  170. } else {
  171. $cmd = "$backupcmd \"$pathlist\"";
  172. }
  173. Log 2, "Backup with command: $cmd";
  174. if($cl && ref($cl) eq "HASH" && $cl->{TYPE} && $cl->{TYPE} eq "FHEMWEB") {
  175. use Blocking;
  176. our $BC_telnetDevice;
  177. BC_searchTelnet("backup");
  178. my $tp = $defs{$BC_telnetDevice}{PORT};
  179. system("($cmd; echo Backup done;".
  180. "$^X $0 localhost:$tp 'trigger global backup done')2>&1 &");
  181. return "Started the backup in the background, watch the log for details";
  182. }
  183. $ret = `($cmd) 2>&1`;
  184. if($ret) {
  185. chomp $ret;
  186. Log 1, "backup $ret";
  187. }
  188. if (!defined($backupcmd) && -e "$backupdir/FHEM-$dateTime.tar.gz") {
  189. my $size = -s "$backupdir/FHEM-$dateTime.tar.gz";
  190. $msg = "backup done: FHEM-$dateTime.tar.gz ($size Bytes)";
  191. DoTrigger("global", $msg);
  192. Log 1, $msg;
  193. $ret .= "\n".$msg;
  194. }
  195. return $ret;
  196. }
  197. 1;
  198. =pod
  199. =item command
  200. =item summary create a backup of the FHEM installation
  201. =item summary_DE erzeugt eine Sicherungsdatei der FHEM Installation
  202. =begin html
  203. <a name="backup"></a>
  204. <h3>backup</h3>
  205. <ul>
  206. <code>backup</code><br>
  207. <br>
  208. The complete FHEM directory (containing the modules), the WebInterface
  209. pgm2 (if installed) and the config-file will be saved into a .tar.gz
  210. file by default. The file is stored with a timestamp in the
  211. <a href="#modpath">modpath</a>/backup directory or to a directory
  212. specified by the global attribute <a href="#backupdir">backupdir</a>.<br>
  213. Note: tar and gzip must be installed to use this feature.
  214. <br>
  215. <br>
  216. If you need to call tar with support for symlinks, you could set the
  217. global attribute <a href="#backupsymlink">backupsymlink</a> to everything
  218. else as "no".
  219. <br>
  220. <br>
  221. You could pass the backup to your own command / script by using the
  222. global attribute <a href="#backupcmd">backupcmd</a>.
  223. <br>
  224. <br>
  225. </ul>
  226. =end html
  227. =cut