Shell script to back up MySQL database in Linux

Source: Internet
Author: User

A more practical backup MySQL script is attached below

The code is as follows Copy Code

##===========================================================
# # Db_bak_rman.sh
# # Created by Robinson
# # 2011/11/07
# # Usage:db_bak_rman.sh < $ORACLE _sid> < $BACKUP _level>
# # Backup_level:
# # F:full Backup
# # 0:level 0
# # 1:level 1
##============================================================
#!/bin/bash
# User specific environment and startup programs

If [f ~/.bash_profile];
Then
. ~/.bash_profile
Fi

Oracle_sid=${1}; Export Oracle_sid
RMAN_LEVEL=${2}; Export Rman_level
timestamp= ' Date +%y%m%d%h%m '; Export TIMESTAMP
Date= ' Date +%y%m%d '; Export DATE
Rman_dir=/u02/database/${oracle_sid}/backup/rman; Export Rman_dir
Rman_data=${rman_dir}/${date}; Export Rman_data
#RMAN_LOG = $RMAN _data/log; Export Rman_log
Rman_log=/u02/database/${oracle_sid}/backup/rman/log Export Rman_log

# Check Rman Level
#======================================================================
If ["$RMAN _level" = = "F"];
Then unset INCR_LVL
Backup_type=full
Else
Incr_lvl= "incremental level ${rman_level}"
Backup_type=lev${rman_level}
Fi

Rman_file=${rman_data}/${oracle_sid}_${backup_type}_${timestamp}; Export Rman_file
Ssh_log=${rman_log}/${oracle_sid}_${backup_type}_${timestamp}.log; Export Ssh_log
maxpiecesize=4g; Export Maxpiecesize

#Check RMAN Backup Path
#=========================================================================

if! Test-d ${rman_data}
Then
Mkdir-p ${rman_data}
Fi

echo "---------------------------------" >>${ssh_log}
echo "" >>${ssh_log}
echo "Rman Begin to Working ..." >>${ssh_log}
echo "Begin at:" ' Date '--' date +%y%m%d%h%m ' >>${ssh_log}

#Startup Rman to Backup
#=============================================================================
$ORACLE _home/bin/rman Log=${rman_file}.log <<eof
Connect Target/
Run {
CONFIGURE RETENTION POLICY to RECOVERY windows of 3 days;
CONFIGURE BACKUP optimization on;
CONFIGURE Controlfile autobackup on;
CONFIGURE controlfile autobackup FORMAT for DEVICE TYPE DISK to ' ${rman_file}_%f ';
ALLOCATE CHANNEL ' ch1 ' TYPE DISK maxpiecesize=${maxpiecesize};
ALLOCATE CHANNEL ' CH2 ' TYPE DISK maxpiecesize=${maxpiecesize};
Set limit channel CH1 readrate=10240;
Set limit channel CH1 kbytes=4096000;
Set limit channel CH2 readrate=10240;
Set limit channel CH2 kbytes=4096000;
Crosscheck Archivelog All;
DELETE noprompt expired Archivelog all;
BACKUP
#AS Compressed Backupset
${INCR_LVL}
DATABASE FORMAT ' ${rman_file}_%u ' TAG ' ${oracle_sid}_${backup_type}_${timestamp} ';
SQL ' ALTER SYSTEM ARCHIVE LOG current ';
BACKUP archivelog all FORMAT ' ${rman_file}_arc_%u ' TAG ' ${oracle_sid}_arc_${timestamp} '
DELETE INPUT;
DELETE noprompt OBSOLETE;
Release CHANNEL Ch1;
Release CHANNEL CH2;
}
SQL "ALTER DATABASE backup Controlfile to ' ${rman_data}/cntl_${backup_type}.bak '";
Exit
Eof
Rc=$?

Cat ${rman_file}.log >>${ssh_log}
echo "Rman Stop working @ time:" ' Date ' date +%y%m%d%h%m ' >>${ssh_log}

Echo >>${ssh_log}
echo "------------------------" >>${ssh_log}
echo "------Disk space------" >>${ssh_log}
Df-h >>${ssh_log}

Echo >>${ssh_log}

If [$RC-ne "0"]; Then
echo "------error------" >>${ssh_log}
Else
echo "------No error found during RMAN backup peroid------" >>${ssh_log}
RM-RF ${rman_file}.log
Fi

#Remove old backup than 3 days
#============================================================================
rmdir=${rman_dir}/'/bin/date +%y%m%d-d ' 3 days ago '; Export RMDIR
Echo >>${ssh_log}
Echo-e "------Remove old backup than 3 days------n" >>${ssh_log}

If Test-d ${rmdir}
Then
RM-RF ${rmdir}
Rc=$?
Fi

Echo >>${ssh_log}

If [$RC-ne "0"]; Then
Echo-e "------Remove old Backup exception------n" >>${ssh_log}
Else
ECHO-E "------No error found during remove old backup set peroid------n" >>${ssh_log}
Fi

Exit

oracle@szdb:~/robinson/scripts/dba_scripts/custom/sql>./db_bak_rman.sh GOBO1 0
RMAN> RMAN> 2> 3> 4> 5> 6> 7> 8> 9> 10> 11> 12> 13> 14> 15>
16> 17> 18> 19> 20> 21> 22> 23> 24> rman> rman>
Oracle@szdb:~/robinson/scripts/dba_scripts/custom/sql>

oracle@szdb:/u02/database/gobo1/backup/rman> ls
20120928  log
oracle@szdb:/u02/database/gobo1/ backup/rman/20120928> ls
cntl_lev0.bak                              gobo1_ Lev0_201209281421_arc_4onmb9ro_1_1
gobo1_lev0_201209281421_4knmb9jn_1_1      GOBO1_ lev0_201209281421_c-733951103-20120928-00
gobo1_lev0_201209281421_4lnmb9jn_1_1       GOBO1_LEV0_201209281421_C-733951103-20120928-01
Gobo1_lev0_201209281421_arc_4nnmb9rn_1_1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.