#!/usr/bin/perl -w
use strict;

use File::Basename;

use PXT::Config;
use Dobby::CLI;
use Dobby::CLI::MiscCommands;
use Dobby::CLI::MaintCommands;
use Dobby::CLI::BackupCommands;
use Dobby::Log;

$PXT::Config::default_config = new PXT::Config('dobby');

my $exec_name = basename $0;
my $cli = new Dobby::CLI $exec_name;

my $backend = PXT::Config->get('db_backend');
my $cfg = new PXT::Config("dobby");
my $intended_username = $cfg->get("${backend}_user");
my ($username, undef, $uid, $gid) = getpwnam($intended_username);

if (not defined $uid or not defined $gid or $uid != $> or $gid != $)) {
  if ($backend eq 'postgresql') {
    # postgresql backend can be run as root or postgres user
    if ( $> != 0 or $) != 0) {
      $cli->fatal("Error: please run this command as the root ('su -').");
    }
  } else { # oracle backend can be run only as oracle user
    no warnings 'syntax';
    exec '/sbin/runuser', '-', $intended_username, '-c', join ' ', map "'$_'", $0, @ARGV;
    $cli->fatal("Error: runuser $intended_username failed");
  }
}

Dobby::CLI::MiscCommands->register_dobby_commands($cli);
Dobby::CLI::MaintCommands->register_dobby_commands($cli);
Dobby::CLI::BackupCommands->register_dobby_commands($cli);

my ($mode, @params) = @ARGV;

# default to the 'help' command
if (not $mode) {
  $mode = "help";
  @params = ();
}

# force autoflush on stdout write
$|++;

Dobby::Log->log("Invocation: db-control $mode @params");
my $status = $cli->invoke_command($mode, @params);

exit $status || 0;

=head1 NAME

db-control - utility for managing the SUSE Manager database

=head1 SYNOPSIS

B<db-control> 
[B<backup DIRNAME>] 
[B<examine DIRNAME>]
[B<extend TABLESPACE>]
[B<gather-stats PCT>]
[B<get-optimizer>]
[B<help>]
[B<report>]
[B<report-stats>]
[B<restore DIRNAME>]
[B<reset-password>]
[B<set-optimizer>]
[B<shrink-segments>]
[B<start>]
[B<status>]
[B<stop>]
[B<tablesizes>]
[B<verify DIRNAME>]

=head1 OPTIONS

=over 5

=item B<backup DIRNAME>

Backs up the database.  Backup files will be stored in directory DIRNAME.  Note that this is a cold backup; the database must be stopped before running this command.  It's advisable to create a new backup directory for each new backup.

Backups take several minutes; the first backup will be a good indicator of how long subsequent backups will take.

=item B<examine DIRNAME>

Examines the contents of a backup directory.  Quicker than B<verify>, this command returns the timestamp of backup creation and reports on its contents, noting any missing files.

=item B<extend TABLESPACE>

Increases the tablespace size for TABLESPACE by adding an additional datafile. (Oracle only)

=item B<gather-stats PCT>

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<get-optimizer>

Show database optimizer mode (Oracle only).

=item B<help>

Gives a list of db-control options.

=item B<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.  

=item B<report-stats DAYS>

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<reset-password>

Resets database password according to value in /etc/rhn/rhn.conf.

=item B<restore DIRNAME>

Restores the database from backup kept in DIRNAME.  Database must be stopped for this command to run successfully.

Before restoring, the files in DIRNAME are verified.  Once all files are verified successfully, the restore runs.  Once the restore is completed, the database may be started and run normally.

=item B<set-optimizer>

Set database optimizer mode to ALL_ROWS (Oracle only).

=item B<shrink-segments>

Determines and shrinks database segments with signifcant amount of free space.

=item B<start>

Starts the database instance.

=item B<status>

Shows the current status of the database, either "running" or "offline".

=item B<stop>

Stops the SUSE Manager database instance.  This is required to perform B<backup> or B<restore> actions.

=item B<tablesizes>

Show space report for each table. On PostgreSQL the meaning of columns is as follows: "Planner Size" - size which planner uses for determining execution plan. It may differ from real size. "Real Size" - real size of data file on disk. "Real Size+Toasts+Idx" - real size of data file on disk, including TOAST (The Oversized-Attribute Storage Technique) and indexes.

=item B<verify DIRNAME>

Verifies the contents of the backup kept in DIRNAME.  This command checks the md5sums of each of the files kept in the backup.

=back

=head1 SEE ALSO

B<rhn-charsets>(8), B<rhnpush>(8), B<rhn-satellite-activate>(8), B<rhn-schema-version>(8), B<rhn-ssl-dbstore>(8), B<satellite-sync>(8), B<satellite-debug>(8)

