大家好,又见面了,我是你们的朋友全栈君。
1、物理环境准备
说明:这里的Oracle服务器是linux系统,windows系统也是一样的!!
一、冷备份
1、停止数据库
代码语言:javascript复制#连接数据库,用超管用户连接(默认sys)
root@ed8824e05a4c:/# sqlplus / as sysdba
Enter user-name: sys
Enter password: //输入密码格式 '你的密码 as sysdba'
SQL> shutdown immediate //停止数据库
Database closed.
Database dismounted.
ORACLE instance shut down.
#出现以上表明你的数据库停止成功!!
2、备份数据
代码语言:javascript复制#进到数据库存放的路径(我这里是/u01/app/oracle/oradata)
root@ed8824e05a4c:/u01/app/oracle/oradata# ls
XE
#拷贝数据库文件到你备份的文件夹
root@ed8824e05a4c:/u01/app/oracle/oradata# cp -r / /BACKEUP
#查看备份的文件
root@ed8824e05a4c:/# ls BACKEUP/
docker-entrypoint-initdb.d mnt srv tmp u01
#这样就备份成功了!!
#注意如果不知道存放路径可用下面命令查询(必须以超管登录)
select * from DBA_DATA_FILES;
3、启动数据库
代码语言:javascript复制SQL> startup
ORACLE instance started.
Total System Global Area 601272320 bytes
Fixed Size 2228848 bytes
Variable Size 188747152 bytes
Database Buffers 406847488 bytes
Redo Buffers 3448832 bytes
Database mounted.
Database opened.
#数据库启动成功!!
注意:如果是windows服务器,直接到服务里面把以Oracle开头的服务直接停止就可以了,然后直接拷贝数据库文件就可以了。
优点:简单粗暴。
缺点:需要停库影响业务,备份时间长、占用空间大。
2、数据泵备份
1、创建备份文件夹并授权
代码语言:javascript复制#在本地创建备份文件夹
oracle@ed8824e05a4c:~$ mkdir /u01/app/oracle/backup_data
#连接数据库,用超管用户连接(默认sys)
oracle@ed8824e05a4c:~$ sqlplus / as sysdba
SQL> create or replace directory backup as '/u01/app/oracle/backup_data'; //这个目录必须和上面那个保持一致
Directory created.
#授权
SQL> grant read,write on directory backup to system;
Grant succeeded.
#退出
SQL> exit
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
2、开始备份
代码语言:javascript复制#执行备份命令
oracle@ed8824e05a4c:~$ expdp system/oracle@XE directory=backup dumpfile=beifen.dmp logfile=beifen.log full=y
#说明'oracle'是我的数据库密码,你修改成你的密码。XE是我的数据库名字,你也修改成你的,备份数据文件和日志文件名字可自主命名,我这里是'beifen',但后缀必须是以.dmp和.log结尾。
#开始备份
oracle@ed8824e05a4c:~$ expdp system/oracle@XE directory=backup dumpfile=beifen.dmp logfile=beifen.log full=y
Export: Release 11.2.0.2.0 - Production on Fri Dec 17 08:56:09 2021
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
Starting "SYSTEM"."SYS_EXPORT_FULL_01": system/********@XE directory=backup dumpfile=beifen.dmp logfile=beifen.log full=y
Estimate in progress using BLOCKS method...
Processing object type DATABASE_EXPORT/SCHEMA/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 161.3 MB
Processing object type DATABASE_EXPORT/TABLESPACE
Processing object type DATABASE_EXPORT/PROFILE
Processing object type DATABASE_EXPORT/SYS_USER/USER
Processing object type DATABASE_EXPORT/SCHEMA/USER
Processing object type DATABASE_EXPORT/ROLE
Processing object type DATABASE_EXPORT/GRANT/SYSTEM_GRANT/PROC_SYSTEM_GRANT
Processing object type DATABASE_EXPORT/SCHEMA/GRANT/SYSTEM_GRAN
Master table "SYSTEM"."SYS_EXPORT_FULL_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SYSTEM.SYS_EXPORT_FULL_01 is:
/u01/app/oracle/backup_data/beifen.dmp
Job "SYSTEM"."SYS_EXPORT_FULL_01" successfully completed at 08:58:46
#查看备份数据
oracle@ed8824e05a4c:~/backup_data$ ls
beifen.dmp beifen.log
#这就备份成功了
注意:用户不能是root,不然会报错!!
缺点:数据量大时备份时间较长。
优点:无需停库不影响业务等。
3、rman自动备份和归档日志备份
说明:rman备份前提时数据库是归档模式,若不是归档模式,则需要修改为归档模式,再进行备1份。
1、将数据库修改为归档模式
代码语言:javascript复制#连接数据库
oracle@ed8824e05a4c:~$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.2.0 Production on Fri Dec 17 09:08:13 2021
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL>
#查看日志是否是归档模式
SQL> archive log list
Database log mode No Archive Mode //如果是NO Archive Mode即为非归档模式
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 10
Current log sequence 11
SQL>
#停止数据库
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
#将数据库启动之装载状态
SQL> startup mount
ORACLE instance started.
Total System Global Area 601272320 bytes
Fixed Size 2228848 bytes
Variable Size 209718672 bytes
Database Buffers 385875968 bytes
Redo Buffers 3448832 bytes
Database mounted.
SQL>
#将日志改为归档模式
SQL> alter database archivelog;
Database altered.
SQL>
#打开数据库
SQL> alter database open;
Database altered.
SQL>
#再次查看日志是否为归档模式
SQL> archive log list
Database log mode Archive Mode //Archive Mode即为归档模式
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 10
Next log sequence to archive 11
Current log sequence 11
SQL>
2.归档日志备份
代码语言:javascript复制#创建归档日志文件备份目录
oracle@ed8824e05a4c:~$ mkdir /u01/app/oracle/archivelog/ //这是在本地创建;
#在数据库中创建备份目录,必须和本地一样
SQL> alter system set log_archive_dest_1="location=/u01/app/oracle/archivelog";
System altered.
#强制归档
SQL> alter system switch logfile;
System altered.
#在本地查看备份的归档日志
oracle@ed8824e05a4c:~/archivelog$ ls
1_11_1000225551.dbf //这就是备份的归档日志
#这样就备份完成了。
3、设置rman自动备份
代码语言:javascript复制#创建备份脚本rman.sh
#!/bin/bash
rman target / msglog /home/oracle/rman_back/rman_log.log << EOF
CROSSCHECK BACKUP;
DELETE NOPROMPT EXPIRED BACKUP;
crosscheck archivelog all;
delete noprompt expired archivelog all;
RUN {
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 2 DAYS;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO "/home/oracle/rman_back/%F";
ALLOCATE CHANNEL CH1 DEVICE TYPE DISK FORMAT "/home/oracle/rman_back/%U";
BACKUP DATABASE SKIP INACCESSIBLE FILESPERSET 10 PLUS ARCHIVELOG FILESPERSET 20 DELETE ALL INPUT;
RELEASE CHANNEL CH1;
}
ALLOCATE CHANNEL FOR MAINTENANCE DEVICE TYPE DISK;
CROSSCHECK BACKUPSET;
DELETE NOPROMPT OBSOLETE;
exit
EOF
#添加执行权限并执行脚本
oracle@ed8824e05a4c:~$ chmod x rman.sh
oracle@ed8824e05a4c:~$ ./rman.sh
RMAN> RMAN> RMAN> RMAN> RMAN> 2> 3> 4> 5> 6> 7> 8>
#查看日志和备份文件
oracle@ed8824e05a4c:~$ cd /home/oracle/rman_back/
oracle@ed8824e05a4c:~$ ls
030gvm9l_1_1 0b0gvqkb_1_1 0g0gvqul_1_1 rman_log.log
050gvmr1_1_1 0d0gvqtk_1_1 0h0gvquu_1_1
060gvmr2_1_1 0e0gvqtl_1_1 c-1617341554-20211217-00
090gvmts_1_1 0f0gvque_1_1 c-1617341554-20211217-01
#至此rman备份完成
#设置计划任务
oracle@ed8824e05a4c:~$ crontab -l -uoracle
00 02 * * 6 /home/oracle/rman.sh
#这里设置是每周六晚上02点执行备份,可根据自己需求自行设置!!!
到此Oracle常用的备份方式介绍完毕,使用过程中如有问题私信!!!
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158225.html原文链接:https://javaforall.cn