脚本修改仅备份1个数据库

时间:2019-04-19 13:19:23

标签: bash postgresql backup

所以我遇到了这个问题,下面的脚本针对每个数据库运行,并为该数据库创建备份。 i in $databases; do的这一行,但是如何修改此脚本以仅备份1个名为“ test_database”的数据库?

#!/bin/bash
# Location of the backup logfile.
logfile="/home/erp/backups/logfile.log"
#erp user
user="antonp"
# Location to place backups.
backup_dir="/home/erp/backups"

if [ ! -d $backup_dir ]; then
  mkdir $backup_dir
  chown $user:$user $backup_dir
fi

touch $logfile
  timeslot=`date +%d%m%y%H%M%S`
  databases=`sudo su - postgres -c "psql template1 -c '\l'|tail -n+4|cut -d'|' -f 1|sed -e '/^ *$/d'|sed -e '$ d'"`
  for i in $databases; do
    if [ "$i" != "template0" ] && [ "$i" != "template1" ]; then
      timeinfo=`date '+%T %x'`
      echo "Backup and Vacuum started at $timeinfo for time slot $timeslot on database: $i " >> $logfile
      su - postgres -c "vacuumdb -z -U postgres $i >/dev/null 2>&1"
      su - postgres -c "pg_dump $i --exclude-table-data=sale_order -U postgres | gzip > \"/tmp/openerp-$i-$timeslot-database.gz\""
      cp /tmp/openerp-$i-$timeslot-database.gz $backup_dir/openerp-$i-$timeslot-database.gz
      chown $user:$user $backup_dir/openerp-$i-$timeslot-database.gz
      timeinfo=`date '+%T %x'`
      rm /tmp/openerp-$i-$timeslot-database.gz
      echo "Backup and Vacuum complete at $timeinfo for time slot $timeslot on database: $i " >> $logfile
    fi
done

1 个答案:

答案 0 :(得分:1)

只需稍微更改数据库变量:

touch $logfile
  timeslot=`date +%d%m%y%H%M%S`
  #databases=`sudo su - postgres -c "psql template1 -c '\l'|tail -n+4|cut -d'|' -f 1|sed -e '/^ *$/d'|sed -e '$ d'"`
  databases="test_database"
  for i in $databases; do
    if [ "$i" != "template0" ] && [ "$i" != "template1" ]; then
      timeinfo=`date '+%T %x'`
      echo "Backup and Vacuum started at $timeinfo for time slot $timeslot on database: $i " >> $logfile
      su - postgres -c "vacuumdb -z -U postgres $i >/dev/null 2>&1"
      su - postgres -c "pg_dump $i --exclude-table-data=sale_order -U postgres | gzip > \"/tmp/openerp-$i-$timeslot-database.gz\""
      cp /tmp/openerp-$i-$timeslot-database.gz $backup_dir/openerp-$i-$timeslot-database.gz
      chown $user:$user $backup_dir/openerp-$i-$timeslot-database.gz
      timeinfo=`date '+%T %x'`
      rm /tmp/openerp-$i-$timeslot-database.gz
      echo "Backup and Vacuum complete at $timeinfo for time slot $timeslot on database: $i " >> $logfile
    fi
done