- 1 year ago
perl script that does a pg_dumpall of postgres
Here's an example of a Perl script that uses the pg_dumpall
utility to perform a full database cluster backup for PostgreSQL:
perl#!/usr/bin/perl
use strict;
use warnings;
my $username = 'your_username';
my $password = 'your_password';
my $output_file = 'backup.sql';
# Build the pg_dumpall command
my $pg_dumpall_cmd = "pg_dumpall -U $username -w -f $output_file";
# Execute the pg_dumpall command
system($pg_dumpall_cmd);
if ($? == -1) {
print "Failed to execute pg_dumpall: $!\n";
} elsif ($? >> 8 != 0) {
print "pg_dumpall exited with non-zero status: ", $? >> 8, "\n";
} else {
print "Backup completed successfully.\n";
}
Replace 'your_username'
and 'your_password'
with your PostgreSQL username and password. Adjust the 'backup.sql'
to specify the name of the backup file you want to create.
Save the script to a .pl
file, make it executable, and then run it. The script uses the system()
function to execute the pg_dumpall
command, and it checks the exit status of the command to determine if the backup was successful.
Remember that storing passwords in plaintext within scripts is not secure. For production environments, consider using other authentication methods, such as password files or environment variables, to securely provide the required credentials.