AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 333628
Accepted
Kokizzu
Kokizzu
Asked: 2023-12-01 14:56:28 +0800 CST2023-12-01 14:56:28 +0800 CST 2023-12-01 14:56:28 +0800 CST

恢复但仅表上丢失的数据

  • 772

我已经在同一实例上的 2 个不同数据库上恢复了 2 个数据库,调用它geo并且restore1 架构是相同的,唯一的区别是geo数据库仅包含 <= 之前2023-11-23和 >= 之后的记录2023-12-01,而restore1包含 <= 2023-11-30,我如何复制这些每个表容易缺失值(2023-11-23直到数据库) 2023-11-30?restore1geo

我能想到的一种方法是,为每个表创建一个程序,从之前选择最新的 id 2023-11-24(geo称为X),然后选择 id 大于的所有记录X,然后手动将其插入geo数据库。

有没有更简单的方法?

有数百张表,但大多数都是这样的:

restore1=# \d station_logs_599
                                              Table "public.station_logs_599"
         Column         |              Type              | Collation | Nullable |                 Default
------------------------+--------------------------------+-----------+----------+------------------------------------------
 id                     | integer                        |           | not null | nextval('station_logs_id_seq'::regclass)
 created_at             | timestamp(6) without time zone |           |          |
 updated_at             | timestamp(6) without time zone |           |          |
 deleted_at             | timestamp(6) without time zone |           |          |
 submitted_at           | timestamp(6) without time zone |           | not null |
 sequence               | integer                        |           |          |
 level_sensor           | double precision               |           |          |
 accel_x                | double precision               |           |          |
 accel_y                | double precision               |           |          |
 accel_z                | double precision               |           |          |
 power_current          | double precision               |           |          |
 ip_address             | character varying(50)          |           |          |
 log_type               | integer                        |           |          |
 station_id             | integer                        |           | not null |
 power_voltage          | double precision               |           |          |
 data                   | jsonb                          |           |          |
 is_deleted             | boolean                        |           | not null | false
 temperature            | double precision               |           |          |
 wind_speed             | double precision               |           |          |
 soil_moisture          | double precision               |           |          |
 wind_direction         | double precision               |           |          |
 raindrop               | double precision               |           |          |
 humidity               | integer                        |           |          |
 barometric_pressure    | double precision               |           |          |
 wind_speed_average     | double precision               |           |          |
 wind_gust              | double precision               |           |          |
 wind_direction_average | double precision               |           |          |
 rain_rate              | double precision               |           |          |
Indexes:
    "station_logs_599_epoch" btree (date_part('epoch'::text, submitted_at))
    "station_logs_599_submitted_at" btree (submitted_at)
    "uniq_sid_sat_599" UNIQUE CONSTRAINT, btree (submitted_at)
Foreign-key constraints:
    "station_logs_599_station_fk" FOREIGN KEY (station_id) REFERENCES stations(id)
postgresql
  • 1 1 个回答
  • 46 Views

