mysql database backup regularly

The content of the shell script is as follows:

Automatically and regularly back up the database through shellll scripts:

#!/bin/bash

# Backup hs_king database

# Set MySQL connection information

MYSQL_USER="root"

MYSQL_PASSWORD="123456"

MYSQL_DATABASE="hs_king"

# Set the name and path of the backup file

BACKUP_PATH="/root/SQL/mysql-file/"

BACKUP_FILENAME="$(date +%Y.%m.%d.%H.%M)-${MYSQL_DATABASE}.sql" BACKUP_FILE="$BACKUP_PATH$BACKUP_FILENAME"

# Export the MySQL database

mysqldump -R -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE > $BACKUP_FILE # Delete expired backup files: last 15 days

find $BACKUP_PATH -type f -name "*-${MYSQL_DATABASE}.sql" -mtime +15 -exec rm {} \;

# Delete based on the creation time of the file 

script authorization

chmod +x backup_mysql.sh

crontab add timing tasks

crontab -e

*/10 * * * * /root/SQL/backup_mysql.sh

Verify backup database

explain

This shell script is used to back up the MySQL database. Here is a detailed description of the script:

  1. Lines 2 to 6: set the information for connecting to MySQL. You can modify the value of the variable according to the actual situation.

    • MYSQL_USER: MySQL user name, set to "root" here.
    • MYSQL_PASSWORD: MySQL password, here is set to "123456", please modify it according to the actual situation.
    • MYSQL_DATABASE: The name of the database to be backed up, set to "hs_king" here.
  2. Lines 9 and 10: Set the name and path of the backup file.

    • BACKUP_PATH: The storage path of the backup file, here is set to "/root/SQL/mysql-file/", please ensure that the directory exists and has write permission.
    • BACKUP_FILENAME: The name of the backup file, in the form of the current date (year, month, day, hour and minute) plus the database name.
  3. Line 13: Use mysqldumpthe command to export the MySQL database.

    • -RParameters are used to export stored procedures and triggers.
    • -u$MYSQL_USER -p$MYSQL_PASSWORDSpecifies the username and password to connect to MySQL.
    • $MYSQL_DATABASESpecifies the database to back up.
    • > $BACKUP_FILEOutput the exported data to a backup file.
  4. Line 16: Delete expired backup files.

    • find $BACKUP_PATHSpecifies the path to look for.
    • -type fIndicates to only find files.
    • -name "*-${MYSQL_DATABASE}.sql"Specifies the backup filename pattern to match, where "*" represents any character.
    • -mtime +15Indicates that only files that have been modified for more than 15 days are selected.
    • -exec rm {} \;Delete the matched files.

The function of this script is to back up the specified MySQL database and keep the backup files of the past 15 days, and the backup files older than 15 days will be automatically deleted. Please ensure that the directory where the backup files are stored has been created and has appropriate write permissions before running this script. In addition, please modify the MySQL connection information and backup file path in the script according to the actual situation to suit your environment.

Guess you like

Origin blog.csdn.net/qq_52497256/article/details/131538986