艾丽游戏ing

数据库备份脚本 数据库备份方法

艾丽游戏ing 1

1、在ECS上使用mysqldump进行逻辑备份,再中转到本地服务器。使用ECS中转是为了避免将RDS暴露到公网上。

数据库备份脚本 数据库备份方法数据库备份脚本 数据库备份方法


2、在RDS控制台上的“备份恢复”中找到“数据备份”,七天内的全量备份是带有下载地址的。点击后会产生一个24小时有效的下载地址。下载回去后按照文档说明进行恢复。

@echo off

echo ================================================

echo Windows环境下Oracle数据库的自动备份脚本

echo 1. 使用当前日期命名备份文件。

echo 2. 自动删除7天前的备份。

echo ================================================

::以“YYYYMMDD”格式取出当前时间。

set BACKUPDATE=%date:~0,4%%date:~5,2%%date:~8,2%

::设置用户名、密码和要备份的数据库。

set USER=xxx

set PASSWORD=123456

set DATABASE=dbtest

::创建备份目录。

if not exist "D:\backup\data" mkdir D:\backup\data

if not exist "D:\backup\log" mkdir D:\backup\log

set DATADIR=D:\backup\data

set LOGDIR=D:\backup\log

exp %USER%/%PASSWORD%@%DATABASE% file=%DATADIR%\data_%BACKUPDATE%.dmp log=%LOGDIR%\log_%BACKUPDATE%.log

::删除7天前的备份。

forfiles /p "%DATADIR%" /s /m *.* /d -7 /c "cmd /c del @path"

forfiles /p "%LOGDIR%" /s /m *.* /d -7 /c "cmd /c del @path"

exit

在ORACLE数据库中,RMAN备份的脚本非常多,下面介绍一例shell脚本如何通过RMAN备份,以及FTP上传RMAN备份文件以及归档日志文件的脚本。

fullback.sh 里面调用RMAN命令做数据库备份,它使用的cmdfile为/home/oracle/backup/bin/fullback.rcv,同时在/home/oracle/backup/logs目录下生成日志文件。

1: [oracle@DB-Server bin]$ more fullback.sh

2:

3: #!/bin/bash

4:

5: export ORACLE_BASE=/u01/app/oracle

6:

7: export ORACLE_SID=gps

8:

9: ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1; export ORACLE_HOME

10:

11: TMP=/tmp; export TMP

12:

13: TMPDIR=$TMP; export TMPDIR

14:

15: PATH=/usr/sbin:$PATH; export PATH

16:

17: PATH=$ORACLE_HOME/bin:$PATH; export PATH

18:

19: LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH

20:

21: CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib;

22:

23: export CLASSPATH

24:

25: TODAY=`date +%Y_%m_%d`

26:

27: rman nocatalog target / cmdfile /home/oracle/backup/bin/fullback.rcv log /home/oracle/backup/logs/fullbackup_$TODAY.log

28:

29: /home/oracle/backup/bin/ftpbackup.sh

30:

fullback.rcv文件非常简单, 如下所示:

1: [oracle@DB-Server bin]$ more /home/oracle/backup/bin/fullback.rcv

2:

3: run{

4:

5: allocate channel c4 type disk;

6:

7: backup as compressed backupset

8:

9: skip inaccessible

10:

11: tag fullbackupwitharchivelog

12:

13: (database);

14:

15: backup current controlfile;

16:

17: backup spfile;

18:

19: sql "alter system archive log current";

20:

21: delete noprompt obsolete;

22:

23: release channel c4;

24:

25: }

26:

RMAN生成的备份文件,需要通过FTP上传到FTP服务器,一则数据库服务器没有这么多空间存储多天的备份,二则是出于容灾、数据安全需要。

下面脚本中FTP服务器,用户名密码均使用xxx替代,在实际环境中,使用具体的信息替代即可。

1: [oracle@DB-Server bin]$ more ftpbackup.sh

2:

3: #!/bin/sh、

4:

5: rm -f date_yesterday=`date -d'yesterday' +%Y_%m_%d`

8:

9: date_today=`date +%Y_%m_%d`

10:

