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

Comparison of the advantages of vue3 and vue2

Table of contents Advantage 1: Optimization of di...

Solution to the garbled code problem in MySQL 5.x

MySQL is a commonly used open source database sof...

Getting Started: A brief introduction to HTML's basic tags and attributes

HTML is made up of tags and attributes, which are...

Docker View the Mount Directory Operation of the Container

Only display Docker container mount directory inf...

Detailed analysis of the principles and usage of MySQL views

Preface: In MySQL, views are probably one of the ...

Detailed tutorial on installing centos8 on VMware

CentOS official website address https://www.cento...

Implementation of textarea adaptive height solution in Vue

Table of contents Hidden Problems Solution to ada...

Solve the conflict between docker and vmware

1. Docker startup problem: Problem Solved: You ne...

A general method for implementing infinite text carousel with native CSS

Text carousels are very common in our daily life....

Detailed explanation of angular parent-child component communication

Table of contents APIs used Simple Example person...

A practical record of encountering XSS attack in a VUE project

Table of contents Preface Discover the cause Cust...

JavaScript to implement the aircraft war game

This article shares with you how to use canvas an...

MySQL learning notes help document

View system help help contents mysql> help con...