Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mysqldump backup specifies the mysql database script

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

A few days ago, a friend asked to help write a mysql data backup script, so there is the following script to back up the database through the mysqldump command, posted to communicate with you, if you have any questions, please correct, thank you.

Implement the function:

1 back up the specified database

2 delete backup files before the specified number of days. The default setting is 7 days.

#! / bin/bash## file name: directory of mysql_bak.sh## database backup files If not, please create a directory or specify a backup directory BACKUP_DIR= "/ bak/mysqlbak" # specify the host name of the host where the mysql is located, specify the mysql login user name, DB_USERNAME='backupuser'#, specify the mysql login password, specify the database name of the backup, DB_NAME= "dbname" # define the current date as the variable CURRENT_DATE=$ (date + "% Y%m%d$H") # define N days before deletion The file variable DEL_DAYS_BEFORE_FILES=7# specifies the directory where mysqldump is located, MYSQLDUMP_DIR= "/ usr/bin" # backup specified database if $($MYSQLDUMP_DIR/mysqldump-h ${DB_HOSTNAME}-u$ {DB_USERNAME}-p$ {DB_PASSWORD} ${DB_NAME} > "${BACKUP_DIR} / ${DB_NAME} _ ${CURRENT_DATE} .sql") Thencd ${BACKUP_DIR} gzip ${DB_NAME} _ ${CURRENT_DATE} .sqlecho "${CURRENT_DATE}-- Backup database ${DB_NAME} successfully!" elseecho "${CURRENT_DATE}-- Backup database ${DB_NAME} unsuccessfully" fi# deletes the backup file find ${BACKUP_DIR}-name "${DB_NAME} _ * .sql.gz"-type f-mtime + ${DEL_DAYS_BEFORE_FILES}-exec rm {}\ > / dev/null 2 > & 1

Instructions for using mysql backup scripts:

1 the backup operation is completed using the mysqldump command, and the default setting is to keep the backup files for nearly 7 days.

2 it is recommended to create the user backupuser for backup (the host field suggests that you specify IP)

> CREATE USER 'backupuser'@'%' IDENTIFIED BY' password'

> GRANT SELECT,LOCK TABLES,FILE,RELOAD ON *. * TO 'backupuser'@'%'

(personally, I think these permissions are sufficient, if not enough to add them on their own.)

3 Note that you need to specify the database backup directory in the script

4 give the script execution permission

$chmod + x mysql_bak.sh

5 create scheduled tasks, such as backup operations at 2: 00 a. M. every day

Operation under Redhat

$crontab-e

# $PWD refers to the path where the backup script is located

# $BACKUP_DIR refers to the database backup directory

* 2 * $PWD/mysql_bak.sh & > $BACKUP_DIR/mysql_bak.log

Note: the permission settings of the user who runs the script on the directory being operated on.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report