[GENERAL] Automated Backup Script Help (Linux)

2012-03-07 Thread Carlos Mennens
I'm trying to get the automated backup scripts to work from:

http://wiki.postgresql.org/wiki/Automated_Backup_on_Linux

Currently I'm using PostgreSQL 9.1.3 and have the following three files:

carlos@db1:~/postgresql$ ls -l
total 20
drwxr-xr-x 2 carlos users 4096 Mar  7 13:13 backup
-rw-r--r-- 1 carlos users 1434 Mar  7 13:19 pg_backup.config
-rw-r--r-- 1 carlos users 4304 Mar  7 12:54 pg_backup_rotated.sh
-rw-r--r-- 1 carlos users 3379 Mar  7 12:54 pg_backup.sh

My Linux shell user 'carlos' has rwx to the entire directory as well
as the database role 'carlos' is listed as a REPLICATION role:

postgres=# \du
 List of roles
 Role name |   Attributes
 | Member of
---++---
 carlos| Superuser, No inheritance, Create role, Create DB,
Replication | {it}

Now when I attempt to run the script for the 1st time, I get the
following error:

carlos@db1:~/postgresql$ pwd
/home/carlos/postgresql

carlos@db1:~/postgresql$ ls -l
total 20
drwxr-xr-x 2 carlos users 4096 Mar  7 13:13 backup
-rw-r--r-- 1 carlos users 1435 Mar  7 13:25 pg_backup.config
-rw-r--r-- 1 carlos users 4304 Mar  7 12:54 pg_backup_rotated.sh
-rw-r--r-- 1 carlos users 3379 Mar  7 12:54 pg_backup.sh

