- 论坛徽章:
- 0
|
[编写]自制的一个自动备份数据文件脚本
版本更新:增加了如果不是archivelog模式自动退出,命令使用格式提示,错误命令格式自动退出,以及错误提示等方面,只需将这个脚本放到一个文件例如backup中,并chmod 755 backup,然后用./backup <dir>;就可以了(dir为你要备份到的目录)。
#!/bin/sh
ORACLE_SID=dzzd; export ORACLE_SID
ORAENV_ASK=NO; export ORAENV_ASK
rm -Rf arch.txt
sqlplus -s "/ as sysdba" <<ARCH
set head off
set feedback off
spool arch.txt
select log_mode from v\$database;
spool off
EXIT
ARCH
for archstat in `cat arch.txt`
do
echo $archstat |grep "^ARCHIVELOG"
if [ "$?" -eq "0" ] ; then
mypath=$1
if [ -z $mypath ] ; then
echo 'ERROR: Please Use This Command Like ./backup /u01/backup'
else
rm -Rf direc.txt
sqlplus -s "/ as sysdba" <<dire
set head off
set feedback off
spool direc.txt
select '$mypath' from dual where trim('$mypath') like '%/';
spool off
exit
dire
if [ -s direc.txt ] ; then
echo 'ERRORlease Do Not Add / At The End Of The Direcotry!'
exit
fi
if [ -d $mypath ] ; then
rm -Rf aa.txt
sqlplus -s "/ as sysdba"<<EOF
set head off
set feedback off
spool aa.txt
select tablespace_name from dba_tablespaces;
spool off
EXIT
EOF
for spacename in `cat aa.txt`
do
echo $spacename |grep "^TEMP"
if [ "$?" -eq "0" ]
then
echo TEMP no need backup else
else
echo "---------------------------------------------------------------------------------------------"
echo Begin Backup $spacename
sqlplus -s "/ as sysdba"<<SETSTA
alter tablespace $spacename begin backup;
EXIT
SETSTA
rm -Rf bb.txt
sqlplus -s "/ as sysdba"<<DATAFILE1
set head off
spool bb.txt
SELECT 'cp '||file_name||' $mypath'||SUBSTR(FILE_NAME,INSTR(FILE_NAME,'/',-1,1))
FROM dba_data_files where tablespace_name=trim('$spacename');
spool off
EXIT
DATAFILE1
sh bb.txt
sqlplus -s "/ as sysdba"<<SETSTA
alter tablespace $spacename end backup;
EXIT
SETSTA
echo NOTES: End Backup $spacename
fi
done
echo "---------------------------------------------------------------------------------------------"
echo "NOTES: Begin Backup ControlFile!"
rm -Rf $mypath/control.dbf
sqlplus -s "/ as sysdba"<<BACKCON
set head off
set feedback off
alter database backup controlfile to '$mypath/control.dbf';
EXIT
BACKCON
echo "NOTES: End Backup ControlFile!"
echo "---------------------------------------------------------------------------------------------"
echo "NOTES: Begin Backup ArchiveLogFile!"
rm -Rf $mypath/arc
mkdir $mypath/arc
rm -Rf arc1.txt
sqlplus -s "/ as sysdba"<<ARC
set head off
set feedback off
spool arc1.txt
alter system switch logfile;
SELECT 'cp '||name||' $mypath/arc'||SUBSTR(NAME,INSTR(NAME,'/',-1,1))
FROM v\$archived_log where status!='D' and status!='X';
spool off
EXIT
ARC
sh arc1.txt
echo "NOTES: End Backup ArchiveLogFiel!"
else
echo "ERROR: Please Input a Right Direction!"
fi
fi
else
echo "ERROR: Your Database Is Not Run At Archive Mod!"
fi
done |
|