Home  >  Article  >  System Tutorial  >  Teach you how to set up automatic daily backup of Oracle database under Linux

Teach you how to set up automatic daily backup of Oracle database under Linux

WBOY
WBOYforward
2024-02-12 14:18:091445browse

Oracle database is the most commonly used relational database management system in enterprise-level applications. Since data is critical to a business, it is very important to back up your Oracle database regularly. On Linux systems, you can use a variety of backup tools to back up and restore Oracle databases. This article will introduce you to the method of backing up Oracle database on Linux system, allowing you to easily protect the security of enterprise data.

This article takes the CentOS 7.6 system and Oracle 11g as examples to teach you how to set up automatic daily backup of the Oracle database under Linux.

1. First find the environment variables of the database

If you are under the root account, you must first log in to the account where the database is located

su oracle
cat ~/.bash_profile
Teach you how to set up automatic daily backup of Oracle database under Linux
export PATH
export ORACLE_BASE=/home/nnc_db/app
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
export PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_SID=orcl

Environment variables have been found!

2. Edit the backup script file

vi bak.sh

First copy and paste the environment variables into the bak.sh file,

Then define a variable date (its role is to name the files backed up every day for easy identification), and use the expdp command to export the database. The code is as follows:

date=$(date +%Y%m%d)
expdp 数据库账号/数据库密码@数据库实例名 dumpfile=${date}_db_auto_backup.dmp schemas=数据库账号名 compression=all

Exit and save.

3. Use the Linux crontab command to set up scheduled tasks

crontab -e

The function of crontab -e is to use a text editor to set up scheduled tasks (the specific usage of crontab will not be described here);

Enter in the text editor after entering

0 0 * * * /home/nnc_db/bak.sh

The above code means to execute the bak.sh file under /home/nnc_db at 0:00 every day, which is the file created in the second step. It needs to be adjusted according to its own backup time and file directory;

Then restart the crond service

service crond restart

4. Test whether the task runs successfully

According to the information I collected online, some friends said that crontab -e will not take effect three minutes after the setting is successful! That is to say, if you set a task at 10:00 am and want to test whether it can succeed, and set the next scheduled task to 10:02, problems may occur! But here I use

* * * * * /home/nnc_db/bak.sh

Set to run every minute and found no problem. Another big pitfall for newbies is that if you directly run the script file, the detailed process will be output on the command line, but if you set up a scheduled task to run, there will be no response! So if you want to see if your script runs automatically, you need to go to /var/spool/mail/ and look at the log file, as shown in Figure

Teach you how to set up automatic daily backup of Oracle database under Linux

In this article, we introduce the methods of backing up and restoring Oracle databases using RMAN, expdp, and impdp tools, and elaborate on the advantages and disadvantages of each method. Whether you're a beginner or an experienced database administrator, these tips will help you keep your business data secure. If you encounter any problems or have any questions during the backup process, please feel free to leave a message in the comment area and we will be happy to answer your questions.

The above is the detailed content of Teach you how to set up automatic daily backup of Oracle database under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete