標籤:postgresql shell
指令碼功能:
系統內容:SUSE Linux Server 11.3 X64
1.刪除3天前壓縮檔(.bak.tar.gz)
2.備份資料庫(未壓縮檔.bak)
3.壓縮昨天Database Backup檔案(.bak.tar.gz),刪除昨天Database Backup未壓縮檔(.bak)
#!/bin/bash
export PATH=$PATH:/usr/local/pgsql/bin
DATE=`date +%Y_%m_%d_%H`
YESDAY=`date -d "yesterday" +%Y_%m_%d_%H`
TWODAGO=`date -d "3 day ago" +%Y_%m_%d_%H`
PGHOME="/home/postgres"
cd $PGHOME
echo "Dir list before delete 3 day ago backup file: "
ls -lrt
#### delete 3 day ago backup file ####
TDAFILE="testdb_"$TWODAGO".bak.tar.gz"
echo "Detele 3 Day ago file: "$TDAFILE
rm "$TDAFILE"
echo "Dir list after delete 3 day ago backup file: "
ls -lrt
#### Backup ape database ####
echo $DATE
echo "Starting backup database: ape ...."
su - postgres -c "pg_dump testdb > /home/postgres/testdb_$DATE.bak"
reval=#?
if reval=0
then
echo "Database testdb backup Successful!"
else
echo "Database testdb backup Failed!"
fi
echo "Dir list After backup database testdb: "
ls -lrt
#### Compress and remove yesterday bak file####
COMFILE="testdb_"$YESDAY".bak.tar.gz"
YDFILE="testdb_"$YESDAY".bak"
echo "tar zcvf $COMFILE $YDFILE"|sh
echo "General yesterday tar file: "$COMFILE
rm "$YDFILE"
echo "delete yesterday file: "$YDFILE
echo "Dir list After tar yesterday backup file: "
ls -lrt
find /home/postgres -mtime +3 -name "*.bak" -exec rm -rf {} \;
find /home/postgres -mtime +3 -name "*.bak.tar.gz" -exec rm -rf {} \;
本文出自 “yiyi” 部落格,請務必保留此出處http://heyiyi.blog.51cto.com/205455/1658896
postgreSQL備份指令碼刪除3天前備份同時壓縮昨天備份