98_backup.pm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. ################################################################
  2. # $Id: 98_backup.pm 15862 2018-01-12 21:01:28Z 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 $byUpdate = ($param && $param eq "startedByUpdate");
  46. my $modpath = AttrVal("global", "modpath","");
  47. my $configfile = AttrVal("global", "configfile", "");
  48. my $statefile = AttrVal("global", "statefile", "");
  49. # prevent duplicate entries in backup list for default config, forum #54826
  50. $configfile = '' if ($configfile eq 'fhem.cfg' || configDBUsed());
  51. $statefile = '' if ($statefile eq "./log/fhem.save");
  52. my $msg;
  53. my $ret;
  54. Log 1, "NOTE: make sure you have a database backup!" if(configDBUsed());
  55. # set backupdir
  56. my $backupdir;
  57. if (!defined($attr{global}{backupdir})) {
  58. $backupdir = "$modpath/backup";
  59. } else {
  60. if ($attr{global}{backupdir} =~ m/^\/.*/) {
  61. $backupdir = $attr{global}{backupdir};
  62. } elsif ($attr{global}{backupdir} =~ m/^\.+\/.*/) {
  63. $backupdir = "$modpath/$attr{global}{backupdir}";
  64. } else {
  65. $backupdir = "$modpath/$attr{global}{backupdir}";
  66. }
  67. }
  68. # create backupdir if not exists
  69. if (!-d $backupdir) {
  70. Log 4, "backup create backupdir: '$backupdir'";
  71. $ret = `(mkdir -p $backupdir) 2>&1`;
  72. if ($ret) {
  73. chomp $ret;
  74. $msg = "backup: $ret";
  75. return $msg;
  76. }
  77. }
  78. if(configDBUsed()) {
  79. # add configDB configuration file
  80. push @pathname, 'configDB.conf';
  81. Log 4, "backup include: 'configDB.conf'";
  82. } else {
  83. # get pathnames to archiv
  84. push @pathname, $configfile if($configfile);
  85. Log 4, "backup include: '$configfile'";
  86. $ret = parseConfig($configfile);
  87. push @pathname, $statefile if($statefile);
  88. Log 4, "backup include: '$statefile'";
  89. }
  90. $ret = readModpath($modpath,$backupdir);
  91. # create archiv
  92. $ret = createArchiv($backupdir, $cl, $byUpdate);
  93. @pathname = [];
  94. undef @pathname;
  95. return $ret;
  96. }
  97. sub
  98. parseConfig($)
  99. {
  100. my $configfile = shift;
  101. # we need default value to read included files
  102. $configfile = $configfile ? $configfile : 'fhem.cfg';
  103. my $fh;
  104. my $msg;
  105. my $ret;
  106. if (!open($fh,$configfile)) {
  107. $msg = "Can't open $configfile: $!";
  108. Log 1, "backup $msg";
  109. return $msg;
  110. }
  111. while (my $l = <$fh>) {
  112. $l =~ s/[\r\n]//g;
  113. if ($l =~ m/^\s*include\s+(\S+)\s*.*$/) {
  114. if (-e $1) {
  115. push @pathname, $1;
  116. Log 4, "backup include: '$1'";
  117. $ret = parseConfig($1);
  118. } else {
  119. Log 1, "backup configfile: '$1' does not exists! File not included."
  120. }
  121. }
  122. }
  123. close $fh;
  124. return $ret;
  125. }
  126. sub
  127. readModpath($$)
  128. {
  129. my ($modpath,$backupdir) = @_;
  130. my $msg;
  131. my $ret;
  132. if (!opendir(DH, $modpath)) {
  133. $msg = "Can't open $modpath: $!";
  134. Log 1, "backup $msg";
  135. return $msg;
  136. }
  137. my @files = <$modpath/*>;
  138. foreach my $file (@files) {
  139. if ($file eq $backupdir && (-d $file || -l $file)) {
  140. Log 4, "backup exclude: '$file'";
  141. } else {
  142. Log 4, "backup include: '$file'";
  143. push @pathname, $file;
  144. }
  145. }
  146. return $ret;
  147. }
  148. sub
  149. createArchiv($$$)
  150. {
  151. my ($backupdir,$cl,$byUpdate) = @_;
  152. my $backupcmd = (!defined($attr{global}{backupcmd}) ? undef : $attr{global}{backupcmd});
  153. my $symlink = (!defined($attr{global}{backupsymlink}) ? "no" : $attr{global}{backupsymlink});
  154. my $tarOpts;
  155. my $msg;
  156. my $ret;
  157. my $dateTime = TimeNow();
  158. $dateTime =~ s/ /_/g;
  159. $dateTime =~ s/(:|-)//g;
  160. my $pathlist = join( "\" \"", @pathname );
  161. my $cmd="";
  162. if (!defined($backupcmd)) {
  163. if (lc($symlink) eq "no") {
  164. $tarOpts = "cf";
  165. } else {
  166. $tarOpts = "chf";
  167. }
  168. # prevents tar's output of "Removing leading /" and return total bytes of
  169. # archive
  170. $cmd = "tar -$tarOpts - \"$pathlist\" |gzip > $backupdir/FHEM-$dateTime.tar.gz";
  171. } else {
  172. $cmd = "$backupcmd \"$pathlist\"";
  173. }
  174. Log 2, "Backup with command: $cmd";
  175. if(!$fhemForked && !$byUpdate) {
  176. use Blocking;
  177. our $BC_telnetDevice;
  178. BC_searchTelnet("backup");
  179. my $tp = $defs{$BC_telnetDevice}{PORT};
  180. system("($cmd; echo Backup done;".
  181. "$^X $0 localhost:$tp 'trigger global backup done')2>&1 &");
  182. return "Started the backup in the background, watch the log for details";
  183. }
  184. $ret = `($cmd) 2>&1`;
  185. if($ret) {
  186. chomp $ret;
  187. Log 1, "backup $ret";
  188. }
  189. if (!defined($backupcmd) && -e "$backupdir/FHEM-$dateTime.tar.gz") {
  190. my $size = -s "$backupdir/FHEM-$dateTime.tar.gz";
  191. $msg = "backup done: FHEM-$dateTime.tar.gz ($size Bytes)";
  192. DoTrigger("global", $msg);
  193. Log 1, $msg;
  194. $ret .= "\n".$msg;
  195. }
  196. return $ret;
  197. }
  198. 1;
  199. =pod
  200. =item command
  201. =item summary create a backup of the FHEM installation
  202. =item summary_DE erzeugt eine Sicherungsdatei der FHEM Installation
  203. =begin html
  204. <a name="backup"></a>
  205. <h3>backup</h3>
  206. <ul>
  207. <code>backup</code><br>
  208. <br>
  209. The complete FHEM directory (containing the modules), the WebInterface
  210. pgm2 (if installed) and the config-file will be saved into a .tar.gz
  211. file by default. The file is stored with a timestamp in the
  212. <a href="#modpath">modpath</a>/backup directory or to a directory
  213. specified by the global attribute <a href="#backupdir">backupdir</a>.<br>
  214. Note: tar and gzip must be installed to use this feature.
  215. <br>
  216. <br>
  217. If you need to call tar with support for symlinks, you could set the
  218. global attribute <a href="#backupsymlink">backupsymlink</a> to everything
  219. else as "no".
  220. <br>
  221. <br>
  222. You could pass the backup to your own command / script by using the
  223. global attribute <a href="#backupcmd">backupcmd</a>.
  224. <br>
  225. <br>
  226. </ul>
  227. =end html
  228. =cut