Linux配置postgresql自动备份

1、pg_dump数据库备份命令

           /usr/pgsql-9.3/bin/pg_dump -F c -O -U dotop -h 127.0.0.1 -p 5432 -f /data/erp_data/db_backup/geyt_20170214_09_37_55.sql geyt

    
 
2.Linux配置postgresql自动备份
说明:使用系统用户dotop来备份dbuser(数据库用户)的数据库demo
1)创建系统用户dotop
    adduser dotop
2)创建数据库用户dbuser,创建demo数据库
  • [root@hst init.d]# su - postgres
  • -bash-4.1createuser --superuser dbuser
  • -bash-4.1$ psql
  • psql (9.3.5)
  • Type "help" for help.
  • postgres=# alter user dbuser with password 'dbuser';
  • postgres=# create database demo with encoding='UTF8' owner=dbuser;
3)在dotop根目录下编写备份脚本
  • cd /home/dotop
  • vi db_backup.sh
在db_backup.sh中输入以下内容:
#!/bin/bash
/usr/pgsql-9.3/bin/pg_dump -F c -O -U dbuser -h 127.0.0.1 -p 5432 -f /data/db_backup/demo_$(date +%Y%m%d_%H_%M_%S).sql dotop
echo "backup finished"  
保存,测试执行:/home/dotop/db_backup.sh
4)加入自动执行命令
    
vi  /etc/crontab
最后一行添加以下命令:
00 2 * * * dotop /home/dotop/db_backup.sh
意思是,每天凌晨2点执行dotop下的自动备份任务
posted @ 2018-02-01 09:30  wydong  阅读(3619)  评论(0编辑  收藏  举报