11: echo "default login xxxx password xxxxxx" >> echo "macdef init" >> echo "binary" >> echo "cd archivelog" >> echo "mkdir $date_yesterday" >> echo "cd $date_yesterday" >> echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_yesterday" >> echo "mput *" >> echo "cd .." >> echo "mkdir $date_today" echo "cd $date_today" echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_today" echo "mput * " echo "cd .." echo "cd ../backupset" >> echo "mkdir $date_today" >> echo "cd $date_today" >> echo "lcd /u04/flash_recovery_area/gps/backupset/$date_today" >> echo "mput *" >> echo "cd .." >> echo "cd ../autobackup" >> echo "mkdir $date_today" >> echo "cd $date_today" >> echo "lcd /u04/flash_recovery_area/gps/autobackup/$date_today" >> echo "mput *" >> echo "quit" >> echo "" >> chmod 600 ftp -i -v xxx.xxx.xxx.xxx 8021 >>/home/oracle/backup/logs/ftp$date_today.log 2>&1

68:

另外,关于归档日志也需要每隔2小时上传一次到FTP服务器,2小时上传一次归档日志的shell脚本如下所示:

1: [oracle@DB-Server bin]$ more ftp2hours.sh

2:

3: #!/bin/sh

4:

5: rm -f date_yesterday=`date -d'yesterday' +%Y_%m_%d`

8:

9: date_today=`date +%Y_%m_%d`

10:

11: echo "default login xxxx password xxxx" >> echo "macdef init" >> echo "binary" >> echo "cd archivelog" >> echo "mkdir $date_today" echo "cd $date_today" echo "lcd /u04/flash_recovery_area/gps/archivelog/$date_today" echo "mput * " echo "quit" >> echo "" >> chmod 600 ftp -i -v xxx.xxx.xxx.xxx 8021 >>/home/oracle/backup/logs/ftp2hours.$date_today.log 2>&1

34:

最后需要将RMAN备份生成的日志文件,以及FTP上传备份文件以及归档日志的记录通过邮件形式发送给DBA或系统管理员,

1: [oracle@DB-Server bin]$ more chkbackandmail.sh

2: #!/bin/bash

3: rm -f /home/oracle/backup/bin/sendmail.pl

4: date_today=`date +%Y_%m_%d`

5: subject="Oracle Backup Alert Service on $date_today"

6: content="Dear colleagues,

7:

8: Attached please find the logs of xxx(xxx.xxx.xxx.xxx) oracle database backup and transfer to FTP Server(xxx.xxx.xxx.xxx), please

9: review the file and check whether the backup succeeded or not,and double check all backups have been dumped to tape, many tha

10: nks

11:

12:

13:

14:

15: Best regards

16: Oracle Alert Services

17:

18: "

19: file="/home/oracle/backup/logs/fullbackup_$date_today.log,/home/oracle/backup/logs/ftp$date_today.log"

20: echo "#!/usr/bin/perl" >> /home/oracle/backup/bin/sendmail.pl

21: echo "use Mail::Sender;" >> /home/oracle/backup/bin/sendmail.pl

22: echo "\$sender = new Mail::Sender {smtp => 'xxx.xxx.xxx.xxx', from => }; ">> /home/oracle/backup/bin/sendmai

23: l.pl

24: echo "\$sender->MailFile({to => ">> /home/oracle/backup/bin/sendmail.pl

25: echo "" >> /home/oracle/backup/b

26: in/sendmail.pl

27: echo "subject => '$subject',">> /home/oracle/backup/bin/sendmail.pl

28: echo "msg => '$content',">> /home/oracle/backup/bin/sendmail.pl

29: echo "file => '$file'});">> /home/oracle/backup/bin/sendmail.pl

30: perl /home/oracle/backup/bin/sendmail.pl

最后在Crontab 作业里面配置调用这些shell脚本。例如如下所示,在1:01分执行fullback.sh ,每隔两个小时(例如0:50、2:50...)执行一次ftp2hours.sh, 在每天早上8:40执行chkbackandmail.sh 发送fullback.sh 以及ftp2hour.sh的执行日志记录。

对于比较重要的数据 建议用RMAN物理备份+打开归档, 你上面写的是逻辑备份,逻辑备份虽然也是有效备份,但一般会丢失备份到当前的热数据。

AskMaclean Oracle

