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

Beginners learn some HTML tags (2)

Related article: Beginners learn some HTML tags (1...

Tomcat class loader implementation method and example code

Tomcat defines multiple ClassLoaders internally s...

Linux CentOS MySQL database installation and configuration tutorial

Notes on installing MySQL database, share with ev...

Use elasticsearch to delete index data regularly

1. Sometimes we use ES Due to limited resources o...

How to configure Bash environment variables in Linux

Shell is a program written in C language, which i...

Solution to the error when installing Docker on CentOS version

1. Version Information # cat /etc/system-release ...

Detailed example of sorting function field() in MySQL

Preface In our daily development process, sorting...

MySQL common backup commands and shell backup scripts sharing

To back up multiple databases, you can use the fo...

JavaScript Array Methods - Systematic Summary and Detailed Explanation

Table of contents Common array methods Adding and...

Detailed steps for deploying Microsoft Sql Server with Docker

Table of contents 1 Background 2 Create a contain...

Detailed tutorial on building nextcloud private cloud storage network disk

Nextcloud is an open source and free private clou...

Special commands in MySql database query

First: Installation of MySQL Download the MySQL s...

Implementing a simple student information management system based on VUE

Table of contents 1. Main functions 2. Implementa...