#!/bin/sh # This script deletes tables from a PostgreSQL pg_dump plain # text dump. This is useful for getting smaller backups. # # Usage: getridof.sh # Check requirements echo "This is a beta release use it at your own risc. If you want to use this script first you must edit variuables then comment out this line."; exit requirements="egrep grep sed awk" for req in $requirements do if [ -z "`type -p $req`" ] ; then echo "Requirement $req is not found. Exiting..." exit 1 fi done tables="usr|x3d" # Check that arguments are given if [ "$1" = "" ]; then echo "Error: no backup file given" exit fi # Extract the backup to a working SQL script cp "$1" ${1}_to_norm.sql.gz gunzip < ${1}_to_norm.sql.gz > working.sql #array=($(egrep '^(usr|x3d)' < working.sql)) array=($(egrep '^($tables)' < working.sql)) for table in ${array[@]} do # Find the line before the table's COPY output begins START=`grep -n "^COPY $table " working.sql | sed -e 's/:.*//'` ((START--)) #Find line number at which COPY ends #Trying to use awk END=`grep -n "^\\\\\\." working.sql | awk -v START=$START '{if ($1 > START) print $1}' | sed -e 's/:.*//' | head -1` ((END++)) # Remove all contents of the working file containing the COPY command sed -i -e $START,$END d working.sql done # Rename the working file to the table name mv working.sql "${1}_normalized.sql" gzip "${1}_normalized.sql" > "${1}_normalized.sql.gz" # Remove temporary file rm working.sql-e rm ${1}_to_norm.sql.gz