Ubuntu下Oracle数据库备份恢复测试指南
确认归档模式
Oracle数据库需开启归档模式才能进行完整恢复(如时间点恢复)。执行以下SQL命令检查归档状态:
SQL> ARCHIVE LOG LIST;
若未开启,需关闭数据库、启动到mount状态并开启归档:
SQL> SHUTDOWN IMMEDIATE;
SQL> STARTUP MOUNT;
SQL> ALTER DATABASE ARCHIVELOG;
SQL> ALTER DATABASE OPEN;
创建备份目录与权限
在Ubuntu服务器上创建专用备份目录(如/u01/backup),并赋予Oracle用户读写权限:
sudo mkdir -p /u01/backup
sudo chown oracle:oinstall /u01/backup
sudo chmod 700 /u01/backup
配置RMAN环境
编辑Oracle用户的.bash_profile,添加Oracle环境变量(替换为实际路径):
export ORACLE_HOME=/u01/app/oracle/product/19c/dbhome_1
export ORACLE_SID=orcl
export PATH=$ORACLE_HOME/bin:$PATH
source ~/.bash_profile
RMAN(Recovery Manager)是Oracle官方推荐的物理备份工具,支持全备份、增量备份及归档日志备份。
全库备份(含归档日志)
执行以下命令进行全库备份,并压缩备份集、交叉检查过期备份:
rman target / log=/u01/backup/rman_full_$(date +%F).log <<EOF
RUN {
ALLOCATE CHANNEL c1 TYPE DISK;
BACKUP DATABASE PLUS ARCHIVELOG DELETE INPUT;
CROSSCHECK BACKUP;
DELETE NOPROMPT EXPIRED BACKUP;
RELEASE CHANNEL c1;
}
EOF
BACKUP DATABASE PLUS ARCHIVELOG:备份数据库及所有归档日志。DELETE INPUT:备份后删除已备份的归档日志,节省空间。CROSSCHECK BACKUP:检查备份集有效性,DELETE NOPROMPT EXPIRED BACKUP:删除过期备份。自动化备份脚本
创建定时任务(cron)实现每日自动备份。示例脚本/u01/script/orcl_backup.sh:
#!/bin/bash
source /home/oracle/.bash_profile
LOG_DIR=/u01/backup/log
mkdir -p $LOG_DIR
rman target / log="$LOG_DIR/rman_full_$(date +%F).log" <<EOF
RUN {
ALLOCATE CHANNEL c1 TYPE DISK;
BACKUP DATABASE PLUS ARCHIVELOG DELETE INPUT;
CROSSCHECK BACKUP;
DELETE NOPROMPT EXPIRED BACKUP;
RELEASE CHANNEL c1;
}
EOF
find "$LOG_DIR" -name "rman_*.log" -mtime +7 -exec rm -f {} \;
添加cron任务(每天凌晨2点执行):
crontab -e
插入以下内容:
0 2 * * * /u01/script/orcl_backup.sh
恢复测试需在测试环境中进行(避免影响生产数据),步骤如下:
准备测试环境
SQL> CREATE TABLESPACE test_ts DATAFILE '/u01/oradata/test/test_ts.dbf' SIZE 100M AUTOEXTEND ON;
SQL> CREATE USER test_user IDENTIFIED BY test_pwd DEFAULT TABLESPACE test_ts TEMPORARY TABLESPACE temp;
SQL> GRANT CONNECT, RESOURCE TO test_user;
复制备份文件到测试环境
将生产环境的备份文件(数据文件、归档日志、控制文件、参数文件)复制到测试环境的对应目录(如/u01/oradata/test)。可使用scp命令:
scp -r oracle@production_server:/u01/backup/* oracle@test_server:/u01/oradata/test/
恢复数据库
sqlplus / as sysdba
SQL> SHUTDOWN IMMEDIATE;
SQL> STARTUP NOMOUNT;
rman target /
RMAN> RESTORE CONTROLFILE FROM '/u01/oradata/test/autobackup/2025_10_20/o1_mf_s_1234567890_abcdef.bkp';
RMAN> ALTER DATABASE MOUNT;
RMAN> RUN {
ALLOCATE CHANNEL c1 TYPE DISK;
RESTORE DATABASE;
RECOVER DATABASE;
RELEASE CHANNEL c1;
}
RESTORE DATABASE:恢复数据文件到测试环境。RECOVER DATABASE:应用归档日志,使数据库达到一致状态。SQL> ALTER DATABASE OPEN RESETLOGS;
RESETLOGS:重置重做日志,适用于不完全恢复(如恢复到指定时间点)。验证恢复结果
SQL> CONNECT test_user/test_pwd@orcl
SQL> SELECT * FROM test_table WHERE id = 100; -- 检查备份后插入的记录
SQL> SELECT TABLESPACE_NAME, STATUS FROM DBA_TABLESPACES;
SQL> SELECT FILE_NAME, STATUS FROM DBA_DATA_FILES;
rman_full_*.log),确认备份无错误;可通过LIST BACKUP命令查看备份集详情。通过以上流程,可在Ubuntu环境下完成Oracle数据库的备份恢复测试,确保数据安全与恢复能力。