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
- 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
- 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
- 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
- Grant permission to psopuser ﹣ bak.sh
[root@localhost /]# chmod 777 /home/oraclebak/psopuser_bak.sh
- Change the psopuser? Bak.sh encoding
[root@localhost /]# vi /home/oraclebak/psopuser_bak.sh :set ff=unix #Set encoding :wq #Save exit
- 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
- View tasks
[root@localhost ~]# crontab -l
III. configure unclassified transmission
- 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
- 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
- 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
- Run command on remote server (server B)
[root@localhost ~]# cd /root/.ssh/ [root@localhost .ssh]# cat id_rsa.pub.dbbak >> authorized_keys
- 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
- 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
- Change the psopuser? Remote? Bak.sh encoding
[root@localhost /]# vi /home/oraclebak/psopuser_remote_bak.sh :set ff=unix #Set encoding :wq #Save exit
- 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
- 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