To restore the database on a new host-将数据库恢复至一个新的主机上

本文介绍如何将Oracle数据库迁移到新主机的详细步骤,包括备份恢复、参数文件编辑、控制文件恢复等关键技术点。

To restore the database on a new host:
1. Ensure that the backups of the target database are accessible on the new host.
2. Configure the ORACLE_SID on hostb.
3. Start RMAN on hostb and connect to the target database without connecting to the recovery catalog.
For example, enter the following command:
% rman NOCATALOG
RMAN> CONNECT TARGET
/
4. Set the DBID and start the database instance without mounting the database. For example, run SET DBID to set the DBID, then run STARTUP NOMOUNT:
SET DBID 1340752057;
STARTUP NOMOUNT
RMAN fails to find the server parameter file, which has not yet been restored, but starts the instance with a "dummy" file. Sample output follows:
startup failed: ORA-01078: failure in processing system parameters LRM-00109: could not open parameter file '/net/hostb/oracle/dbs/inittrgta.ora' trying to start the Oracle instance without parameter files ...
Oracle instance started
5. Restore and edit the server parameter file. --从自动备份文件中恢复参数文件
Allocate a channel to the media manager, then restore the server parameter file as a client-side parameter file and use the SET command to indicate the location of the autobackup (in this example, the autobackup is in /tmp):
RUN
{
ALLOCATE CHANNEL c1 DEVICE TYPE sbt PARMS '...';
SET CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/tmp/%F'; RESTORE SPFILE
TO PFILE '?/oradata/test/inittrgta.ora'
FROM AUTOBACKUP;
SHUTDOWN ABORT;
}

6. Edit the restored initialization parameter file.  --修改已恢复的参数文件
Change any location-specific parameters, for example, those ending in _DEST, to reflect the new directory structure. For example, edit the following parameters:
- IFILE
- LOG_ARCHIVE_DEST_1
- CONTROL_FILES
7. Restart the instance with the edited initialization parameter file.  --用修改好的参数文件来重新启动数据库
For example, enter the following command:
STARTUP FORCE NOMOUNT PFILE='?/oradata/test/inittrgta.ora';
8. Restore the control file from an autobackup and then mount the database. --从自动备份文件中恢复控制文件至CONTROL_FILES parameter.
For example, enter the following command:
RUN
{
ALLOCATE CHANNEL c1 DEVICE TYPE sbt PARMS '...';
RESTORE CONTROLFILE FROM AUTOBACKUP;
ALTER DATABASE MOUNT;
}
RMAN restores the control file to whatever locations you specified in the CONTROL_FILES initialization parameter.
9. Catalog the data file copies that you copied in "Restoring Disk Backups to a New Host", using their new file names or CATALOG START WITH (if you know all the files are in directories with a common prefix easily addressed with a CATALOG START WITH command). For example, run:
CATALOG START WITH '/oracle/oradata/trgt/';
If you want to specify files individually, then you can execute a CATALOG command as follows:
CATALOG DATAFILECOPY
'/oracle/oradata/trgt/system01.dbf', '/oracle/oradata/trgt/undotbs01.dbf', '/oracle/oradata/trgt/cwmlite01.dbf', '/oracle/oradata/trgt/drsys01.dbf', '/oracle/oradata/trgt/
example01.dbf', '/oracle/oradata/trgt/indx01.dbf', '/oracle/oradata/trgt/tools01.dbf', '/oracle/oradata/trgt/users01.dbf';
10. Start a SQL*Plus session on the new database and query the database file names recorded in the control file.
Because the control file is from the trgta database, the recorded file names use the original hosta file names. You can query V$ views to obtain this information.
Run the following query in SQL*Plus:

COLUMN NAME FORMAT a60
SPOOL LOG '/tmp/db_filenames.out'
SELECT FILE# AS "File/Grp#", NAME
FROM V$DATAFILE
UNION
SELECT GROUP#,MEMBER
FROM V$LOGFILE;
SPOOL OFF
EXIT

11. Write the RMAN restore and recovery script. The script must include the following steps:
a. For each data file on the destination host that is restored to a different path than it had on the source host, use a SET NEWNAME command to specify the new path on the destination host. If the file systems on the destination system are set up to have the same paths as the source host, then do not use SET NEWNAME for those files restored to the same path as on the source host.

