How to set up automatic daily database backup in Linux

How to set up automatic daily database backup in Linux

This article takes Centos7.6 system and Oracle11g as an example:

1. Find the database environment variables first

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

su oracle
cat ~/.bash_profile 

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

The environment variable has 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 function is to name the daily backup file for easy identification), and use the expdp command to export the database. The code is as follows:

date=$(date +%Y%m%d)
expdp database account/database password@database instance name dumpfile=${date}_db_auto_backup.dmp schemas=database account name compression=all

Exit save.

3. Use Linux crontab command to set up scheduled tasks

crontab -e

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

Enter in the text editor

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

The above code means that the bak.sh file in /home/nnc_db is executed at 0:00 every day, which is the file created in the second step. It needs to be adjusted according to your 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 being set successfully! That is to say, if you set a task at 10:00 am and want to test whether it is successful, you may set the next scheduled task to 10:02, there may be problems! But here I use

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

Set it to run once every minute and found no problems.

Another big pitfall for novices is that if you run the script file directly, the detailed process will be output on the command line, but there will be no response when you set a scheduled task to run! So if you want to see whether your script is running automatically, you need to look at the log file in /var/spool/mail/ , as shown in the figure

Summarize

The above is the method that I introduced to you to set up automatic daily database backup under Linux. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Linux implements automatic and scheduled backup of MySQL database every day
  • How to automatically back up the MySQL database in Linux every day
  • A simple method to implement scheduled backup of MySQL database in Linux
  • Linux regularly backs up the MySQL database and deletes previous backup files (recommended)
  • Implementation script for scheduled database backup in Linux

<<:  Implementation of element multiple form validation

>>:  How to set mysql5.7 encoding set to utf8mb4

Recommend

Basic application methods of javascript embedded and external links

Table of contents Basic application of javascript...

Descending Index in MySQL 8.0

Preface I believe everyone knows that indexes are...

Four solutions for using setTimeout in JS for loop

Table of contents Overview Solution 1: Closures S...

Realizing the effect of carousel based on jQuery

This article shares the specific code of jQuery t...

Comprehensive understanding of HTML Form elements

As shown below: XML/HTML CodeCopy content to clip...

Solution to the welcome to emergency mode message when booting CentOS7.4

Today I used a virtual machine to do an experimen...

Nginx operation and maintenance domain name verification method example

When configuring the interface domain name, each ...

JavaScript to implement the web version of the snake game

This article shares the specific code for JavaScr...

How to manually encapsulate paging components in Vue3.0

This article shares the specific code of the vue3...

Use of Linux read command

1. Command Introduction The read command is a bui...

Solution to MySQL replication failure caused by disk fullness

Table of contents Case scenario Solving the probl...

Cross-domain issues in front-end and back-end separation of Vue+SpringBoot

In the front-end and back-end separation developm...