1 个回答

  • Voted
  1. Best Answer
    Kokizzu
    2023-12-05T00:44:53+08:002023-12-05T00:44:53+08:00

    我使用golang来解决这个问题:

    tl;dw:

    1. 读取所有电台表
    2. 如果两者都存在,请选择源和目标的每个日期回顾 ( DATE(submitted_at), COUNT(1))
    3. 如果目标的编号与源的编号不同,则插入到目标
        var src, dest *Pg.RDBMS
        {
            opt := `host=127.0.0.1 user=x password=x dbname=x sslmode=disable` 
            conn := sqlx.MustConnect(`postgres`, opt)
            conn.DB.SetMaxOpenConns(61) 
            dest = &Pg.RDBMS{
                Name:    `destination`,
                Adapter: conn,
            }
        }
    
        {
            opt := `host=127.0.0.1 user=y password=y dbname=y sslmode=disable` 
            conn := sqlx.MustConnect(`postgres`, opt)
            src = &Pg.RDBMS{
                Name:    `source`,
                Adapter: conn,
            }
        }
    
    
        srcStations := src.QStrMapMap(`imei`, `SELECT * FROM stations`)
        destStations := dest.QStrMapMap(`imei`, `SELECT * FROM stations`)
    
        // compare stations
        removeUnecessary := func(m M.SX) {
            delete(m, `last_level_sensor`)
            delete(m, `last_submitted_at`)
            delete(m, `updated_at`)
            delete(m, `updated_by`)
            delete(m, `name`)
            delete(m, `lat`)
            delete(m, `long`)
        }
    
        for imei := range srcStations {
            ss := srcStations.GetMSX(imei)
            removeUnecessary(ss)
            ds := destStations.GetMSX(imei)
            removeUnecessary(ds)
    
            // check the diff of stations table
            if len(ds) == 0 {
                fmt.Println(`missing from destination: `+imei, ss)
                // TODO: insert on destination
                continue
            } else if !reflect.DeepEqual(ss, ds) {
                fmt.Println(`different: ` + imei)
                diffs := pretty.Diff(ss, ds)
                for _, diff := range diffs {
                    fmt.Println(diff)
                }
            }
    
            // if source different from destination, there might be one more than station created not with same order after moving server
            srcStationId := fmt.Sprint(ss[`id`])
            dstStationId := fmt.Sprint(ds[`id`])
            if srcStationId != dstStationId {
                fmt.Println(`source station id not the same as destination`, srcStationId, dstStationId)
            }
    
            // check whether tables exists
            if !src.TableExists(`station_logs_` + srcStationId) {
                fmt.Println(`missing source table station_logs_` + srcStationId)
                continue
            }
            if !dest.TableExists(`station_logs_` + dstStationId) {
                fmt.Println(`missing destination table station_logs_` + dstStationId)
                continue
            }
    
            srcSelect1 := fmt.Sprintf(`SELECT DATE(submitted_at)::TEXT, COUNT(1) FROM station_logs_%s GROUP BY 1`, srcStationId)
            srcDtCount := src.QStrIntMap(srcSelect1)
    
            dstSelect1 := fmt.Sprintf(`SELECT DATE(submitted_at)::TEXT, COUNT(1) FROM station_logs_%s GROUP BY 1`, dstStationId)
            dstDtCount := dest.QStrIntMap(dstSelect1)
    
            fmt.Println(`comparing station_logs_`, srcStationId, dstStationId)
    
            for submittedAt, srcCount := range srcDtCount {
                dstCount := dstDtCount[submittedAt]
                if srcCount != dstCount {
                    fmt.Println(`different count: `, srcStationId, submittedAt, srcCount, dstCount)
    
                    query2 := fmt.Sprintf(`SELECT * FROM station_logs_%s WHERE DATE(submitted_at) = '%s'`, srcStationId, submittedAt)
    
                    srcLogs := src.QStrMapMap(`id`, query2)
    
                    dstInsertLogs := fmt.Sprintf(`
    INSERT INTO station_logs_%s (
        created_at, updated_at, submitted_at, sequence, level_sensor, accel_x, accel_y, accel_z, power_current, ip_address, log_type, station_id, power_voltage, data, is_deleted, temperature, wind_speed, soil_moisture, wind_direction, raindrop, humidity, barometric_pressure, wind_speed_average, wind_gust, wind_direction_average, rain_rate
    ) VALUES (
                $1,         $2,        $3,           $4,           $5,         $6,      $7,      $8,      $9,            $10,       $11,      $12,        $13,           $14,   $15,        $16,         $17,        $18,           $19,         $20,      $21,                 $22,                 $23,       $24,                  $25,        $26 
    )`, dstStationId)
                    query3 := fmt.Sprintf(`
    SELECT COUNT(1) FROM station_logs_%s WHERE submitted_at = $1`, dstStationId)
    
                    skipped := 0
                    dest.DoTransaction(func(tx *Pg.Tx) string {
                        for id := range srcLogs {
                            log := srcLogs.GetMSX(id)
                            if tx.QInt(query3, log[`submitted_at`]) > 0 {
                                skipped++
                                continue
                            }
    
                            tx.DoExec(dstInsertLogs,
                                log[`created_at`],
                                log[`updated_at`],
                                log[`submitted_at`],
                                log[`sequence`],
                                log[`level_sensor`],
                                log[`accel_x`],
                                log[`accel_y`],
                                log[`accel_z`],
                                log[`power_current`],
                                log[`ip_address`],
                                log[`log_type`],
                                log[`station_id`],
                                log[`power_voltage`],
                                log[`data`],
                                log[`is_deleted`],
                                log[`temperature`],
                                log[`wind_speed`],
                                log[`soil_moisture`],
                                log[`wind_direction`],
                                log[`raindrop`],
                                log[`humidity`],
                                log[`barometric_pressure`],
                                log[`wind_speed_average`],
                                log[`wind_gust`],
                                log[`wind_direction_average`],
                                log[`rain_rate`],
                            )
                        }
                        return ``
                    })
    
                    fmt.Println(`inserted: `, srcStationId, submittedAt, len(srcLogs), `skipped`, skipped)
    
                }
    
            }
       }
    
    • 0

相关问题

  • 我可以在使用数据库后激活 PITR 吗?

  • 运行时间偏移延迟复制的最佳实践

  • 存储过程可以防止 SQL 注入吗?

  • PostgreSQL 中 UniProt 的生物序列

  • PostgreSQL 9.0 Replication 和 Slony-I 有什么区别?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve