Scheduled Oracle database backup to remote server

Keywords: ssh Database Oracle encoding

I. demand

Regularly generate dmp from A database in oracle database of server A and back it up to server B (save for 10 days)

II. Configure scheduled database backup

  1. Create A backup directory on server A and give permissions
[root@localhost /]# mkdir -p /home/oraclebak  
#Create a new oracle backup directory
[root@localhost /]# chown -R oracle:oinstall /home/oraclebak -R   
#oracle users with directory permission of oinstall user group
  1. Create a backup directory on server B and grant permissions
[root@localhost /]# mkdir -p /home/oraclebak  
#Create a new oracle backup directory
[root@localhost /]# chmod 777 /home/oraclebak
  1. Check whether the IP, port, SID, etc. of the database server in psopuser · bak.sh (see the end of the article) are correct. Put the file psopuser · bak.sh in the directory / home/oraclebak of server A
  2. Grant permission to psopuser ﹣ bak.sh
[root@localhost /]# chmod 777 /home/oraclebak/psopuser_bak.sh
  1. Change the psopuser? Bak.sh encoding
[root@localhost /]# vi /home/oraclebak/psopuser_bak.sh
:set ff=unix #Set encoding
:wq #Save exit
  1. Server A adds scheduled tasks
[root@localhost /]# crontab -e
#Insert the following statement (back up at 1:00 a.m. every day)
00 1 * * * /home/oraclebak/psopuser_bak.sh
  1. View tasks
[root@localhost ~]# crontab -l

III. configure unclassified transmission

  1. Use the xSell or shh connection tool to connect to the database server (server A), run SSH keygen-t RSA, and enter / root/.ssh. You can see the id_rsa and id_rsa_pub files
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #Keep going back here
[root@localhost .ssh]# cd /root/.ssh
[root@localhost .ssh]# ll
total 8
-rw------- 1 root root 1675 Mar 29 16:31 id_rsa
-rw-r--r-- 1 root root  408 Mar 29 16:31 id_rsa.pub
[root@localhost .ssh]# cp id_rsa.pub id_rsa.pub.dbbak
  1. Run command on remote server (server B)
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #Keep going back here
  1. Run command on server A
[root@localhost .ssh]# SCP. / id_rsa. Pub. Dbbak server B_IP:/root/.ssh
The authenticity of host '10.3.100.16 (10.3.100.16)' can't be established.
RSA key fingerprint is 17:a3:ae:64:08:b0:42:fd:35:2d:e0:99:a5:8a:59:5b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.3.100.16' (RSA) to the list of known hosts.
root@10.3.100.16's password: 
id_rsa.pub.dbbak                              100%  408     0.4KB/s   00:00 
  1. Run command on remote server (server B)
[root@localhost ~]# cd /root/.ssh/
[root@localhost .ssh]# cat id_rsa.pub.dbbak    >>  authorized_keys
  1. Run the script on server A to test whether the execution is successful without entering A password
[root@localhost .ssh]# SCP. / id_rsa. Pub. Dbbak server B_IP:/root/.ssh
id_rsa.pub.dbbak                              100%  408     0.4KB/s   00:00 

IV. configure remote server (server B) to delete dmp N days ago

  1. Connect to the server B, put the script psopuser? Remote? Bak.sh (see the end of the article) in the directory / home/oraclebak, and grant permissions
[root@localhost ~]# chmod 777 /home/oraclebak/psopuser_remote_bak.sh
  1. Change the psopuser? Remote? Bak.sh encoding
[root@localhost /]# vi /home/oraclebak/psopuser_remote_bak.sh
:set ff=unix #Set encoding
:wq  #Save exit
  1. Add scheduled task
[root@localhost /]# crontab -e
#Insert the following statement (delete at 1:00 a.m. every day)
00 1 * * * /home/oraclebak/psopuser_remote_bak.sh
  1. View tasks
[root@localhost ~]# crontab -l

Attached script file:

psopuser_bak.sh

#Here, the database name ﹐ bak.sh is used to name, in order to distinguish the specific database backed up
if [ -f ~/.bash_profile ];
then
  . ~/.bash_profile
fi
echo "begin export psopuser dmp file..."
#Setting environment variables
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/db_1
export ORACLE_SID=MEDB #SID
export PATH=$ORACLE_HOME/bin:/usr/sbin:$PATH
export LANG=en_US.UTF-8
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
date=$(date +%Y%m%d)  #current time
days=10  #Set to delete data 10 days ago
orsid='10.3.100.15:1521/MEDB' #Oracle database IP, port, SID to be backed up
orowner=PSOPUSER  #User name of the database to back up
bakuser=PSOPUSER  #Use this user to export dmp
bakpass=123456789    #User password
bakdir=/home/oraclebak #Backup file path
bakdata=$orowner"_"$date.dmp  #dmp name of the backup
baklog=$orowner"_"$date.log   #log name generated during backup
ordatabak=$orowner"_"$date.tar.gz #Compressed file names of dmp and log to be backed up
cd $bakdir
mkdir -p $orowner
cd $orowner
#Exporting dmp and log files
exp $bakuser/$bakpass@$orsid grants=y owner=$orowner file=$bakdir/$orowner/$bakdata log=$bakdir/$orowner/$baklog 
tar -zcvf $ordatabak $bakdata $baklog  #Compress dmp and log files
find $bakdir/$orowner -type f -name "*.log" -exec rm {} \;  #Delete log file
find $bakdir/$orowner -type f -name "*.dmp" -exec rm {} \;  #Delete dmp file
find $bakdir/$orowner -type f -name "*.tar.gz" -mtime +$days -exec rm -rf {} \; #Delete backup files 10 days ago

psopuser_remote_bak.sh

bakdir=/home/oraclebak #Backup file path
days=10  #Set to delete data 10 days ago
cd $bakdir
find $bakdir -type f -name "*.tar.gz" -mtime +$days -exec rm -rf {} \; #Delete backup files 10 days ago

Posted by ldomingues on Mon, 02 Dec 2019 22:44:58 -0800