0
点赞
收藏
分享

微信扫一扫

PostgreSQL 备份恢复

pg_dump -h 192.168.1.8 -p 15432 -U postgres oumumes> /home/temp/oumumes.bak

2.1 单库备份

  • -U : Specify the PostgreSQL username.
  • -W : Force pg_dump command to ask for a password.
  • -F : Specify the format of the output file.
  • -f : Specify the output file.
  • p : Plain text SQL script.
  • c : Specify the custom formate.
  • d : Specify the directory format.
  • t : Specify tar format archive file.

(1) 备份一个tar 格式的文件

 pg_dump -h 127.0.0.1 -p 15432 -U postgres oumumes> /home/temp/oumumes.tar

(2)备份一个压缩文件,当存储量比较大的时候可以节省空间

 pg_dump -h 127.0.0.1 -p 15432 -U postgres oumumes|gzip> /home/temp/oumumes.gz

(3)产生一个备份格式为目录的备份

   pg_dump -U test_user -F d -j 5 test_db -f  testdb

   pg_dump -h 127.0.0.1 -p 15432 -U postgres oumumes|gzip> /home/temp/oumumes.gz

note:以上都可以进行备份缩短备份时间,但是对系统io负载产生一定影响

pg_dumpall -U postgres -W -f   test_db.sql

上面备份明亮可能会遇到连续输入密码的问题,可以采用下面的方式来避免


export PGUSER=pg_user export PGPASSWORD='pg_password' 
export PGHOST=localhost


比如下面要备份192.168.0.100这台机器上的remote_db1 数据库

pg_dump -h 192.168.0.100 -U postgres -F c remote_db1 > remote_db1.tar

定时自动备份 crontab 下面是定时每天晚上20:30 开始备份yourdb 到/root/backup_db文件


30 20 * * * pg_dump -U postgres your_db > /root/backup_db.sql


举报

相关推荐

0 条评论