b. For each online redo log that is to be created at a different location than it had on the source host, use SQL ALTER DATABASE RENAME FILE commands to specify the path name on the destination host. If the file systems on the destination system are set up to have the same paths as the source host, then do not use ALTER DATABASE RENAME FILE for those files restored to the same path as on the source host.
c. Perform a SET UNTIL operation to limit recovery to the end of the archived redo logs. The recovery stops with an error if no SET UNTIL command is specified.
d. Restore and recover the database.
e. Run the SWITCH DATAFILE ALL command so that the control file recognizes the new path names as the official new names of the data files.
Example 20-3 shows the RMAN script reco_test.rman that can perform the restore and recovery operation.
Example 20-3 Restoring a Database on a New Host:
RUN
{
# allocate a channel to the tape device
ALLOCATE CHANNEL c1 DEVICE TYPE sbt PARMS '...';
# rename the data files and online redo logs
SET NEWNAME FOR DATAFILE 1 TO '?/oradata/test/system01.dbf'; SET NEWNAME FOR DATAFILE 2 TO '?/oradata/test/undotbs01.dbf'; SET NEWNAME
FOR DATAFILE 3 TO '?/oradata/test/cwmlite01.dbf'; SET NEWNAME FOR DATAFILE 4 TO '?/oradata/test/drsys01.dbf'; SET NEWNAME FOR DATAFILE 5 TO
'?/oradata/test/example01.dbf'; SET NEWNAME FOR DATAFILE 6 TO '?/oradata/test/indx01.dbf'; SET NEWNAME FOR DATAFILE 7 TO '?/oradata/test/
tools01.dbf'; SET NEWNAME FOR DATAFILE 8 TO '?/oradata/test/users01.dbf'; SQL "ALTER DATABASE RENAME FILE ''/dev3/oracle/dbs/redo01.log'' TO ''?/
oradata/test/redo01.log'' ";
SQL "ALTER DATABASE RENAME FILE ''/dev3/oracle/dbs/redo02.log'' TO ''?/oradata/test/redo02.log'' ";
# Do a SET UNTIL to prevent recovery of the online logs SET UNTIL SCN 123456;
# restore the database and switch the data file names RESTORE DATABASE;
SWITCH DATAFILE ALL;
# recover the database
RECOVER DATABASE;
}
EXIT
12. Execute the script created in the previous step.
For example, start RMAN to connect to the target database and run the @ command:
% rman TARGET / NOCATALOG
RMAN> @reco_test.rman
13. Open the restored database with the RESETLOGS option. From the RMAN prompt, open the database with the RESETLOGS option:
ALTER DATABASE OPEN RESETLOGS;
Caution:
When you re-open your database in the next step, do not connect to the recovery catalog. Otherwise, the new database incarnation created is registered automatically in the recovery catalog, and the file names of the production database are replaced by the new file names specified in the script.
14. Optionally, delete the test database with all of its files.
Note:
If you used an ASM disk group, then the DROP DATABASE command is the only way to safely remove the files of the test database. If you restored to non-ASM storage then you can also use operating system commands to remove the database.
Use the DROP DATABASE command to delete all files associated with the database automatically. The following example deletes the database files:
STARTUP FORCE NOMOUNT PFILE='?/oradata/test/inittrgta.ora'; DROP DATABASE;
Because you did not perform the restore and recovery operation when connected to the recovery catalog, the recovery catalog contains no records for any of the restored files or the procedures performed during the test. Likewise, the control file of the trgta database is completely unaffected by the test.

