[postgis-users] postgis_restore.pl patch to allow psql options

James Marca jmarca at translab.its.uci.edu
Tue May 17 14:23:45 PDT 2005


Hi,

Appended is a second (of three) patch to postgis_restore.pl.  This
patch copies the brief description at the top of the script into pod
format (at the bottom), and inserts pod2usage() calls in place of the
various "die" commands.

James E. Marca

--- postgis-1.0.0/utils/postgis_restore.pl.usestrict	2005-05-16 15:59:22.000000000 -0700
+++ postgis-1.0.0/utils/postgis_restore.pl	2005-05-17 14:15:58.000000000 -0700
@@ -1,4 +1,4 @@
-#/bin/sh
+#!/usr/bin/perl -w
 #
 # This script is aimed at restoring postgis data
 # from a dumpfile produced by pg_dump -Fc
@@ -37,10 +37,13 @@
 #	pg_dump-800/pg800 => pg_restore-800/pg800
 #
 use strict;
-eval "exec perl -w $0 $@"
-	if (0);
+use warnings;
+use Pod::Usage;
 
-(@ARGV == 3) || die "Usage: postgis_restore.pl <postgis.sql> <db> <dump>\nRestore a custom dump (pg_dump -Fc) of a postgis enabled database.\n";
+# eval "exec perl -w $0 $@"
+# 	if (0);
+
+(@ARGV == 3) || pod2usage(-exitstatus => 0, -verbose => 2);
 
 my $DEBUG=1;
 
@@ -108,11 +111,19 @@ print "postgis.sql is $postgissql\n";
 print "dbname is $dbname\n";
 print "dumpfile is $dump\n";
 
+# form the various client program commands, for use later
+
+my $pg_restore_command= "pg_restore ";
+my $createdb_command = "createdb  ";
+my $createlang_command = "createlang   ";
+my $psql_command = "psql  ";
+
 #
 # Scan postgis.sql
 #
 print "Scanning $postgissql\n"; 
