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

React+axios implements github search user function (sample code)

load Request Success Request failed Click cmd and...

Solve the mysql user deletion bug

When the author was using MySQL to add a user, he...

How to reset the root password of Mysql in Windows if you forget it

My machine environment: Windows 2008 R2 MySQL 5.6...

Mysql uses stored procedures to quickly add millions of data sample code

Preface In order to reflect the difference betwee...

In-depth exploration of whether Mysql fuzzy query is case-sensitive

Preface Recently, I have been busy writing a smal...

Vite introduces the implementation of virtual files

Table of contents background Importing virtual fi...

Play and save WeChat public account recording files (convert amr files to mp3)

Table of contents Audio transcoding tools princip...

How to solve the problem of character set when logging in to Linux

Character set error always exists locale: Cannot ...

Implementation of CSS sticky footer classic layout

What is a sticky footer layout? Our common web pa...

MySQL installation and configuration method graphic tutorial (CentOS7)

1. System environment [root@localhost home]# cat ...

How to install redis5.0.3 in docker

1. Pull the official 5.0.3 image [root@localhost ...

Nginx solves cross-domain issues and embeds third-party pages

Table of contents Preface difficulty Cross-domain...