以后改行了或老了回头看看,我曾经会这些,也是件愉快的事
【备份】
--创建目录
[oracle@test20 backup]$ mkdir -p /home/oracle/backup
--备份脚本
[oracle@test20 backup]$ cat backup2.sh
#!/bin/bash
source /home/oracle/.bash_profile
$ORACLE_HOME/bin/rman target / log=/home/oracle/backup/backup_`date +%m%d`.log append <
run {
allocate channel ch1 device type disk;
allocate channel ch2 device type disk;
allocate channel ch3 device type disk;
allocate channel ch4 device type disk;
sql 'alter system archive log current';
#backup database format '/home/oracle/backup/data_%d_%T_%U.bak';
#compressed
backup as compressed backupset database format '/home/oracle/backup/data_%d_%T_%U.bak';
#backup archivelog all format '/home/oracle/backup/arch_%d_%T_%U.bak';
#compressed
backup as compressed backupset archivelog all format '/home/oracle/backup/arch_%d_%T_%U.bak';
#for standby
#backup current controlfile for standby format '/home/oracle/backup/ctl_%d_%T_%U.bak';
#for restore
backup current controlfile format '/home/oracle/backup/ctl_%d_%T_%U.bak';
release channel ch1;
release channel ch2;
release channel ch3;
release channel ch4;
}
EOF
--授权
[oracle@test20 backup]$ chmod +x backup.sh
--备份
[oracle@test20 backup]$ ./backup.sh
--传输
【恢复】
--创建目录。机器已装oracle软件,DB已关闭
mkdir -p /u01/app/oracle/admin/payme/{adump,bdump,cdump,dpdump,udump,pfile}
mkdir -p /u01/app/oracle/oradata/payme
mkdir -p /u01/app/oracle/fast_recovery_area/payme
--设置参数文件和SID
echo 'db_name=payme' > $ORACLE_HOME/dbs/initpayme.ora
more $ORACLE_HOME/dbs/initpayme.ora
export ORACLE_SID=payme
rman target /
--原始数据库dbid payme (DBID=1239133617) 从RMAN备份日志查下
set dbid 1239133617
--启动
SQL> startup nomount;
--恢复控制文件
rman target /
RMAN> restore controlfile from '/home/oracle/backup/ctl_payme_20191108_35ugc7rl_1_1.bak';
...
#会输出新的控制文件
output file name=/u01/app/oracle/oradata/datafile/TEST154/controlfile/o1_mf_grv284j3_.ctl
output file name=/u01/app/oracle/oradata/flash_recovery_area/TEST154/controlfile/o1_mf_grv284k0_.ctl
--启动实例到mount状态
alter database mount;
--检查备份有效性
crosscheck backup;
--在新的控制文件中注册数据文件备份和归档备份。结尾处要带/,且不能有空格,注意文件权限
catalog start with '/home/oracle/backup/';
--主库执行,生成rename SQL,替换成对应数据文件路径
col AA FOR a200
SET pagesize 100
SET line 200
SELECT 'set newname for datafile ' || A.FILE# || ' to "' || A.NAME || '";' AS AA
FROM V$DATAFILE A
UNION ALL
SELECT 'set newname for tempfile ' || A.FILE# || ' to "' || A.NAME || '";' AS AA
FROM V$TEMPFILE A;
--开始恢复
RMAN> run
{
allocate channel ch1 device type disk;
allocate channel ch2 device type disk;
allocate channel ch3 device type disk;
allocate channel ch4 device type disk;
set newname for datafile 1 to "/u01/app/oracle/oradata/payme/system01.dbf";
set newname for datafile 2 to "/u01/app/oracle/oradata/payme/sysaux01.dbf";
set newname for datafile 3 to "/u01/app/oracle/oradata/payme/undotbs01.dbf";
set newname for datafile 4 to "/u01/app/oracle/oradata/payme/tbs_1.dbf";
set newname for datafile 5 to "/u01/app/oracle/oradata/payme/users01.dbf";
set newname for datafile 6 to "/u01/app/oracle/oradata/payme/example01.dbf";
...
set newname for tempfile 1 to "/u01/app/oracle/oradata/payme/temp01.dbf";
set newname for tempfile 2 to "/u01/app/oracle/oradata/payme/temp_01.dbf";
set newname for tempfile 3 to "/u01/app/oracle/oradata/payme/TEMP21.dbf";
set newname for tempfile 4 to "/u01/app/oracle/oradata/payme/temp_pfpt.dbf";
release channel ch1;
release channel ch2;
release channel ch3;
release channel ch4;
restore database;
switch datafile all;
switch tempfile all;
recover database;
}
renamed tempfile 1 to /u01/app/oracle/oradata/payme/temp01.dbf in control file
renamed tempfile 2 to /u01/app/oracle/oradata/payme/temp_01.dbf in control file
Starting recover at 2019-11-08 16:17:31
using channel ORA_DISK_1
starting media recovery
channel ORA_DISK_1: starting archived log restore to default destination
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=486
channel ORA_DISK_1: reading from backup piece /home/oracle/backup/arch_payme_20191108_34ugc7r6_1_1.bak
channel ORA_DISK_1: piece handle=/home/oracle/backup/arch_payme_20191108_34ugc7r6_1_1.bak tag=TAG20191108T160349
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
archived log file name=/u01/app/oracle/oradata/flash_recovery_area/arch_1021737825_1_486.arc thread=1 sequence=486
unable to find archived log
archived log thread=1 sequence=487
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 11/08/2019 16:17:33
RMAN-06054: media recovery requesting unknown archived log for thread 1 with sequence 487 and starting SCN of 69884756
【处理redo log和temp】
--如果实例安装路径不同,或者redo log和临时表空间对应的文件在目标服务器上找不到
SQL> col status for a7
SQL> col type for a7;
SQL> col member for a64;
SQL> select * from v$logfile;
GROUP# STATUS TYPE MEMBER IS_RECOVE
---------- ------- ------- ---------------------------------------------------------------- ---------
1 ONLINE /u01/app/oracle/oradata/dbtest/actlog/redo01.log NO
2 ONLINE /u01/app/oracle/oradata/dbtest/actlog/redo02.log NO
3 ONLINE /u01/app/oracle/oradata/dbtest/actlog/redo03.log NO
show parameter standby_file_management;
--alter system set standby_file_management=MANUAL scope=both;
--新、旧redo日志路径一致直接clear就可以,自动生成新的redo日志。不一致了先执行alter
alter database rename file '/u01/app/oracle/oradata/dbtest/actlog/redo01.log' to '/u01/app/oracle/oradata/flash_recovery_area/payme/onlinelog/redo01.log';
alter database clear logfile group 1;
alter database rename file '/u01/app/oracle/oradata/dbtest/actlog/redo02.log' to '/u01/app/oracle/oradata/flash_recovery_area/payme/onlinelog/redo02.log';
alter database clear logfile group 2;
alter database rename file '/u01/app/oracle/oradata/dbtest/actlog/redo03.log' to '/u01/app/oracle/oradata/flash_recovery_area/payme/onlinelog/redo03.log';
alter database clear logfile group 3;
--resetlogs,观察alter日志看是否有异常
SQL> alter database open resetlogs;
若TEMP文件路径不一致,先drop再add,此时DB要open才可以
SQL> col name for a80;
SQL> select name from v$tempfile;
NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/payme/temp01.dbf
/u01/app/oracle/oradata/payme/temp_01.dbf
alter database tempfile '/u01/app/oracle/oradata/payme/temp01.dbf' drop;
alter database tempfile '/u01/app/oracle/oradata/payme/temp_01.dbf' drop;
/*
--ORA-01109: 数据库未打开 需要在open状态下添加
alter tablespace temp add tempfile '/u01/app/oracle/oradata/payme/temp01.dbf' size 100M autoextend on next 10M maxsize 4G;
alter tablespace temp add tempfile '/u01/app/oracle/oradata/payme/temp_01.dbf' size 100M autoextend on next 10M maxsize 4G;
*/
【创建spfile】
create spfile from pfile='$ORACLE_HOME/dbs/initpayme.ora';
create pfile from spfile;
【配置环境变量】
vi .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
umask 022
export ORACLE_HOSTNAME=test154
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
#export ORACLE_SID=test154
export ORACLE_SID=payme
export PATH=.:$ORACLE_HOME/bin:$ORACLE_HOME/OPatch:$ORACLE_HOME/jdk/bin:$PATH
export LC_ALL="en_US"
export LANG="zh_CN.UTF-8"
export NLS_LANG="SIMPLIFIED CHINESE_CHINA.AL32UTF8"
export NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"
alias sql="sqlplus '/as sysdba'"
set line 200
set pagesize 200
stty erase ^h
set _EDITOR=vi
alias sqlplus='rlwrap sqlplus'
alias rman='rlwrap rman'
alias dbs='cd /u01/app/oracle/product/11.2.0/dbhome_1/dbs/'
alias admin='cd /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/'
alias oralog='cd /u01/app/oracle/diag/rdbms/payme/payme/trace/'
【修改TNSNAME和LISTENER】
--TNSNAME
payme =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL= TCP)(HOST = 192.168.xx.154)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME =payme)
)
)
--LISTENER
LISTENER=
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST =192.168.xx.154)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY =EXTPROC1521))
)
)
SID_LIST_LISTENER=
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = payme)
(ORACLE_HOME =/u01/app/oracle/product/11.2.0/dbhome_1)
(SID_NAME = payme)
)
)
ADR_BASE_LISTENER = /u01/app/oracle