#!/usr/bin/perl
use strict; use warnings;

=pod

=head1 NAME

vox-backup-server - backup a vote-server

=head1 SYNOPSIS

> vox-backup-wiki

> vox-backup-server VS

=head1 DESCRIPTION

This would normally back up both the database and filebase, but currently it only backs up
the database.  It must be run after vox-backup-wiki, which creates the backup directory.
There the following file is created, where VS is the name of the vote-server account.

  /var/cache/pollwiki/backup/cur/VS-dump.sql

=head1 RESTORING

=over

=item 1

> (vS=VS; createdb --owner=$vS --template=template0 $vS)

where VS is the name of the vote-server account

=item 2

> (vS=VS; psql --file=/var/cache/pollwiki/backup/cur/$vS-dump.sql --output=/tmp/psql-out-$vS.txt --single-transaction $vS)

=item 3

VS> psql

Manually drop any restored vote-server mounts.  Do this only if the ~/votorola/out
directory of the filebase was not restored.  Enter the following commands in the psql
database shell:

  VS@psql> \dn

That command lists all of the schemata.  Drop any that are named out_*:

  VS@psql> DROP SCHEMA out_count CASCADE;

  VS@psql> DROP SCHEMA out_trace CASCADE;

=item 4

> vacuumdb --all --analyze

If other databases are to be restored, then do the vacuuming last.

=back

=cut


    use Pod::Usage qw( pod2usage );
    my $vS = shift; defined $vS or pod2usage( -verbose => 1 ); # and exits
    my $backDirCur = "/var/cache/pollwiki/backup/cur";
    -d $backDirCur or die "missing backup directory: $backDirCur\ndid you run vox-backup-wiki first?\n";

    my $dbFile = "$backDirCur/$vS-dump.sql";
    -f $dbFile and die "backup already exists: $dbFile\ndid you run vox-backup-wiki first?\n";

   # Filebase.  (later it's not crucial for alpha testing)
   # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   # Database.
   # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    my $command = "nice pg_dump $vS > $dbFile";
    print "   $command\n";
    system $command and die 'unable to execute: ' . $command;
    print "\n";