-open( INPUT, $postgissql ) || die "Couldn't open file: $postgissql\n";
+open( INPUT, $postgissql ) || pod2usage(-exitval => 2, -verbose => 0,
+					-msg => "Couldn't open file: $postgissql for reading\n") ; 
 while( my $line = <INPUT>)
 {
 	$line =~ s/[\r\n]//g;
@@ -302,8 +313,10 @@ close( INPUT );
 # Scan dump list
 #
 print "Scanning $dump list\n"; 
-open( OUTPUT, ">$dumplist") || die "Can't write to ".$dump.".list\n";
-open( INPUT, "pg_restore -l $dump |") || die "Couldn't run pg_restore -l $dump\n";
+open( OUTPUT, ">$dumplist") || pod2usage(-exitval => 2, -verbose => 0,
+					 -msg =>  "Can't write to $dumplist\n");
+open( INPUT, "$pg_restore_command  -l $dump |") || pod2usage(-exitval => 2, -verbose => 0,
+					 -msg => "Couldn't run $pg_restore_command -l $dump\n");
 while( my $line = <INPUT> )
 {
 	next if $line =~ /^;/;
@@ -532,8 +545,10 @@ close( INPUT );
 close(OUTPUT);
 
 print "Producing ascii dump $dumpascii\n"; 
-open( INPUT, "pg_restore -L $dumplist $dump |") || die "Can't run pg_restore\n";
-open( OUTPUT, ">$dumpascii") || die "Can't write to $dumpascii\n";
+open( INPUT, "$pg_restore_command -L $dumplist $dump |") || pod2usage(-exitval => 2, -verbose => 0,
+								      -msg => "Can't run $pg_restore_command -L $dumplist $dump\n");
+open( OUTPUT, ">$dumpascii") || pod2usage(-exitval => 2, -verbose => 0,
+					  -msg =>  "Can't write to $dumpascii.  \nCheck that the disk isn't full, and that you have permission to write to the current working directory\n");
 while( my $line = <INPUT> )
 {
 	next if $line =~ /^ *--/;
@@ -594,22 +609,27 @@ close(OUTPUT);
 #
 # Create the new db and install plpgsql language
 #
+
 print "Creating db ($dbname)\n";
-`createdb $dbname`;
-die "Can't restore in an existing database\n" if ($?);
+`$createdb_command $dbname`;
+pod2usage(-exitval => 2, -verbose => 0,
+	  -msg => "Can't create database $dbname.  \nCheck that the dbuser is correct, has permission to create tables, and that the database doesn't already exist\n" ) if ($?);
+
 print "Adding plpgsql\n";
-`createlang plpgsql $dbname`;
+`$createlang_command plpgsql $dbname`;
 
 #
 # Open a pipe to the SQL monitor
 #
-open( PSQL, "| psql -a $dbname") || die "Can't run psql\n";
+open( PSQL, "| $psql_command -a $dbname") || pod2usage(-exitval => 2, -verbose => 0,
+						       -msg => "Can't run $psql_command -a $dbname \nCheck that the database user is correct, and that the hostname and port are correct\n");
 
 #
 # Source new postgis.sql
 #
 print "Sourcing $postgissql\n";
-open(INPUT, "<$postgissql") || die "Can't read $postgissql\n";
+open(INPUT, "<$postgissql") || pod2usage(-exitval => 2, -verbose => 0,
+					 -msg => "Couldn't open file: $postgissql for reading, which is strange because it was read successfully earlier in this script.\n");
 while(<INPUT>) { print PSQL; }
 close(INPUT);
 
@@ -628,7 +648,130 @@ print PSQL "DROP TABLE spatial_ref_sys;"
 # Source modified ascii dump
 #
 print "Restoring ascii dump $dumpascii\n";
-open(INPUT, "<$dumpascii") || die "Can't read $postgissql\n";
+open(INPUT, "<$dumpascii") || pod2usage(-exitval => 2, -verbose => 0,
+					 -msg => "Can't read $dumpascii, which is strange because it was just written earlier in this script.  Perhaps the disk is full?\n");
 while(<INPUT>) { print PSQL; }
 close(INPUT);
 close(PSQL);
+
+
+1;  # all done
+
+__END__
+
+=head1 NAME
+
+postgis_restore.pl -- a script to restoring postgis data from a
+dumpfile produced by pg_dump -Fc
+
+=head1 SYNOPSIS
+
+  postgis_restore.pl [options] [postgis.sql] [db] [dump] 
+
+Arguments:
+
+  postgis.sql --- the new version of PostGIS SQL commands you wish
+                  to use
+  db --- the new database name.  This cannot be an existing database
+  dump --- the dump file created by pg_dump
+
+=head1 ARGUMENTS
+
+=over 
+
+=item B<postgis.sql>
+
+When upgrading to a new version of PostGIS, this is the file that
+contains the new PostGIS functions.  For example, in PostGIS 1.0.0,
+the file is called lwpostgis.sql.
+
+=item B<db>
+
+A database name for the database that will be created by this script.
+Note that this script will fail if the database name already exists,
+but you will have to wait around while the dump file is processed
+first.  If you want to reuse an old database name, run the dropdb
+program before running this script.  But be aware that dropdb really
+B<drop>s your database, and there is no undo_mistake program!
+
+=item B<dump>
+
+The 'custom' dump file created by pg_dump.  Given a database called
+'olddb', one might run the following:
+
+    pg_dump -U postgres -W -f olddb.dump -Fc olddb
+
+to get a dump file called olddb.dump
+
+=back
+
+=head1 DESCRIPTION
+
+B<postgis_restore.pl> is aimed at restoring postgis data from a
+dumpfile produced by pg_dump -Fc
+
+Basically it will restore all but things created by the given
+postgis.sql.  Before restoring, it will create and postgis-enable the
+target db.
+
+Particular attention must be given to the spatial_ref_sys and
+geometry_columns tables which are created and populated from the dump,
+not the postgis.sql file. When the new installation is against
+pgsql7.5+ and dump from pre7.5 this script should probably drop
+statistic fields from that table.  This is currently not done.
+
+=head1 ISSUES
+
+=over 
+
+=item B<OBSOLETED FUNCTIONS BEHAVIOR>
+
+Some obsoleted functions would not be present in the postgis.sql, but
+will be found in the dump. Currently some are skipped, but some depend
+on the pg version so will issue an ERROR due to unavailability of
+corresponding C function in postgis lib.
+
+=item B<LESS IS MORE?>
+
+This script could do less than it does, to allow users to further
+modify the edited dump before feeding it to the restoring side.
+
+=back
+
+
+=head1 TESTED ON:
+
+The original code was tested on the following platforms:
+
+=over
+
+=item pg_dump-734/pg734 => pg_restore-743/pg743
+
+=item pg_dump-743/pg734 => pg_restore-743/pg743
+
+=item pg_dump-743/pg743 => pg_restore-743/pg743
+
+=item pg_dump-734/pg734 => pg_restore-800/pg800
+
+=item pg_dump-743/pg734 => pg_restore-800/pg800
+
+=item pg_dump-743/pg743 => pg_restore-800/pg800
+
+=item pg_dump-800/pg800 => pg_restore-800/pg800
+
+=back
+
+Note that this version, which adds pod, pod2usage calls, and modifies
+some small pieces of logic, has not been tested on these platforms.
+
+=head1 AUTHOR
+
+Unknown?  Maintained by staff of Refractions Research.  
+
+=head1 AVAILABILITY
+
+Included with PostGIS distribution.  The best place to discuss
+this script or to get help is on the PostGIS mailing list,
+L<http://postgis.refractions.net/pipermail/postgis-users/>
+
+=cut





More information about the postgis-users mailing list