CentOS 上使用 SQL*Plus 的 Oracle 备份与恢复技巧
一 环境准备与常用检查
echo $ORACLE_HOME $ORACLE_SID $PATH,必要时执行 source /home/oracle/.bash_profile。mkdir -p /home/oracle/backup && chown -R oracle:oinstall /home/oracle/backup。sqlplus / as sysdba。archive log list;(建议生产库开启归档)。select file#, name, status from v$datafile;、select * from v$recover_file;、select * from v$archived_log;。select * from dba_directories;。二 逻辑备份与恢复(Data Pump 与 exp)
create directory expdp_bak_dir as '/home/oracle/backup';grant read, write on directory expdp_bak_dir to system;(按实际账号授权)。expdp system/YourPass@orcl directory=expdp_bak_dir dumpfile=full_$(date +%F).dmp logfile=full_$(date +%F).log full=y parallel=2expdp system/YourPass@orcl directory=expdp_bak_dir dumpfile=scott_$(date +%F).dmp logfile=scott_$(date +%F).log schemas=SCOTTexp system/YourPass@orcl file=/home/oracle/backup/full_$(date +%F).dmp log=/home/oracle/backup/full_$(date +%F).log full=yimpdp system/YourPass@orcl directory=expdp_bak_dir dumpfile=full_2025-08-01.dmp logfile=impdp_full.log full=yimpdp system/YourPass@orcl directory=expdp_bak_dir dumpfile=scott_2025-08-01.dmp logfile=impdp_scott.log schemas=SCOTTimp system/YourPass@orcl file=/home/oracle/backup/full_2025-08-01.dmp log=/home/oracle/backup/imp.log full=y三 手工介质恢复要点(仅用 SQL*Plus,配合 OS 拷贝)
recover database;alter database open;alter tablespace ts_name offline immediate;recover tablespace ts_name;alter tablespace ts_name online;alter database datafile N offline;recover datafile N; 4) alter database datafile N online;recover datafile N; 3) alter database open;v$recover_file、v$datafile_header、v$archived_log 可快速定位需要恢复的文件与所需日志。alter database clear logfile group <n>; 重建联机日志(存在数据丢失风险)。四 自动化与运维实践
#!/bin/bash
export ORACLE_SID=orcl
export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH
DATA_DIR=/home/oracle/backup
BAK_TIME=$(date +%F)
LOG_FILE=$DATA_DIR/full_${BAK_TIME}.log
DMP_FILE=$DATA_DIR/full_${BAK_TIME}.dmp
expdp system/YourPass@orcl directory=expdp_bak_dir \
dumpfile=$(basename $DMP_FILE) logfile=$(basename $LOG_FILE) \
full=y parallel=2
# 清理 7 天前
find $DATA_DIR -type f -mtime +7 -name "full_*.dmp" -delete
find $DATA_DIR -type f -mtime +7 -name "full_*.log" -delete
0 1 * * * /home/oracle/backup_full.sh >> /home/oracle/backup/backup.log 2>&1五 常见坑与排错清单
dba_directories 中目录存在且对执行账号授予了 READ/WRITE。sqlplus 不可用,先 source /home/oracle/.bash_profile 再执行。v$recover_file 与 v$archived_log 的输出,确保所需归档/在线日志可用。ONLINE。alter database open resetlogs(必要时)并立即做一次全备。