Debian下Oracle数据库备份实操指南
一、备份方式总览
二、RMAN物理备份与恢复步骤
run {
allocate channel ch1 type disk;
backup as compressed backupset database format '/backup/rman/full_%U.bkp' tag 'full_$(date +%F)';
sql 'alter system archive log current';
backup as compressed backupset archivelog all format '/backup/rman/arch_%U.bkp' delete input;
backup current controlfile format '/backup/rman/ctl_%U.bkp';
backup spfile format '/backup/rman/spfile_%U.bkp';
release channel ch1;
}
run {
allocate channel ch1 type disk;
backup as compressed backupset incremental level 0 database format '/backup/rman/inc0_%U.bkp' tag 'inc0_$(date +%F)';
sql 'alter system archive log current';
backup as compressed backupset archivelog all format '/backup/rman/arch_%U.bkp' delete input;
backup current controlfile format '/backup/rman/ctl_%U.bkp';
release channel ch1;
}
run {
allocate channel ch1 type disk;
backup as compressed backupset incremental level 1 database format '/backup/rman/inc1_%U.bkp' tag 'inc1_$(date +%F)';
sql 'alter system archive log current';
backup as compressed backupset archivelog all format '/backup/rman/arch_%U.bkp' delete input;
backup current controlfile format '/backup/rman/ctl_%U.bkp';
release channel ch1;
}
rman target /
restore database;
recover database;
alter database open;
rman target /
restore database;
recover database until time "to_date('2025-11-27 10:00:00','yyyy-mm-dd hh24:mi:ss')";
alter database open resetlogs;
rman target /
restore controlfile from autobackup; -- 或 restore controlfile from '/backup/rman/ctl_*.bkp';
startup nomount;
restore spfile from autobackup; -- 或 restore spfile from '/backup/rman/spfile_*.bkp';
startup force;
三、Data Pump逻辑备份与恢复步骤
sqlplus / as sysdba
CREATE DIRECTORY backup_dir AS '/backup/dp';
GRANT READ, WRITE ON DIRECTORY backup_dir TO system;
expdp system/密码@SID DIRECTORY=backup_dir DUMPFILE=full_$(date +%F).dmp LOGFILE=full_$(date +%F).log FULL=y PARALLEL=4
impdp system/密码@SID DIRECTORY=backup_dir DUMPFILE=full_2025-11-27.dmp LOGFILE=imp_$(date +%F).log FULL=y PARALLEL=4
四、自动化与运维要点
#!/bin/bash
export ORACLE_SID=your_sid
export ORACLE_HOME=/u01/app/oracle/product/19.0.0.0/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH
BACKUP_DIR=/backup/rman
LOG_DIR=/backup/log
TS=$(date +%F_%H%M%S)
$ORACLE_HOME/bin/rman target / log=$LOG_DIR/rman_full_$TS.log <<'EOF'
run {
allocate channel ch1 type disk;
backup as compressed backupset database format '${BACKUP_DIR}/full_${TS}_%U.bkp' tag 'full_${TS}';
sql 'alter system archive log current';
backup as compressed backupset archivelog all format '${BACKUP_DIR}/arch_${TS}_%U.bkp' delete input;
backup current controlfile format '${BACKUP_DIR}/ctl_${TS}_%U.bkp';
backup spfile format '${BACKUP_DIR}/spfile_${TS}_%U.bkp';
release channel ch1;
crosscheck backup;
delete noprompt expired backup;
delete noprompt obsolete;
}
EOF
0 2 * * * /bin/su - oracle -c "/backup/script/rman_backup.sh" >> /backup/log/rman_cron.log 2>&1
0 2 * * 0 /bin/su - oracle -c "/backup/script/rman_level0.sh"
0 2 * * 1-6 /bin/su - oracle -c "/backup/script/rman_level1.sh"
五、常见问题与排错清单