How to implement scheduled backup of CentOS MySQL database

How to implement scheduled backup of CentOS MySQL database

The following script is used for scheduled backup of the entire MySQL database

mysql_dump_script.sh

#!/bin/bash

#Save the number of backup files, retain up to 4 files number=4
#Backup save path backup_dir=/db/backup_mysql
#Date dd=`date +%Y-%m-%d-%H-%M-%S`
#Backup tool tool=mysqldump
#Username username=root
#passwordpassword=yourpassword
#Database to be backed up database_name=mydb

#If the folder does not exist, create it if [ ! -d $backup_dir ];
then
  mkdir -p $backup_dir;
fi

#Simple way to write mysqldump -u root -p123456 users > /root/mysqlbackup/users-$filename.sql
$tool -h127.0.0.1 -u $username -p$password $database_name > $backup_dir/$database_name-$dd.sql

#Write to create backup log echo "create $backup_dir/$database_name-$dd.dupm" >> $backup_dir/log.txt

#Find the backup that needs to be deleted delfile=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | head -1`

#Judge whether the current number of backups is greater than $number
count=`ls -l -crt $backup_dir/*.sql | awk '{print $9 }' | wc -l`

if [ $count -gt $number ]
then
 #Delete the earliest generated backup and keep only number of backups rm $delfile
 #Write the delete file log echo "delete $delfile" >> $backup_dir/log.txt
fi

centos set crontab

yum install crontabs
systemctl enable crond (set to start at boot)
systemctl start crond (start crond service)
systemctl status crond (check status)

vi /etc/crontab

Add a scheduled task

Load the task to make it effective:

crontab /etc/crontab

View tasks:

crontab -l

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Notes on the MySQL database backup process
  • Shell script to backup MySQL database data regularly and retain it for a specified time
  • Mysql database scheduled backup script sharing
  • MySQL database backup and recovery implementation code
  • MySQL database introduction: detailed explanation of database backup operation
  • Analysis of MySQL data backup and recovery implementation methods
  • MySQL scheduled database backup operation example
  • Summary of various implementation methods of mysql database backup
  • Linux implements scheduled backup of MySQL database and deletes backup files older than 30 days
  • Linux regularly backs up the MySQL database and deletes previous backup files (recommended)
  • Mysql backup multiple database code examples
  • Selection and thinking of MySQL data backup method

<<:  Three JavaScript methods to solve the Joseph ring problem

>>:  A colorful cat under Linux

Recommend

Pitfalls based on MySQL default sorting rules

The default varchar type in MySQL is case insensi...

Detailed explanation of the usage and difference between nohup and & in Linux

Example: We use the Python code loop_hello.py as ...

Various types of MySQL indexes

What is an index? An index is a data structure th...

Solution to MySQL restarting automatically

Preface Recently, a problem occurred in the test ...

Detailed explanation of how to use grep to obtain MySQL error log information

To facilitate the maintenance of MySQL, a script ...

Docker Tutorial: Using Containers (Simple Example)

If you’re new to Docker, take a look at some of t...

Two methods to stretch the background image of a web page

There are two solutions: One is CSS, using backgro...

JavaScript data flattening detailed explanation

Table of contents What is Flattening recursion to...

mysql subquery and join table details

Table of contents 1. What is a subquery? 2. Self-...

Steps to deploy Docker project in IDEA

Now most projects have begun to be deployed on Do...

Vue button permission control introduction

Table of contents 1. Steps 1. Define buttom permi...

A friendly alternative to find in Linux (fd command)

The fd command provides a simple and straightforw...

Tutorial on deploying jdk and tomcat on centos7 without interface

1. Install xshell6 2. Create a server connection ...