Postgres 9.4 恢复不工作

Postgres 9.4 restore not working

我根据 WAL 归档的 postgres 文档备份了 postgresql 9.4 数据库。

备份后我在数据库中创建了 2 条记录。

现在,当我尝试恢复数据库时,我在上面创建的最后 2 条记录没有出现。

WAL归档步骤:

cd /etc/postgresql/9.4/
mkdir archives
mkdir backups
chown postgres:postgres archives
chown postgres:postgres backups
cd /etc/postgresql/9.4/main/
echo 'max_wal_senders=1' >> postgresql.conf
echo 'wal_level=hot_standby' >> postgresql.conf
echo 'archive_mode=on' >> postgresql.conf
echo "archive_command='test ! -f /etc/postgresql/9.4/archives/%f && cp %p /etc/postgresql/9.4/archives/%f'" >> postgresql.conf
echo 'local replication postgres trust' >> pg_hba.conf
service postgresql restart

备份步骤:

cd /etc/postgresql/9.4/backups
rm -rf *
pg_basebackup --xlog -U postgres --format=t -D /etc/postgresql/9.4/backups/

恢复步骤:

 service postgresql stop
 cd /var/lib/postgresql/9.4/
 if [ ! -d "/var/lib/postgresql/9.4/tmp/" ]
 then
    mkdir tmp
  else
    rm -rf tmp
  fi
  mkdir tmp
   mv /var/lib/postgresql/9.4/main/* /var/lib/postgresql/9.4/tmp/
  cd /var/lib/postgresql/9.4/main/
  rm -rf *
   cd /etc/postgresql/9.4/backups
  tar -xf base.tar -C /var/lib/postgresql/9.4/main/
 cd /var/lib/postgresql/9.4/main/

 FROMDIR="/etc/postgresql/9.4/archives/"

 TODIR="/var/lib/postgresql/9.4/tmp/pg_xlog/"

 if [ ! -d "$FROMDIR" ]
 then
      echo "Directory $FROMDIR does not exist!!"
 exit
 fi


 if [ ! -d "$TODIR" ]
 then
        echo "Directory $TODIR does not exist!!"
        exit
  fi

  cd $FROMDIR

  for i in `find . -type f`
      do
         if [ ! -f $TODIR/$i ]
      then
      echo "copying file $i"
      cp $i /var/lib/postgresql/9.4/main/pg_xlog/$i
      fi

     done

   cd /var/lib/postgresql/9.4/main/pg_xlog/

   chown -R postgres:postgres *

   cd /var/lib/postgresql/9.4/main/

   FILE="recovery.done"

   if [ -f $FILE ]
   then
          mv $FILE recovery.conf
   else
         echo "restore_command = 'cp /etc/postgresql/9.4/archives/%f %p'" >> recovery.conf
   fi

   su postgres service postgresql start

   exit

当前 WAL 段(通常为 16 Mb)已满时,更改会出现在存档中(/etc/postgresql/9.4/archives/)。让我引用 documentation:

The archive_command is only invoked for completed WAL segments. Hence, if your server generates little WAL traffic (or has slack periods where it does so), there could be a long delay between the completion of a transaction and its safe recording in archive storage. To limit how old unarchived data can be, you can set archive_timeout to force the server to switch to a new WAL segment file periodically. When this parameter is greater than zero, the server will switch to a new segment file whenever this many seconds have elapsed since the last segment file switch, and there has been any database activity, including a single checkpoint. (Increasing checkpoint_timeout will reduce unnecessary checkpoints on an idle system.) Note that archived files that are closed early due to a forced switch are still the same length as completely full files. Therefore, it is unwise to use a very short archive_timeout — it will bloat your archive storage.

如果您只想测试还原过程,您可以在创建一些记录后简单地执行 select pg_switch_xlog(); 以强制切换到新的 WAL 段。然后验证存档目录中是否出现了新文件。


此外,您不需要将文件从存档目录复制到 pg_xlog/。 Restore_command 会为你做的。