In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to create a MySQL database backup script, the article is very detailed, has a certain reference value, interested friends must read it!
How to implement MySQL database backup? You can use the bash script for MySQL database backups. In this article, we will introduce a simple bash script for backing up the MySQL database, archiving the backup and storing it on the local system. This bash script also removes old backups from disk to free up space. You can also specify the number of days to keep backups on the local disk.
Create a MySQL backup script
Now, copy the following into a script file (such as / backup/mysql backup.sh) and save it on the Linux system. After that, change some configuration values in the "Update below values" section of the script according to your environment
#! / bin/bash # MySQL Database Backup Script # # Written By: Rahul Kumar## URL: https://tecadmin.net/bash-script-mysql-database-backup/## Last Update: Jan 05 20years export PATH=/bin:/usr/bin:/usr/local/binTODAY= `date + "% d%b%Y" `# # # Update below values # DB_BACKUP_PATH='/backup/dbbackup'MYSQL_HOST='localhost'MYSQL_PORT='3306'MYSQL_USER='root'MYSQL_PASSWORD='mysecret'DATABASE_ NAME='mydb'BACKUP_RETAIN_DAYS=30 # # Number of days to keep local backup copy # mkdir-p ${DB_BACKUP_PATH} / ${TODAY} echo "Backup started for database-${DATABASE_NAME}" mysqldump-h $ {MYSQL_HOST}\-P ${MYSQL_PORT}\-u ${MYSQL_USER}\-p$ {MYSQL_PASSWORD}\ ${DATABASE_NAME} | gzip > ${DB_BACKUP_PATH} / ${TODAY} / ${DATABASE_NAME}-${TODAY} .sql.gz if [$?-eq 0] Then echo "Database backup successfully completed" else echo "Error found during backup" fi # Remove backups older than {BACKUP_RETAIN_DAYS} days # DBDELDATE= `date + "% d%b%Y"-date= "${BACKUP_RETAIN_DAYS} days ago" `if [!-z ${DB_BACKUP_PATH}]; then cd ${DB_BACKUP_PATH} if [!-z ${DBDELDATE}] & & [- d ${DBDELDATE}] Then rm-rf ${DBDELDATE} fifi # End of script # #
After you create or download a script, make sure that you set the execution permissions to work properly.
$chmod + x / backup/mysql-backup.sh
Schedule scripts in Crontab
Now, schedule scripts in crontab to run every day and complete backups on a regular basis. Use the crontab-e command to edit crontab on the system. Add the following settings to enable backup at 2 a.m.
Scheduling scripts in crontab
Now schedule the script to run every day in crontab and complete the backup on a regular basis. Use the crontab-e command to edit crontab on the system. Add the following settings to enable backup at 2 a.m.
0 2 * root / backup/mysql-backup.sh
Save your crontab file. When cron is enabled, the script is backed up automatically, but you need to check weekly or monthly to make sure it has been backed up.
The above is all the contents of how to create a MySQL database backup script, thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.