前提
必须开启归档
使用SYSDBA登录数据库manager工具,输入以下SQL语句,并执行
#修改到mount状态(不用像oracle那样shutdown关库)
alter database mount;
#开启归档
alter database archivelog;
#设置归档参数
alter database add archivelog 'type=local,dest=/dm8/arch,file_size=1024,space_limit=4096';
#修改到open状态
alter database open;
#查看归档模式
select name,status$,arch_mode from v$database;
#查看归档文件
select * from sys.v$arch_file;
#查看归档配置
select * from v$dm_arch_ini;
创建作业环境
#作业环境初始化
SP_INIT_JOB_SYS(1); #创建 SYSJOB 模式、系统表和系统视图
图形化界面创建定时备份
创建定时备份
1.右击管理工具-[代理]-[作业]-[新建作业]
2.出现如下图所示界面,在作业名称和作业描述中填写备份名称和描述:
3.在作业步骤中选择具体的备份方式,如下图所示:
4.在作业调度中选择备份的时间策略,如下图所示:
5.在作业步骤中选择具体的备份方式,如下图所示:
6.在作业调度中选择备份的时间策略,如下图所示:
7.在作业步骤中选择具体的备份方式,如下图所示:
8.在作业调度中选择备份的时间策略,如下图所示:
定时备份日志查看
右击管理工具-[代理]-[作业]-[job名称],点击[查看历史作业信息]
定时备份脚本
windows 定时备份脚本
1.每周六晚上 23:30 定时全库备份
call SP_CREATE_JOB('back_full',1,0,'',0,0,'',0,'back_full');
call SP_JOB_CONFIG_START('back_full');
call SP_ADD_JOB_STEP('back_full', 'back_full', 6, '01000000D:\dmdbms8\data\DAMENG81\bak\FULL', 1, 2, 0, 0, NULL, 0);
call SP_ADD_JOB_SCHEDULE('back_full', 'back_full', 1, 2, 1, 64, 0, '23:30:00', NULL, '2020-03-27 16:35:56', NULL, '');
call SP_JOB_CONFIG_COMMIT('back_full');
2.每周一、周二、周三、周四、周五、周日晚上 23:30 定时增量备份
call SP_CREATE_JOB('backup_inc',1,0,'',0,0,'',0,'backup_inc');
call SP_JOB_CONFIG_START('backup_inc');
call SP_ADD_JOB_STEP('backup_inc', 'backup_inc', 6, '11000000D:\dmdbms8\data\DAMENG81\bak\FULL|D:\dmdbms8\data\DAMENG81\bak\FULL', 1, 2, 0, 0, NULL, 0);
call SP_ADD_JOB_SCHEDULE('backup_inc', 'backup_inc', 1, 2, 1, 63, 0, '23:30:00', NULL, '2020-03-27 22:03:58', NULL, '');
call SP_JOB_CONFIG_COMMIT('backup_inc');
3.每天 00:00 删除 7 天以前的数据
call SP_CREATE_JOB('JOB_DEL_BAK_TIMELY',1,0,'',0,0,'',0,'JOB_DEL_BAK_TIMELY');
call SP_JOB_CONFIG_START('JOB_DEL_BAK_TIMELY');
call SP_ADD_JOB_STEP('JOB_DEL_BAK_TIMELY', 'JOB_DEL_BAK_TIMELY', 0, 'call SF_BAKSET_BACKUP_DIR_ADD(''DISK'', ''D:\dmdbms8\data\DAMENG81\bak\FULL'');
CALL SP_DB_BAKSET_REMOVE_BATCH(''DISK'', NOW()-7);', 1, 2, 0, 0, NULL, 0);
call SP_ADD_JOB_SCHEDULE('JOB_DEL_BAK_TIMELY', 'JOB_DEL_BAK_TIMELY', 1, 1, 1, 0, 0, '00:00:00', NULL, '2020-03-27 22:06:13', NULL, '');
call SP_JOB_CONFIG_COMMIT('JOB_DEL_BAK_TIMELY');
linux 定时备份脚本
全量备份
1.每周六凌晨1:05 定时全库备份
--创建代理环境
SP_INIT_JOB_SYS(1);
--创建备份目录
mkdir -p /dmbak/fuwa
--创建全量备份
call SP_CREATE_JOB('bak_full',1,0,'',0,0,'',0,'周六全量备份,并删除30天之前的备份。');
call SP_JOB_CONFIG_START('bak_full');
call SP_JOB_SET_EP_SEQNO('bak_full', 0);
call SP_ADD_JOB_STEP('bak_full', 'bak_full', 6, '01000000/dmbak/DAMENG', 3, 1, 0, 0, NULL, 0);
call SP_ADD_JOB_STEP('bak_full', 'bak_del', 0, 'CALL SF_BAKSET_BACKUP_DIR_ADD(''DISK'',''/dmbak/DAMENG'');
CALL SP_DB_BAKSET_REMOVE_BATCH(''DISK'',NOW()-30);', 1, 1, 0, 0, NULL, 0);
call SP_ADD_JOB_SCHEDULE('bak_full', 'bak_full', 1, 2, 1, 64, 0, '01:05:00', NULL, '2020-01-01 00:00:00', NULL, '');
call SP_JOB_CONFIG_COMMIT('bak_full');
--查询全量备份作业
SELECT NAME,
describe
FROM sysjob.sysjobs;
SELECT top 1 NAME,
command
FROM "SYSJOB"."SYSJOBSTEPS";
命令解释
#创建作业,1启用
call SP_CREATE_JOB('bak_full',1,0,'',0,0,'',0,'周六全量备份,并删除30天之前的备份。');
#配置作业启动
call SP_JOB_CONFIG_START('bak_full');
#配置作业执行节点,默认控制节点执行
call SP_JOB_SET_EP_SEQNO('bak_full', 0);
#增加作业步骤 6:表示执行基于 V2.0 版本的备份还原 0:完全备份1:压缩 3:表示报告步骤执行成功,并执行下一步1:表示报告步骤执行失败,并结束作业
call SP_ADD_JOB_STEP('bak_full', 'bak_full', 6, '01000000/dmbak/DAMENG', 3, 1, 0, 0, NULL, 0);
#增加作业步骤0:表示执行一段 SQL 语句或者是语句块
call SP_ADD_JOB_STEP('bak_full', 'bak_del', 0, 'CALL SF_BAKSET_BACKUP_DIR_ADD(''DISK'',''/dmbak/DAMENG'');
#批量删除备份集
CALL SP_DB_BAKSET_REMOVE_BATCH(''DISK'',NOW()-30);', 1, 1, 0, 0, NULL, 0);
#增加作业调度 1:启用;2:按周的频率来执行;当 TYPE=2 时,表示的是每几个星期执行,取值范围没有限制表示不同调度类型下的发生频率;
call SP_ADD_JOB_SCHEDULE('bak_full', 'bak_full', 1, 2, 1, 64, 0, '01:05:00', NULL, '2020-01-01 00:00:00', NULL, '');
例如,选中周六,7 位二进制就是 1000000,转化成十进制就是 63,所以 FREQ_SUB_INTERVAL 就取值 63。
#结束作业配置
call SP_JOB_CONFIG_COMMIT('bak_full');
增量备份
2.每周一、周二、周三、周四、周五、周日晚上1:05定时增量备份
call SP_CREATE_JOB('bak_inc',1,0,'',0,0,'',0,'周日到周五做增量备份,如果失败,清除8天前备份,做全量备份');
call SP_JOB_CONFIG_START('bak_inc');
call SP_JOB_SET_EP_SEQNO('bak_inc', 0);
call SP_ADD_JOB_STEP('bak_inc', 'bak_inc', 6, '11000000/dmbak/DAMENG|/dmbak/DAMENG', 1, 3, 2, 6, NULL, 0);
call SP_ADD_JOB_STEP('bak_inc', 'bak_del', 0, 'SF_BAKSET_BACKUP_DIR_ADD(''DISK'',''/dmbak/DAMENG'');
CALL SP_DB_BAKSET_REMOVE_BATCH(''DISK'',NOW()-8);', 3, 1, 0, 0, NULL, 0);
call SP_ADD_JOB_STEP('bak_inc', 'switch_bak_full', 6, '01000000/dmbak/DAMENG', 1, 1, 0, 0, NULL, 0);
call SP_ADD_JOB_SCHEDULE('bak_inc', 'bak_inc', 1, 2, 1, 63, 0, '01:05:00', NULL, '2020-01-01 00:00:00', NULL, '');
call SP_JOB_CONFIG_COMMIT('bak_inc');
--查看作业
SELECT NAME,
describe
FROM sysjob.sysjobs;
SELECT top 1 NAME,
command
FROM "SYSJOB"."SYSJOBSTEPS";
命令解释
--创建增量备份作业
call SP_CREATE_JOB('bak_inc',1,0,'',0,0,'',0,'周日到周五做增量备份,如果失败,清除8天前备份,做全量备份');
--配置作业启动
call SP_JOB_CONFIG_START('bak_inc');
--配置作业执行节点,默认控制节点执行
call SP_JOB_SET_EP_SEQNO('bak_inc', 0);
--增加作业步骤 6:表示执行基于 V2.0 版本的备份还原 1:完全备份1:压缩 基备份路径与备份路径 3:表示报告步骤执行成功,并执行下一步1:表示报告步骤执行失败,并结束作业
call SP_ADD_JOB_STEP('bak_inc', 'bak_inc', 6, '11000000/dmbak/DAMENG|/dmbak/DAMENG', 1, 3, 2, 6, NULL, 0);
call SP_ADD_JOB_STEP('bak_inc', 'bak_del', 0, 'SF_BAKSET_BACKUP_DIR_ADD(''DISK'',''/dmbak/DAMENG'');
--批量删除备份集
CALL SP_DB_BAKSET_REMOVE_BATCH(''DISK'',NOW()-8);', 3, 1, 0, 0, NULL, 0);
--增加作业调度 6:表示执行基于 V2.0 版本的备份还原
call SP_ADD_JOB_STEP('bak_inc', 'switch_bak_full', 6, '01000000/dmbak/DAMENG', 1, 1, 0, 0, NULL, 0);
-1:按天的频率来执行 当 TYPE=1 时,表示每几天执行,取值范围为 1~100
call SP_ADD_JOB_SCHEDULE('bak_inc', 'bak_inc', 1, 2, 1, 63, 0, '01:05:00', NULL, '2020-01-01 00:00:00', NULL, '');
例如,选中周一至周五和周日,7 位二进制就是 0111111,转化成十进制就是 63,所以 FREQ_SUB_INTERVAL 就取值 63。
--结束作业配置
call SP_JOB_CONFIG_COMMIT('bak_inc');
归档备份
3.每天03:05点整进行
call SP_CREATE_JOB('bak_arch',1,0,'',0,0,'',0,'每天备份归档,删除30天之前的备份');
call SP_JOB_CONFIG_START('bak_arch');
call SP_JOB_SET_EP_SEQNO('bak_arch', 0);
call SP_ADD_JOB_STEP('bak_arch', 'bak_arch', 6, '30000000/dmbak/DAMENG', 3, 0, 0, 0, NULL, 0);
call SP_ADD_JOB_STEP('bak_arch', 'bak_del', 0, 'CALL SF_BAKSET_BACKUP_DIR_ADD(''DISK'',''/dmbak/DAMENG'');
CALL SP_ARCH_BAKSET_REMOVE_BATCH(''DISK'',NOW()-30);', 0, 0, 0, 0, NULL, 0);
call SP_ADD_JOB_SCHEDULE('bak_arch', 'back_arch', 1, 1, 1, 0, 0, '03:05:00', NULL, '2023-05-24 18:16:05', NULL, '');
call SP_JOB_CONFIG_COMMIT('bak_arch');
创建收集统计信息的作业
周六凌晨2点收集统计信息
call SP_CREATE_JOB('statistics',1,0,'',0,0,'',0,'');
call SP_JOB_CONFIG_START('statistics');
call SP_JOB_SET_EP_SEQNO('statistics', 0);
call SP_ADD_JOB_STEP('statistics', 'statistics1', 0, 'begin
for rs in (select ''sf_set_SESSION_para_value(''''HAGR_HASH_SIZE'''',
(select cast( case when max(table_rowcount(owner,table_name))<=(select max_value from v$dm_ini where para_Name=''''HAGR_HASH_SIZE'''') and max(table_rowcount(owner,table_name))>=( select min_value from v$dm_ini where para_Name=''''HAGR_HASH_SIZE'''') then max(table_rowcount(owner,table_name))
when max(table_rowcount(owner,table_name))<( select min_value from v$dm_ini where para_Name=''''HAGR_HASH_SIZE'''') then (select min_value from v$dm_ini where para_Name=''''HAGR_HASH_SIZE'''')
else (select max_value from v$dm_ini where para_Name=''''HAGR_HASH_SIZE'''')
end as bigint)
from dba_tables
where owner=''''''||NAME||''''''));'' sql1,
''DBMS_STATS.GATHER_SCHEMA_STATS(''''''||NAME||'''''',100,TRUE,''''FOR ALL COLUMNS SIZE AUTO'''');'' sql2
from SYS.SYSOBJECTS
where TYPE$=''SCH'')
loop
execute immediate rs.sql1;
execute immediate rs.sql2;
end loop;
end;', 0, 0, 0, 0, NULL, 0);
call SP_ADD_JOB_SCHEDULE('statistics', 'statistics1', 1, 2, 1, 64, 0, '02:00:00', NULL, '2021-06-09 22:54:37', NULL, '');
call SP_JOB_CONFIG_COMMIT('statistics');
手动收集统计信息
##方法1:
--查看统计信息
DBMS_STATS.TABLE_STATS_SHOW('SYSDBA','TEST')
--收集指定用户下所有表所有列的统计信息:
SET LINESIZE 999
SELECT 'CALL DBMS_STATS.GATHER_SCHEMA_STATS('''|| USERNAME || ''',100,TRUE,''FOR ALL COLUMNS SIZE AUTO'');' AS "收集表的统计信息"
FROM DBA_USERS
WHERE USERNAME = 'SYSDBA';
--收集指定用户下所有索引的统计信息:
SET LINESIZE 999
SELECT 'CALL DBMS_STATS.GATHER_SCHEMA_STATS('''|| USERNAME || ''',1.0,TRUE,''FOR ALL INDEXED SIZE AUTO'');' AS "收集表的统计信息"
FROM DBA_USERS
WHERE USERNAME = 'SYSDBA';
#方法2:
begin
for rs in (select 'sf_set_SESSION_para_value(''HAGR_HASH_SIZE'',
(select cast( case when max(table_rowcount(owner,table_name))<=(select max_value from v$dm_ini where para_Name=''HAGR_HASH_SIZE'') and max(table_rowcount(owner,table_name))>=( select min_value from v$dm_ini where para_Name=''HAGR_HASH_SIZE'') then max(table_rowcount(owner,table_name))
when max(table_rowcount(owner,table_name))<( select min_value from v$dm_ini where para_Name=''HAGR_HASH_SIZE'') then (select min_value from v$dm_ini where para_Name=''HAGR_HASH_SIZE'')
else (select max_value from v$dm_ini where para_Name=''HAGR_HASH_SIZE'')
end as bigint)
from dba_tables
where owner='''||NAME||'''));' sql1,
'DBMS_STATS.GATHER_SCHEMA_STATS('''||NAME||''',100,TRUE,''FOR ALL COLUMNS SIZE AUTO'');' sql2
from SYS.SYSOBJECTS
where TYPE$='SCH')
loop
execute immediate rs.sql1;
execute immediate rs.sql2;
end loop;
end;