carlos@db1:~/postgresql$ sh pg_backup.sh
pg_backup.sh: 7: cd: can't cd to pg_backup.sh
pg_backup.sh: 8: pg_backup.sh: source: not found
pg_backup.sh: 16: [: !=: unexpected operator
Making backup directory in 2012-03-07/
pg_backup.sh: 54: pg_backup.sh: Bad substitution

So the only thing this script is doing for me is creating an empty
folder with the date as the name. Any idea what I'm doing wrong? I've
attached the configuration file since this is the only thing I was
told that needs to be modified. The contents are exactly as they are
on my server.


pg_backup.config
Description: Binary data

-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Automated Backup Script Help (Linux)

2012-03-07 Thread Andrew Gould
On Wed, Mar 7, 2012 at 12:33 PM, Carlos Mennens
carlos.menn...@gmail.com wrote:
 I'm trying to get the automated backup scripts to work from:

 http://wiki.postgresql.org/wiki/Automated_Backup_on_Linux

 Currently I'm using PostgreSQL 9.1.3 and have the following three files:

 carlos@db1:~/postgresql$ ls -l
 total 20
 drwxr-xr-x 2 carlos users 4096 Mar  7 13:13 backup
 -rw-r--r-- 1 carlos users 1434 Mar  7 13:19 pg_backup.config
 -rw-r--r-- 1 carlos users 4304 Mar  7 12:54 pg_backup_rotated.sh
 -rw-r--r-- 1 carlos users 3379 Mar  7 12:54 pg_backup.sh

 My Linux shell user 'carlos' has rwx to the entire directory as well
 as the database role 'carlos' is listed as a REPLICATION role:

 postgres=# \du
                                     List of roles
  Role name |                           Attributes
     | Member of
 ---++---
  carlos    | Superuser, No inheritance, Create role, Create DB,
 Replication | {it}

 Now when I attempt to run the script for the 1st time, I get the
 following error:

 carlos@db1:~/postgresql$ pwd
 /home/carlos/postgresql

 carlos@db1:~/postgresql$ ls -l
 total 20
 drwxr-xr-x 2 carlos users 4096 Mar  7 13:13 backup
 -rw-r--r-- 1 carlos users 1435 Mar  7 13:25 pg_backup.config
 -rw-r--r-- 1 carlos users 4304 Mar  7 12:54 pg_backup_rotated.sh
 -rw-r--r-- 1 carlos users 3379 Mar  7 12:54 pg_backup.sh

 carlos@db1:~/postgresql$ sh pg_backup.sh
 pg_backup.sh: 7: cd: can't cd to pg_backup.sh

Doesn't this mean it's trying to cd to a file instead of a directory?

Andrew

-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Automated Backup Script Help (Linux)

2012-03-07 Thread John R Pierce

On 03/07/12 10:33 AM, Carlos Mennens wrote:

carlos@db1:~/postgresql$ sh pg_backup.sh
pg_backup.sh: 7: cd: can't cd to pg_backup.sh
pg_backup.sh: 8: pg_backup.sh: source: not found
pg_backup.sh: 16: [: !=: unexpected operator
Making backup directory in 2012-03-07/
pg_backup.sh: 54: pg_backup.sh: Bad substitution

So the only thing this script is doing for me is creating an empty
folder with the date as the name. Any idea what I'm doing wrong? I've
attached the configuration file since this is the only thing I was
told that needs to be modified. The contents are exactly as they are
on my server.


whats that pg_backup.sh script look like?  you're getting shell errors 
on line 7 and 8 of the script.


this has nothing to do with postgresql.

--
john r pierceN 37, W 122
santa cruz ca mid-left coast


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Automated Backup Script Help (Linux)

2012-03-07 Thread Carlos Mennens
On Wed, Mar 7, 2012 at 2:08 PM, John R Pierce pie...@hogranch.com wrote:
 whats that pg_backup.sh script look like?  you're getting shell errors on
 line 7 and 8 of the script.

I didn't attach it since it's the same as the link I referenced in my
original post. I'll attach it in here for those that don't or can't
download it:

 this has nothing to do with postgresql.

It's a scrip to backup and interact with the database so I think it
does. It may not be specifically what this mailing list was intended
for but it's the best suited that I could find not to mention the
script is posted on the main PostgreSQL wiki site so it appears to be
legit and supported...

http://wiki.postgresql.org/wiki/Automated_Backup_on_Linux

Thanks for any info!
#!/bin/bash
 
###
### LOAD CONFIG ###
###
 
SCRIPTPATH=$(cd ${0%/*}  pwd -P)
source $SCRIPTPATH/pg_backup.config
 
 
###
 PRE-BACKUP CHECKS 
###
 
# Make sure we're running as the required backup user
if [ $BACKUP_USER !=  -a $(id -un) != $BACKUP_USER ]; then
echo This script must be run as $BACKUP_USER. Exiting.
exit 1;
fi;
 
 
###
### INITIALISE DEFAULTS ###
###
 
if [ ! $HOSTNAME ]; then
HOSTNAME=localhost
fi;
 
if [ ! $USERNAME ]; then
USERNAME=postgres
fi;
 
 
###
 START THE BACKUPS 
###
 
 
FINAL_BACKUP_DIR=$BACKUP_DIR`date +\%Y-\%m-\%d`/
 
echo Making backup directory in $FINAL_BACKUP_DIR
 
if ! mkdir -p $FINAL_BACKUP_DIR; then
echo Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix 
it!
exit 1;
fi;
 
 
###
### SCHEMA-ONLY BACKUPS ###
###
 
for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ }
do
SCHEMA_ONLY_CLAUSE=$SCHEMA_ONLY_CLAUSE or datname ~ '$SCHEMA_ONLY_DB'
done
 
SCHEMA_ONLY_QUERY=select datname from pg_database where false 
$SCHEMA_ONLY_CLAUSE order by datname;
 
echo -e \n\nPerforming schema-only backups
echo -e \n
 
SCHEMA_ONLY_DB_LIST=`psql -h $HOSTNAME -U $USERNAME -At -c 
$SCHEMA_ONLY_QUERY postgres`
 
echo -e The following databases were matched for schema-only 
backup:\n${SCHEMA_ONLY_DB_LIST}\n
 
for DATABASE in $SCHEMA_ONLY_DB_LIST
do
echo Schema-only backup of $DATABASE
 
if ! pg_dump -Fp -s -h $HOSTNAME -U $USERNAME $DATABASE | gzip  
$FINAL_BACKUP_DIR$DATABASE_SCHEMA.sql.gz.in_progress; then
echo [!!ERROR!!] Failed to backup database schema of $DATABASE
else
mv $FINAL_BACKUP_DIR$DATABASE_SCHEMA.sql.gz.in_progress 
$FINAL_BACKUP_DIR$DATABASE_SCHEMA.sql.gz
fi
done
 
 
###
## FULL BACKUPS ###
###
 
for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ }
do
EXCLUDE_SCHEMA_ONLY_CLAUSE=$EXCLUDE_SCHEMA_ONLY_CLAUSE and datname !~ 
'$SCHEMA_ONLY_DB'
done
 
FULL_BACKUP_QUERY=select datname from pg_database where not datistemplate and 
datallowconn $EXCLUDE_SCHEMA_ONLY_CLAUSE order by datname;
 
echo -e \n\nPerforming full backups
echo -e \n
 
for DATABASE in `psql -h $HOSTNAME -U $USERNAME -At -c $FULL_BACKUP_QUERY 
postgres`
do
if [ $ENABLE_PLAIN_BACKUPS = yes ]
then
echo Plain backup of $DATABASE
 
if ! pg_dump -Fp -h $HOSTNAME -U $USERNAME $DATABASE | 
gzip  $FINAL_BACKUP_DIR$DATABASE.sql.gz.in_progress; then
echo [!!ERROR!!] Failed to produce plain backup 
database $DATABASE
else
mv $FINAL_BACKUP_DIR$DATABASE.sql.gz.in_progress 
$FINAL_BACKUP_DIR$DATABASE.sql.gz
fi
fi
 
if [ $ENABLE_CUSTOM_BACKUPS = yes ]
then
echo Custom backup of $DATABASE
 
if ! pg_dump -Fc -h $HOSTNAME -U $USERNAME $DATABASE -f 
$FINAL_BACKUP_DIR$DATABASE.custom.in_progress; then
echo [!!ERROR!!] Failed to produce custom backup 
database $DATABASE
else
mv $FINAL_BACKUP_DIR$DATABASE.custom.in_progress 
$FINAL_BACKUP_DIR$DATABASE.custom
fi
fi
 
done
 
echo -e \nAll database backups complete!

-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Automated Backup Script Help (Linux)

2012-03-07 Thread John R Pierce

On 03/07/12 11:20 AM, Carlos Mennens wrote:

SCRIPTPATH=$(cd ${0%/*}  pwd -P)


thats line 7 by my count.


thats some very strange stuff there.  AFAIK ${0%/*}  means, take $0 
which is the name of the invoking command ('sh' in the invocation you 
gave), match and remove the results of the pattern  /*


I think you're NOT supposed to invoke this script with `sh scriptname` 
but instead invoke it as 'path/to/scriptname' (thereforce, it has to be 
chmod +x).  if it was invoked as ./script, then it would cd ., and set 
SCRIPTPATH to the full path of .


again, this is a linux shell issue, has nothing to do with postgres, 
regardless of the rest of the script.








--
john r pierceN 37, W 122
santa cruz ca mid-left coast


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general