Created
October 7, 2011 01:45
-
-
Save sakama/1269241 to your computer and use it in GitHub Desktop.
PostgreSQL用Backupスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/sh | |
# PostgreSQL database backup script | |
# Version 1.1 [Apr. 17, 2002] Add routine: deleting exipired files. | |
# Version 1.0 [Sep. 10, 2002] First release. | |
# * Feature | |
# Dump to DATABASE_NAME_YYMMDD.pgdmp. | |
# Delete expired pgdmp file. | |
# Output log. | |
# Setting | |
PATH="$PATH":/usr/local/pgsql/bin | |
PG=/usr/local/pgsql | |
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":$PG/lib | |
export PGLIB=$PG/lib | |
export PGDATA=$PG/data | |
# Get all DB name | |
TARGET_DB=`psql -l -P format=unaligned -P fieldsep=, -P tuples_only |perl -F, -lane "if (@F[0] ne 'template0') {print @F[0]}"` | |
DIR=/backup/postgres | |
LOG=$DIR/pgdump.log | |
BACKUPDIR=$DIR/dbbackup | |
EXPIRE_DAY=14 | |
# Main routine | |
echo "pg_dump start." `date` >>$LOG | |
date=`date +%Y%m%d` | |
for db_name in $TARGET_DB | |
do | |
DUMP_FILE_PATH=$BACKUPDIR/${db_name}_${date}.pgdmp | |
/usr/local/pgsql/bin/pg_dump $db_name -b -o -Fc > $DUMP_FILE_PATH 2>>$LOG | |
echo "Database $db_name backup complete." `date` >>$LOG | |
done | |
echo "pg_dump complete." `date` >>$LOG | |
echo "Delete expired pgdmp file start." `date` >>$LOG | |
find $BACKUPDIR -type f -daystart -mtime +$EXPIRE_DAY |xargs /bin/rm -f | |
echo "Delete expired pgdmp file complete." `date` >>$LOG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment