PostgreSQL数据库GB级备份与还原方法

  • A+
所属分类:ODOO项目 网站首页

ODOO数据库的备份和还原有两种方法,一种是利用ODOO自带的自动化备份工具Database auto-backup 进行备份,并在WEB页面进行数据库还原,另一种是GB级别以上的备份方法,本文先描述第二种备份方法,然后下篇发布第一种利用ODOO自带自动化工具进行备份方法。

一、备份数据库

1.编写备份数据库shell脚本

自动化备份PG数据库,并按日期进行命名,备份完成后,将备份文件上传到远程FTP服务器。

 
  1. #!/bin/bash  
  2. #postgesql-10 db bakup  
  3. #shell name:auto_pg_bak.sh  
  4. #by:moonrong  
  5. #2021-01-06  
  6.   
  7.   
  8. #定义PG数据库基本信息  
  9. time_date="`date +%Y%m%d_%H%M%S`"  
  10. db_user="postgres"  
  11. db_password="postgres"  
  12. db_host="127.0.0.1"  
  13. db_name="testdb"  
  14. db_port="5432"  
  15. src_dir="/opt/odoo/mybackup"  
  16.   
  17.   
  18. #定义FTP服务器基本信息  
  19. ftp_user="myodoo"  
  20. ftp_password="myodoo"  
  21. ftp_host="192.168.150.121"  
  22. ftp_dir="/home/pg_bak"  
  23.   
  24.   
  25. file_name="${db_name}_${time_date}.sql"  
  26. cd "$src_dir"  
  27. pg_dump -h "$db_host" -p "$db_port" -U "$db_user" -c -f "$file_name" "$db_name"  
  28. find "$src_dir" -mtime 0 -exec scp {} ftp_user@"$ftp_host":"$ftp_dir" \;  

2.编写定时任务

指定每天 1点45分,将PG数据库备份,并执行相关动作。

 
  1. [root@moonyun ~]# crontab -e  
  2. no crontab for root - using an empty one  
  3. crontab: installing new crontab  
  4. [root@moonyun ~]# crontab -l  
  5. */2 * * * * /usr/sbin/ntpdate 58.220.207.226 &> /dev/null  
  6. 45 01 * * * sh /bin/auto_pg_bak.sh &> /dev/null  
  7. [root@moonyun ~]# cd   

二、恢复数据库

1.登录数据库

  1. [root@mytest mybackup]# psql -h 127.0.0.1 -U postgres   
  2. psql (10.14)  
  3. 输入 "help" 来获取帮助信息.  
  4.   
  5. postgres=  

2.psql命令行创建数据库

在还原数据库之前,先创建一个testdb2的数据库,校对规则指定为C。

  1. postgres=# create database testdb2 with encoding 'UTF8'   
  2. postgres-# template template0  
  3. postgres-# owner=odoo  
  4. postgres-# lc_collate='C'  
  5. postgres-# lc_ctype='zh_CN.UTF-8';  
  6. CREATE DATABASE  
  7. postgres=  

3.查看创建结果

  1. postgres=# \l  
  2.                                        数据库列表  
  3.      名称     |  拥有者  | 字元编码 |  校对规则   |    Ctype    |       存取权限          
  4. --------------+----------+----------+-------------+-------------+-----------------------  
  5.  tqdb2021     | odoo     | UTF8     | C           | zh_CN.UTF-8 |   
  6.  b_tqdb3      | odoo     | UTF8     | C           | zh_CN.UTF-8 |   
  7.  tqdb20201108 | odoo     | UTF8     | C           | zh_CN.UTF-8 |   
  8.  postgres     | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |   
  9.  template0    | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +  
  10.               |          |          |             |             | postgres=CTc/postgres  
  11.  template1    | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +  
  12.               |          |          |             |             | postgres=CTc/postgres  
  13.  testdb2      | odoo     | UTF8     | C           | zh_CN.UTF-8 |   
  14. (7 行记录)  
  15.   
  16. postgres=  

4.检查备份文件

 
  1. [root@mytest backups]# ll -h  
  2. 总用量 230M  
  3. -rw-r--r-- 1 root root 230M 1月   8 01:45 testdb_20210108_014501.sql  
  4. [root@mytest backups]  

5.停掉odoo12服务

  1. [root@mytest backups]# systemctl stop odoo12  

6.导入sql文件

将上面的testdb20210108014501.sql数据库文件导入到testdb2

  1. [root@mytest backups]# psql -d testdb2 -U odoo  <test1214_20210108_014501.sql  

7.重启odoo服务

  1. [root@mytest backups]# systemctl start odoo12  

8.处理2个问题

用数据库管理工具清除登录样式表丢失的问题

  1. DELETE FROM ir_attachment WHERE url LIKE '/web/content/%';  

然后再处理模块图标丢失的问题。

注:上面只是测试,数据库恢复自动化脚本以后再更新吧。

moonrong
  • 版权声明:本站原创文章,于2021年1月11日11:20:18,由 发表,共 2951 字。
  • 版权声明: 本文由于2021年1月11日11:20:18 发表在 好派笔记,共 2951 字。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: