| 1 | package WebEditor::OldFeatures::AdminExport; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/); | ||
| 6 | |||
| 7 | 0 | ▶ | use CGI qw(param); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Archive::Tar; | ||
| 9 | |||
| 10 | sub do_export { | ||
| 11 | my $self = shift; | ||
| 12 | my $c = $self->C; | ||
| 13 | |||
| 14 | my $tarfilename = "backup_".get_date().".tgz"; | ||
| 15 | my $tarfile = $c->paths->cgidir."/backup/".$tarfilename; | ||
| 16 | my @files; | ||
| 17 | my $file; | ||
| 18 | |||
| 19 | opendir DIR, $c->paths->database | ||
| 20 | or die "Can't open directory " . $c->paths->database . ": $!"; | ||
| 21 | while (defined($file = readdir(DIR))) { | ||
| 22 | next if $file =~ /^\.|CVS/i; # ignore hidden files | ||
| 23 | push @files, $file if not -d $c->paths->database."/".$file; | ||
| 24 | } | ||
| 25 | closedir DIR; | ||
| 26 | |||
| 27 | opendir DIR, $c->paths->database."/content" | ||
| 28 | or die "Can't open directory " . $c->paths->database . "/content: $!"; | ||
| 29 | while (defined($file = readdir(DIR))) { | ||
| 30 | next if $file =~ /^\.|CVS/i; # ignore hidden files | ||
| 31 | push @files, "content/".$file if not -d $c->paths->database."/content/".$file; | ||
| 32 | } | ||
| 33 | closedir DIR; | ||
| 34 | |||
| 35 | my $tar = Archive::Tar->new(); | ||
| 36 | chdir $c->paths->database or die "Can't chdir: $!"; | ||
| 37 | my $ok = $tar->add_files(@files); | ||
| 38 | $ok = $ok && $tar->add_data(".theanswer","42"); | ||
| 39 | if (!$tar->write($tarfile,9) || !$ok) { | ||
| 40 | print "kann Archivdatei nicht erzeugen: ".$tar->error; | ||
| 41 | } else { | ||
| 42 | print "Archivdatei \"$tarfilename\" erzeugt.<br>\n"; | ||
| 43 | #XXX kann man überhaupt auf dieses Verzeichnis zugreifen??? | ||
| 44 | print "Bitte <a href='" . $c->paths->cgiurl . "/backup/$tarfilename'>hier</a> herunterladen und gut aufbewahren.<br>\n"; | ||
| 45 | } | ||
| 46 | chdir $c->paths->cgidir; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub do_import { | ||
| 50 | my $self = shift; | ||
| 51 | my $c = $self->C; | ||
| 52 | |||
| 53 | if (!param('tarfile')) { | ||
| 54 | print qq~ | ||
| 55 | <form action="@{[ $c->paths->cgiurl ]}/we_redisys.cgi" method="POST" ENCTYPE="multipart/form-data"><table><tr> | ||
| 56 | <td>Backup-Datei: </td> | ||
| 57 | <td><input type=file name="tarfile"> | ||
| 58 | <input type=hidden name="goto" value="admin"> | ||
| 59 | <input type=hidden name="action" value="import"> | ||
| 60 | <td><input type=submit value="upload"></td> | ||
| 61 | </tr></table></form><br>~; | ||
| 62 | } else { | ||
| 63 | require Archive::Tar; | ||
| 64 | my $uploadfile = param('tarfile'); | ||
| 65 | my $tarfile = $c->paths->cgidir."/backup/upload.tgz"; | ||
| 66 | open(BACKUP,">$tarfile") or die "Can't write to $tarfile: $!"; | ||
| 67 | binmode BACKUP; | ||
| 68 | while (<$uploadfile>) { | ||
| 69 | print BACKUP $_; | ||
| 70 | } | ||
| 71 | close BACKUP; | ||
| 72 | print "File-upload abgeschlossen. $tarfile<br>"; | ||
| 73 | my $tar = Archive::Tar->new(); | ||
| 74 | $tar->read($tarfile); | ||
| 75 | if ($tar->get_content(".theanswer") =~ "42") { | ||
| 76 | $tar->remove(".theanswer"); | ||
| 77 | chdir $c->paths->database | ||
| 78 | or die "Can't chdir to " . $c->paths->database . ": $!"; | ||
| 79 | if (!$tar->extract_archive($tarfile)) { | ||
| 80 | print "geht nich: " . $tar->error(); | ||
| 81 | } else { | ||
| 82 | print "Datenfiles extrahiert.<br>"; | ||
| 83 | }; | ||
| 84 | } else { | ||
| 85 | print "<font color=red>Dies scheint keine gültige Backup-datei zu sein!</font><br>\n"; | ||
| 86 | print "Es wurden keine Dateien extrahiert.<br>\n"; | ||
| 87 | } | ||
| 88 | chdir $c->paths->cgidir; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | #################################################################### | ||
| 93 | # | ||
| 94 | # general subs | ||
| 95 | # | ||
| 96 | sub get_date { | ||
| 97 | # Return time as ISO 8601 date from given time in days | ||
| 98 | my $time = time; | ||
| 99 | my @l = localtime $time; | ||
| 100 | sprintf("%04d-%02d-%02d-%02d%02d", | ||
| 101 | $l[5]+1900, $l[4]+1, $l[3], | ||
| 102 | $l[2], $l[1], $l[0]); | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; | ||