A simple method to implement scheduled backup of MySQL database in Linux

A simple method to implement scheduled backup of MySQL database in Linux

Here are the detailed steps:

1. Check the disk space:

[root@localhost backup]# df -h
File system capacity used available used% Mount point /dev/mapper/centos-root 17G 2.7G 15G 16% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 130M 885M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
[root@localhost backup]#

Select a suitable disk to store the backup files

2. Create a backup directory:

cd /home
mkdir backup
cd backup

3. Create a backup shell script:

Create a backup script in the created directory (vi bkDatabaseName.sh)

#!/bin/bash
mysqldump -uroot -proot rtak > /data/backup/rtak_$(date +%Y%m%d_%H%M%S).sql
mysqldump -uroot -proot rtak | gzip > /data/backup/rtak_$(date +%Y%m%d_%H%M%S).sql.gz

Note:

bkDatabaseName.sh Replace with an interesting name

You can choose between sql backup and gz backup, or full backup

Username and password need to be replaced

4. Add executable permissions:

chmod u+x bkDatabaseName.sh

Test whether the file can be executed normally (./bkDatabaseName.sh)

Note: (1) If the error mysqldump: command not found is displayed, execute

ln -fs /usr/local/mysql/bin/mysqldump /usr/bin (/usr/local/mysql is the path where mysql is installed)

(2) If there is a warning (Warning: Using a password on the command line interface can be insecure.), you can ignore it.

(3) Check whether the backup SQL file is normal and whether it can be imported into the database normally

5. Add scheduled tasks

Confirm whether crontab is installed:

If the crontab command reports command not found, it means that it is not installed.

Execute the command:

crontab -e

Enter the following and save:

*/* * 1 * * /data/backup/bkDatabaseName.sh

/* * 1 * * / Several * represent minute, hour, date, month, and day of the week when the backup operation is performed.

For example: perform backup every minute /1 * * * * / (tested)

Perform backup every day at 3:00 AM /00 3 * * * / (not tested)

6. Stop the backup operation

When scheduled backup is not required, perform this operation and the normal process will be completed at step 5.

crontab -r

Note: Clean up expired SQL backups in time to prevent the disk from filling up

You may also be interested in:
  • The best way to automatically backup the mysql database (windows server)
  • MySQL scheduled backup using crontab scheduled backup example under Linux
  • Linux regularly backs up the MySQL database and deletes previous backup files (recommended)
  • How to back up mysql regularly and cut nginx access log regularly
  • MySQL scheduled database backup operation example
  • Implementation of MySQL scheduled database backup (full database backup)

<<:  Summary of 11 common mistakes made by MySQL call novices

>>:  The JS hasOwnProperty() method detects whether a property is an object's own property.

Recommend

MySQL password modification example detailed explanation

MySQL password modification example detailed expl...

Method of implementing recursive components based on Vue technology

describe This article introduces a method to impl...

Docker Data Storage Volumes Detailed Explanation

By default, the reading and writing of container ...

Problems installing TensorRT in docker container

Uninstall the installed version on Ubuntu: sudo a...

How to remount the data disk after initializing the system disk in Linux

Remount the data disk after initializing the syst...

JavaScript array deduplication solution

Table of contents Method 1: set: It is not a data...

Comparison of various ways to measure the performance of JavaScript functions

Table of contents Overview Performance.now Consol...

JavaScript to achieve slow motion animation effect

This article shares the specific code for JavaScr...

Detailed explanation of the order of JS object traversal

Some of you may have heard that the order of trav...

Web Design Principles of Hyperlinks

<br />Related articles: 9 practical tips for...

Using NTP for Time Synchronization in Ubuntu

NTP is a TCP/IP protocol for synchronizing time o...

Introduction to new ECMAscript object features

Table of contents 1. Object properties 1.1 Attrib...

Web page creation basic declaration document type description (DTD

Using CSS layout to create web pages that comply w...

MySQL query example explanation through instantiated object parameters

This article will introduce how to query data in ...