spacewalk/setup/lib/Spacewalk/Setup.pm | 11 ++-- web/modules/dobby/Dobby/CLI/BackupCommands.pm | 60 +++++++++++++++++++++++++- web/modules/dobby/Dobby/CLI/MiscCommands.pm | 11 +++- web/modules/dobby/scripts/db-control | 16 +++++- 4 files changed, 86 insertions(+), 12 deletions(-)
New commits: commit 4c7335370a79bcc371d664470021520adeeec91a Author: Miroslav Suchý msuchy@redhat.com Date: Mon Oct 22 16:02:43 2012 +0200
implement on-line backup and restore on PG
diff --git a/spacewalk/setup/lib/Spacewalk/Setup.pm b/spacewalk/setup/lib/Spacewalk/Setup.pm index 39814d9..9dce806 100644 --- a/spacewalk/setup/lib/Spacewalk/Setup.pm +++ b/spacewalk/setup/lib/Spacewalk/Setup.pm @@ -8,7 +8,7 @@ use English;
use Exporter 'import'; use vars '@EXPORT_OK'; -@EXPORT_OK = qw(loc system_debug system_or_exit); +@EXPORT_OK = qw(loc system_debug system_or_exit postgresql_clear_db);
use Getopt::Long qw(GetOptions); use Symbol qw(gensym); @@ -932,7 +932,8 @@ sub postgresql_populate_db {
if ($opts->{"clear-db"}) { print Spacewalk::Setup::loc("** Database: --clear-db option used. Clearing database.\n"); - postgresql_clear_db($answers); + my $dbh = get_dbh($answers); + postgresql_clear_db($dbh); }
if (postgresql_test_db_schema($answers)) { @@ -946,7 +947,8 @@ sub postgresql_populate_db {
if ($answers->{"clear-db"} =~ /Y/i) { print Spacewalk::Setup::loc("** Database: Clearing database.\n"); - postgresql_clear_db($answers); + my $dbh = get_dbh($answers); + postgresql_clear_db($dbh); print Spacewalk::Setup::loc("** Database: Re-populating database.\n"); } else { @@ -1015,7 +1017,7 @@ my $POSTGRESQL_CLEAR_SCHEMA = <<EOS; create schema public authorization postgres ; EOS sub postgresql_clear_db { - my $answers = shift; + my $dbh = shift;
print loc("** Database: Shutting down spacewalk services that may be using DB.\n");
@@ -1024,7 +1026,6 @@ sub postgresql_clear_db {
print loc("** Database: Services stopped. Clearing DB.\n");
- my $dbh = get_dbh($answers); local $dbh->{RaiseError} = 0; local $dbh->{PrintError} = 1; local $dbh->{PrintWarn} = 0; diff --git a/web/modules/dobby/Dobby/CLI/BackupCommands.pm b/web/modules/dobby/Dobby/CLI/BackupCommands.pm index ceb2a0f..3147439 100644 --- a/web/modules/dobby/Dobby/CLI/BackupCommands.pm +++ b/web/modules/dobby/Dobby/CLI/BackupCommands.pm @@ -19,6 +19,7 @@ package Dobby::CLI::BackupCommands; use Carp; use Dobby::Files; use Dobby::BackupLog; +use English; use File::Basename qw/basename dirname/; use File::Spec; use Filesys::Df; @@ -27,7 +28,7 @@ use POSIX; use Dobby::DB; use Dobby::Reporting; use Dobby::CLI::MiscCommands; - +use Spacewalk::Setup qw/postgresql_clear_db /;
sub register_dobby_commands { my $class = shift; @@ -45,6 +46,13 @@ sub register_dobby_commands { $cli->register_mode(-command => "examine", -description => "Display information about an RHN Oracle Instance backup", -handler => &command_restore); + $cli->register_mode(-command => "pg-online-backup", + -description => "Perform online backup the RHN PostgreSQL database", + -handler => &command_pg_online_backup); + $cli->register_mode(-command => "pg-restore", + -description => "Restore the RHN PostgreSQL database made by pg-online-backup", + -handler => &command_pg_restore); + }
# returns $file cuted of prefix made from $cut_off_dir @@ -305,4 +313,54 @@ sub command_restore { return 0; }
+sub command_pg_online_backup { + my ($cli, $command, $file) = @_; + $cli->usage("FILE") unless $file; + + my $backup_dir = dirname $file; + + my $backend = PXT::Config->get('db_backend'); + $cli->fatal("Error: This backup method works only with PostgreSQL.") unless ($backend eq 'postgresql'); + my $cfg = new PXT::Config("dobby"); + my @rec = getpwnam($cfg->get("postgresql_user")); + $EUID = $rec[2]; + $cli->fatal("Error: $backup_dir is not a writable directory for user $rec[0].") unless -d $backup_dir and -w $backup_dir; + $cli->fatal("Error: Backup file $file already exists in $file.") if -f $file; + + print "Backing up to file $file.\n"; + my $ret = system("/usr/bin/pg_dump", "--blobs", "--clean", "-Fc", "-v", "-Z7", "--file=$file", PXT::Config->get('db_name')); + print "Backup complete.\n"; + return $ret; +} + +sub command_pg_restore { + my ($cli, $command, $file) = @_; + $cli->usage("FILE") unless $file; + + $cli->fatal("Error: restoration failed, unable to locate $file") unless -r $file; + + my $backend = PXT::Config->get('db_backend'); + $cli->fatal("Error: This backup method works only with PostgreSQL.") unless ($backend eq 'postgresql'); + my $cfg = new PXT::Config("dobby"); + my @rec = getpwnam($cfg->get("postgresql_user")); + $EUID = $rec[2]; + $cli->fatal("Error: file $file is not readable by user $rec[0]") unless -r $file; + + my $user = PXT::Config->get("db_user"); + my $password = PXT::Config->get("db_password"); + my $dsn = "dbi:Pg:dbname=".PXT::Config->get("db_name"); + my $dbh = RHN::DB->direct_connect($dsn); + + no warnings 'redefine'; + sub Spacewalk::Setup::system_debug { + system @_; + } + postgresql_clear_db($dbh); + + print "** Restoring from file $file.\n"; + my $ret = system("/usr/bin/pg_restore", "-Fc", "--jobs=2", "--dbname=".PXT::Config->get('db_name'), $file ); + print "Restoration complete.\n"; + return $ret; +} + 1; diff --git a/web/modules/dobby/scripts/db-control b/web/modules/dobby/scripts/db-control index e64b366..09fb555 100755 --- a/web/modules/dobby/scripts/db-control +++ b/web/modules/dobby/scripts/db-control @@ -97,6 +97,14 @@ Gather statistics on database objects. On Oracle PCT is the percentage of rows t
Gives a list of db-control options.
+=item B<pg-online-backup FILE> + +Perform online backup of database. This works only with PostgreSQL backend and FILE must be in directory writeable by postgres user. While this backup method is slower compared to "backup" (aprox. 4 times). You do not need to shut down neither Spacewalk services nor DB service. + +=item B<pg-restore FILE> + +Restores the database from backup file FILE, which was made by command "pg-online-backup". This command will shut down all Spacewalk services and leave only database running. Then all objects from DB are removed and backup is restored. You have to start up Spacewalk services manualy. + =item B<report>
Reports on current usage of database space.
commit 5338ecd9abcddde4f90bc9223256090c232433c4 Author: Miroslav Suchý msuchy@redhat.com Date: Mon Oct 22 16:01:49 2012 +0200
805822 - edit man page to include PostgreSQL specific commands
diff --git a/web/modules/dobby/scripts/db-control b/web/modules/dobby/scripts/db-control index 6bf24cf..e64b366 100755 --- a/web/modules/dobby/scripts/db-control +++ b/web/modules/dobby/scripts/db-control @@ -91,7 +91,7 @@ Increases the tablespace size for TABLESPACE by adding an additional datafile.
=item B<gather-stats PCT>
-Gather statistics on database objects. PCT is the percentage of rows to estimate (the default is 15 per cent). +Gather statistics on database objects. On Oracle PCT is the percentage of rows to estimate (the default is 15 per cent). On PostgreSQL PCT does not have sense.
=item B<help>
@@ -101,9 +101,9 @@ Gives a list of db-control options.
Reports on current usage of database space.
-=item B<report-stats> +=item B<report-stats DAYS>
-Reports tables with stale or empty statistics. +Reports tables with stale or empty statistics. On Oracle DAYS are ignored. On PostgreSQL shows tables, which has not been ANALYZED (gather-stats do that) for DAYS days. Default is one day.
=item B<restore DIRNAME>
@@ -129,7 +129,7 @@ Stops the RHN Satellite database instance. This is required to perform B<backup
=item B<tablesizes>
-Show space report for each table. +Show space report for each table. On PostgreSQL the meaning of columns is as follows: "Planner Size" - size which planner usefor determining execution plan. It may differ from real size. "Real Size" - real size of data file on disk. "Real Size+Toasts+Idx" - reali size of data file on disk, including TOAST (The Oversized-Attribute Storage Technique) and indexes.
=item B<verify DIRNAME>
commit 7abab3ecd6f92a484645c0b4ca639723bb552bf9 Author: Miroslav Suchý msuchy@redhat.com Date: Mon Oct 22 16:01:03 2012 +0200
663315 - wait until database is really offline
diff --git a/web/modules/dobby/Dobby/CLI/MiscCommands.pm b/web/modules/dobby/Dobby/CLI/MiscCommands.pm index dc5e51a..1476110 100644 --- a/web/modules/dobby/Dobby/CLI/MiscCommands.pm +++ b/web/modules/dobby/Dobby/CLI/MiscCommands.pm @@ -86,10 +86,17 @@ sub command_startstop { if ($backend eq 'postgresql') { system("/sbin/service", "postgresql", "stop"); } else { - print "Shutting down database... "; + print "Shutting down database"; $d->listener_shutdown; $d->database_shutdown("immediate"); - print "done.\n"; + my $i = 5; + $| = 1; + while (($i-- > 0) and ($d->instance_state ne 'OFFLINE')) { + sleep 1; + print "."; + } + my $s = ($d->instance_state eq 'OFFLINE') ? " done.\n" : " fail.\n"; + print $s; } } }
spacewalk-commits@lists.fedorahosted.org