Ubuntu上Oracle数据库的备份与恢复实践
一 环境准备与前提
su - oracle,确保ORACLE_SID、ORACLE_HOME、PATH等环境变量正确。archive log list;shutdown immediate;
startup mount;
alter database archivelog;
alter database open;
/u01/backup/rman、/u01/backup/dp),确保oracle:oinstall可读写。mkdir -p /u01/backup/dp
chown oracle:oinstall /u01/backup/dp
chmod 775 /u01/backup/dp
sqlplus / as sysdba
create directory dp_dir as '/u01/backup/dp';
grant read, write on directory dp_dir to system;
rman target /
configure controlfile autobackup on;
configure controlfile autobackup format for device type disk to '/u01/backup/rman/ctl_%F';
二 物理备份 RMAN 实战
rman target /backup as compressed backupset database plus archivelog delete input;
backup incremental level 1 database plus archivelog delete input;
backup archivelog all delete input;
backup current controlfile;
backup spfile;
list backup;
三 物理恢复 RMAN 实战
rman target /
shutdown immediate;
startup mount;
restore database;
recover database;
alter database open;
rman target /
shutdown immediate;
startup mount;
restore database;
recover database until time "to_date('2026-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')";
alter database open resetlogs;
rman target /
startup nomount;
restore controlfile from autobackup;
alter database mount;
recover database;
alter database open resetlogs;
rman target /
sqlplus / as sysdba
alter database datafile <file#> offline immediate;
exit
restore datafile <file#>;
recover datafile <file#>;
sqlplus / as sysdba
alter database datafile <file#> online;
list backup summary;、validate backupset <id>;。四 逻辑备份与恢复 Data Pump
expdp system/oracle@ORCL directory=dp_dir \
dumpfile=full_$(date +%F).dmp logfile=full_$(date +%F).log full=y
expdp system/oracle@ORCL directory=dp_dir \
dumpfile=schema_$(date +%F).dmp logfile=schema_$(date +%F).log \
schemas=SCOTT
impdp system/oracle@ORCL directory=dp_dir \
dumpfile=full_2026-01-05.dmp logfile=impdp_full.log full=y
impdp system/oracle@ORCL directory=dp_dir \
dumpfile=schema_2026-01-05.dmp logfile=impdp_schema.log \
schemas=SCOTT remap_schema=SCOTT:SCOTT_NEW \
remap_tablespace=USERS:USERS_NEW
五 自动化与运维建议
#!/bin/bash
export ORACLE_SID=ORCL
export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH
BACKUP_DIR=/u01/backup/rman
LOG_FILE=$BACKUP_DIR/rman_backup_$(date +%F).log
RETENTION_DAYS=7
mkdir -p $BACKUP_DIR
rman target / <<'EOF' >> $LOG_FILE 2>&1
configure retention policy to recovery window of $RETENTION_DAYS days;
backup as compressed backupset database plus archivelog delete input;
backup current controlfile;
delete noprompt obsolete;
EOF
# 清理过期备份文件(按文件系统时间)
find $BACKUP_DIR -name "*.bkp" -mtime +$RETENTION_DAYS -delete
chmod +x /u01/scripts/rman_backup.sh
crontab -e
0 2 * * * /u01/scripts/rman_backup.sh
list backup summary;与validate backupset;