<think>嗯,用户这次聚焦在PostgreSQL的restore报错问题,特别是唯一键冲突和路径配置错误。用户引用了三个站内案例,但核心诉求很明确:解决恢复数据时的具体报错。看来用户是个运维或开发人员,正在处理数据库恢复任务,情绪可能比较急切——毕竟重复键错误会直接阻断数据恢复流程。 从引用[1]看,“重复键违反唯一约束”是典型的主键冲突。这通常发生在试图恢复的数据包含已存在于表中的主键值时。用户可能没有意识到需要清空目标表就执行了恢复,或者备份文件本身包含重复数据。需要引导用户检查表状态和备份数据完整性。 引用[3]提到的路径错误则更基础,但容易被忽视。pgAdmin找不到pg_restore可执行文件,说明安装路径配置有问题。Windows环境下路径斜杠和空格都是常见坑点,需要验证实际安装位置。 用户隐藏的深层需求可能是想建立系统化的恢复流程:既要解决眼前报错,又想预防同类问题。所以除了具体解决方案,补充检查清单会很有帮助——比如提醒用户每次恢复前校验备份文件版本,这个细节90%的故障都源于此。 技术策略上分三层处理:先解决路径配置(基础环境),再处理数据冲突(业务数据),最后给预防措施(最佳实践)。特别注意提醒用户“TRUNCATE TABLE”比“DELETE”更安全高效,避免事务日志膨胀——这对生产数据库很重要。 安全方面要强调备份的重要性!修改约束这种危险操作必须搭配全量备份警告。用户如果正在处理生产库,一个误操作就可能造成数据事故。 </think>以下是针对PostgreSQL数据库`restore`报错的系统性解决方案,综合引用信息中的关键问题: --- ### **一、核心报错与解决方案** #### **1. 唯一键冲突(引用[1])** - **报错示例** `ERROR: duplicate key violates unique constraint "ws_overyears_monthly_wind_pkey"` - **原因分析** 备份数据中包含与目标表**主键或唯一约束冲突**的记录,常见于: - 目标表已有数据未清空 - 备份数据本身存在重复主键 - 增量备份恢复时未处理冲突 - **解决方案** ```sql -- 方案1:清空目标表再恢复(数据强制覆盖) TRUNCATE TABLE ws_overyears_monthly_wind CASCADE; -- 方案2:忽略冲突数据(保留现有数据) pg_restore -U username -d dbname --data-only --on-conflict-do-nothing backup_file.dump -- 方案3:删除约束后恢复(需谨慎) ALTER TABLE ws_overyears_monthly_wind DROP CONSTRAINT ws_overyears_monthly_wind_pkey; pg_restore -U username -d dbname backup_file.dump ALTER TABLE ws_overyears_monthly_wind ADD PRIMARY KEY (id); -- 恢复后重建约束 ``` --- #### **2. 可执行文件路径错误(引用[3])** - **报错示例** `'C:\PostgreSQL\13\pgAdmin 4\runtime\pg_dump.exe' file not found` - **原因** pgAdmin配置的**二进制路径(Binary Path)** 错误或PostgreSQL未正确安装。 - **修复步骤** 1. **定位实际安装路径** 检查PostgreSQL的真实安装目录(如 `C:\Program Files\PostgreSQL\13\bin`)。 2. **修正pgAdmin配置** - 打开pgAdmin → **File → Preferences → Paths → Binary Paths** - 在**PostgreSQL Binary Path**中填写正确路径(例如:`C:\Program Files\PostgreSQL\13\bin`) 3. **验证环境变量** 将PostgreSQL的`bin`目录加入系统`PATH`环境变量。 --- #### **3. 权限与密码问题(引用[2]引申)** - **报错特征** `Incorrect password or user not known` - **解决方法** - 使用`psql`命令行验证连接: ```bash psql -U informix -d dbname # 交互式输入密码测试 ``` - 重置用户密码: ```sql ALTER USER informix WITH PASSWORD 'new_password'; ``` - 检查`pg_hba.conf`配置文件,确保允许用户远程连接: ``` # 示例配置行 host all informix 0.0.0.0/0 md5 ``` --- ### **二、通用恢复流程建议** 1. **预处理操作** ```sql DROP DATABASE IF EXISTS target_db; -- 删除旧库(确保无在用连接) CREATE DATABASE target_db; -- 新建空白库 ``` 2. **使用命令行恢复**(避免pgAdmin路径问题) ```bash pg_restore -U postgres -d target_db -Fc backup_file.dump ``` - `-Fc`: 指定自定义格式备份文件 - 添加`-v`参数显示详细日志 3. **冲突规避标志** ```bash pg_restore --clean --if-exists ... # 恢复前删除已有对象 pg_restore --no-owner ... # 忽略备份中的所有权设置 ``` --- ### **三、预防措施** 1. **备份时排除冲突** ```bash pg_dump --clean --if-exists ... # 生成包含DROP语句的备份 ``` 2. **定期维护索引** ```sql REINDEX TABLE ws_overyears_monthly_wind; -- 修复索引损坏 ``` 3. **备份恢复沙箱测试** 在非生产环境验证备份文件的完整性和可恢复性。 > **关键提示**:若问题持续,检查PostgreSQL日志(`log_directory`配置项)获取详细错误堆栈[^1][^3]。 --- **
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值