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 index cardinality concept and usage examples

This article uses examples to explain the concept...

MySQL master-slave synchronization principle and application

Table of contents 1. Master-slave synchronization...

Detailed tutorial on running Tomcat in debug mode in IDEA Maven project

1. Add the following dependencies in pom.xml <...

Teach you how to build Tencent Cloud Server (graphic tutorial)

This article was originally written by blogger We...

Detailed explanation of Django+Vue+Docker to build an interface testing platform

1. Two words at the beginning Hello everyone, my ...

Simple web page code used in NetEase blog

How to use the code in NetEase Blog: First log in...

Javascript basics about built-in objects

Table of contents 1. Introduction to built-in obj...

React.js framework Redux basic case detailed explanation

react.js framework Redux https://github.com/react...

MySql8 WITH RECURSIVE recursive query parent-child collection method

background When developing a feature similar to c...

Summary of Binlog usage of MySQL database (must read)

I won't go into details about how important b...

HTML basic summary recommendation (text format)

HTML text formatting tags 標簽 描述 <b> 定義粗體文本 ...

Brief introduction and usage of Table and div

Web front end 1 Student ID Name gender age 01 Zha...

How to write memory-efficient applications with Node.js

Table of contents Preface Problem: Large file cop...