Revision 4e0ea59f
Von Moritz Bunkus vor mehr als 17 Jahren hinzugefügt
bin/mozilla/admin.pl | ||
---|---|---|
36 | 36 |
|
37 | 37 |
use DBI; |
38 | 38 |
use CGI; |
39 |
use POSIX qw(strftime); |
|
40 |
use IO::File; |
|
41 |
use Fcntl; |
|
42 |
use English qw(-no_match_vars); |
|
43 |
use Sys::Hostname; |
|
39 | 44 |
|
40 | 45 |
use SL::Form; |
46 |
use SL::Mailer; |
|
41 | 47 |
use SL::User; |
42 | 48 |
use SL::Common; |
43 | 49 |
use SL::Inifile; |
... | ... | |
562 | 568 |
|
563 | 569 |
$form->{title} = "Lx-Office ERP / " . $locale->text('Database Administration'); |
564 | 570 |
|
571 |
$form->{ALLOW_DBBACKUP} = "$pg_dump_exe" ne "DISABLED"; |
|
572 |
|
|
565 | 573 |
$form->header(); |
566 | 574 |
print $form->parse_html_template("admin/dbadmin"); |
567 | 575 |
} |
... | ... | |
693 | 701 |
"Lx-Office ERP " |
694 | 702 |
. $locale->text('Database Administration') . " / " |
695 | 703 |
. $locale->text('Delete Dataset'); |
696 |
|
|
697 | 704 |
$form->header(); |
698 | 705 |
print $form->parse_html_template("admin/dbdelete"); |
699 | 706 |
} |
700 | 707 |
|
708 |
sub backup_dataset { |
|
709 |
$form->{title} = |
|
710 |
"Lx-Office ERP " |
|
711 |
. $locale->text('Database Administration') . " / " |
|
712 |
. $locale->text('Backup Dataset'); |
|
713 |
|
|
714 |
if ("$pg_dump_exe" eq "DISABLED") { |
|
715 |
$form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.')); |
|
716 |
} |
|
717 |
|
|
718 |
my @dbsources = sort User->dbsources($form); |
|
719 |
$form->{DATABASES} = [ map { { "dbname" => $_ } } @dbsources ]; |
|
720 |
$form->{NO_DATABASES} = !scalar @dbsources; |
|
721 |
|
|
722 |
my $username = getpwuid $UID || "unknown-user"; |
|
723 |
my $hostname = hostname() || "unknown-host"; |
|
724 |
$form->{from} = "Lx-Office Admin <${username}\@${hostname}>"; |
|
725 |
|
|
726 |
$form->header(); |
|
727 |
print $form->parse_html_template("admin/backup_dataset"); |
|
728 |
} |
|
729 |
|
|
730 |
sub backup_dataset_start { |
|
731 |
$form->{title} = |
|
732 |
"Lx-Office ERP " |
|
733 |
. $locale->text('Database Administration') . " / " |
|
734 |
. $locale->text('Backup Dataset'); |
|
735 |
|
|
736 |
$pg_dump_exe ||= "pg_dump"; |
|
737 |
|
|
738 |
if ("$pg_dump_exe" eq "DISABLED") { |
|
739 |
$form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.')); |
|
740 |
} |
|
741 |
|
|
742 |
$form->isblank("dbname", $locale->text('The dataset name is missing.')); |
|
743 |
$form->isblank("to", $locale->text('The email address is missing.')) if $form->{destination} eq "email"; |
|
744 |
|
|
745 |
my $tmpdir = "/tmp/lx_office_backup_" . Common->unique_id(); |
|
746 |
mkdir $tmpdir, 0700 || $form->error($locale->text('A temporary directory could not be created:') . " $!"); |
|
747 |
|
|
748 |
my $pgpass = IO::File->new("${tmpdir}/.pgpass", O_WRONLY | O_CREAT, 0600); |
|
749 |
|
|
750 |
if (!$pgpass) { |
|
751 |
unlink $tmpdir; |
|
752 |
$form->error($locale->text('A temporary file could not be created:') . " $!"); |
|
753 |
} |
|
754 |
|
|
755 |
print $pgpass "$form->{dbhost}:$form->{dbport}:$form->{dbname}:$form->{dbuser}:$form->{dbpasswd}\n"; |
|
756 |
$pgpass->close(); |
|
757 |
|
|
758 |
$ENV{HOME} = $tmpdir; |
|
759 |
|
|
760 |
my @args = ("-c", "-o", "-h", $form->{dbhost}, "-U", $form->{dbuser}); |
|
761 |
push @args, ("-p", $form->{dbport}) if ($form->{dbport}); |
|
762 |
push @args, $form->{dbname}; |
|
763 |
|
|
764 |
my $cmd = "${pg_dump_exe} " . join(" ", map { s/\\/\\\\/g; s/\"/\\\"/g; $_ } @args); |
|
765 |
my $name = "dataset_backup_$form->{dbname}_" . strftime("%Y%m%d", localtime()) . ".sql.gz"; |
|
766 |
|
|
767 |
if ($form->{destination} ne "email") { |
|
768 |
my $in = IO::File->new("$cmd |"); |
|
769 |
|
|
770 |
if (!$in) { |
|
771 |
unlink "${tmpdir}/.pgpass"; |
|
772 |
rmdir $tmpdir; |
|
773 |
|
|
774 |
$form->error($locale->text('The pg_dump process could not be started.')); |
|
775 |
} |
|
776 |
|
|
777 |
print "content-type: application/octet-stream\n"; |
|
778 |
print "content-disposition: attachment; filename=\"${name}\"\n\n"; |
|
779 |
|
|
780 |
while (my $line = <$in>) { |
|
781 |
print $line; |
|
782 |
} |
|
783 |
|
|
784 |
$in->close(); |
|
785 |
|
|
786 |
unlink "${tmpdir}/.pgpass"; |
|
787 |
rmdir $tmpdir; |
|
788 |
|
|
789 |
} else { |
|
790 |
my $tmp = $tmpdir . "/dump_" . Common::unique_id(); |
|
791 |
|
|
792 |
if (system("$cmd > $tmp") != 0) { |
|
793 |
unlink "${tmpdir}/.pgpass", $tmp; |
|
794 |
rmdir $tmpdir; |
|
795 |
|
|
796 |
$form->error($locale->text('The pg_dump process could not be started.')); |
|
797 |
} |
|
798 |
|
|
799 |
my $mail = new Mailer; |
|
800 |
|
|
801 |
map { $mail->{$_} = $form->{$_} } qw(from to cc subject message); |
|
802 |
|
|
803 |
$mail->{charset} = $dbcharset ? $dbcharset : Common::DEFAULT_CHARSET; |
|
804 |
$mail->{attachments} = [ { "filename" => $tmp, "name" => $name } ]; |
|
805 |
$mail->send(); |
|
806 |
|
|
807 |
unlink "${tmpdir}/.pgpass", $tmp; |
|
808 |
rmdir $tmpdir; |
|
809 |
|
|
810 |
$form->{title} = |
|
811 |
"Lx-Office ERP " |
|
812 |
. $locale->text('Database Administration') . " / " |
|
813 |
. $locale->text('Backup Dataset'); |
|
814 |
|
|
815 |
$form->header(); |
|
816 |
print $form->parse_html_template("admin/backup_dataset_email_done"); |
|
817 |
} |
|
818 |
} |
|
819 |
|
|
820 |
sub restore_dataset { |
|
821 |
$form->{title} = |
|
822 |
"Lx-Office ERP " |
|
823 |
. $locale->text('Database Administration') . " / " |
|
824 |
. $locale->text('Restore Dataset'); |
|
825 |
|
|
826 |
if ("$pg_dump_exe" eq "DISABLED") { |
|
827 |
$form->error($locale->text('Database backups and restorations are disabled in lx-erp.conf.')); |
|
828 |
} |
|
829 |
} |
|
830 |
|
|
701 | 831 |
sub unlock_system { |
702 | 832 |
|
703 | 833 |
unlink "$userspath/nologin"; |
Auch abrufbar als: Unified diff
Beim Administrationsfrontend einen Punkt eingebaut, mit dem man Datenbanken mittels pg_dump sichern kann. Das Ergebnis wird ge-gzipt und kann heruntergeladen oder direkt per Email verschickt werden.