Oracle数据库RMAN的自动备份脚本简介

Oracle数据库RMAN的自动备份脚本简介

各位同学知道Oracle数据库RMAN如何自动备份脚本嘛?下面我为大家整理了关于Oracle数据库RMAN的自动备份脚本文章,希望能为你提供帮助:

1、数据库设置为归档方式

2、数据库的备份脚本

db_full_backup.sh :数据库全备脚本

db_l0_backup.sh :数据库0级备份脚本

db_l1_backup.sh :数据库1级备份脚本

ftp.sh :数据FTP上传脚本

ftp_del.sh :数据FTP清理脚本

rman_bak.sh :数据备份主程序

3、备份原理

每周1、3、6进行0级备份

每周日、2、4、5进行1级备份

备份文件上传到FTP服务器

FTP服务器每周清理一次,但是清理后将周六和周日的备份进行保留(6.bak和0.bak)

所有工作防暑crontab中自动执行备份

4、备份目录含义

arc :数据库归档目录

rmanbak :数据库备份文件的保存目录

rmanscripts :数据库脚本存放路径

5、FTP目录

ftp上必须手动建立目录

L0:

---1

---3

---6

---6.bak

L1:

---2

---4

---5

---0

---0.bak

rman_bak.sh脚本主程序

#!/bin/bash

#--------------------------------------------

# Oracle auto backup using rman

## author:songrh

# week:1,3,6 Level 0 backup

# 2,4,5,0 Level 1 backup

# Copyright by ChenLong Tec

#--------------------------------------------

##

export ORACLE_BASE=/u02/oracle

export ORACLE_HOME=/u02/oracle/product/9.2.4

export ORACLE_SID=PROD

export LD_LIBRARY_PATH=$ORACLE_HOME/lib

export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib:$ORACLE_HOME/network/jlib

export NLS_LANG=american_america.ZHS16GBK

export TNS_ADMIN=$ORACLE_HOME/network/admin

export ORA_NL33=$ORACLE_HOME/ocommon/nls/admin/data

export PATH=/bin:/usr/bin:/usr/sbin:$ORACLE_HOME/bin:$PATH

export PATH=$PATH:/opt/local/bin

#SH_PATH=/u02/rmanscripts

ARC_PATH=/u02/arch

RMAN_BAK_PATH=/u02/rmanbak

##FULL_PATH=$RMAN_BAK_PATH/full

L0_PATH=$RMAN_BAK_PATH/L0

L1_PATH=$RMAN_BAK_PATH/L1

##DAY_TAG=`date "%Y-%m-%d"`

LOG_TAG=`date "%Y-%m-%d"`

#FIRST_DAY=`date %e`

WEEK=`date %w`

#WEEK=1

## FTP configure

IP="122.120.150.155"

FTPUSER="ftpbak"

FTPPASS="******"

FTPROOT0="L0"

FTPROOT1="L1"

#DISK_USE=`df -k |sed -n '/u02/'p | awk '{print $5}' |sed 's/%//'`

####check path function

############

if [[ $DISK_USE -ge 90 ]]; then

rm -rf $L0_PATH/*

rm -rf $L1_PATH/*

fi

if [ "$WEEK" = "6" -o "$WEEK" = "3" -o "$WEEK" = "1" ]; then

if [ ! -d $L0_PATH ]; then

mkdir $L0_PATH

fi

if [ "$WEEK" = "1" ]; then

rm -rf $L0_PATH/*

rm -rf $L1_PATH/*

mkdir $L0_PATH/$WEEK

$SH_PATH/db_l0_backup.sh $L0_PATH/$WEEK

cd $L0_PATH/$WEEK

$SH_PATH/ftp_del.sh $IP $FTPUSER $FTPPASS $FTPROOT0 $WEEK $WEEK_$LOG_TAG.log

else

if [ ! -d $L0_PATH/$WEEK ]; then

mkdir $L0_PATH/$WEEK

$SH_PATH/db_l0_backup.sh $L0_PATH/$WEEK

cd $L0_PATH/$WEEK

$SH_PATH/ftp.sh $IP $FTPUSER $FTPPASS $FTPROOT0 $WEEK $WEEK_$LOG_TAG.log

else ;