Get the App
SLTechnology News&Howtos  ›  Database  › 

How to back up mysql database regularly

Shulou Source: shulou.com Published: 2022-05-31 18:30:12 09月22日 Update

This article mainly explains "how to back up mysql database regularly". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to back up mysql database regularly".

Procedure:

1. Put the script in any location, which is not easy to be deleted by mistake.

2. Scheduled tasks. The script is executed once a week as follows:

#! bin/bash

Backuppath=/data/mysql/backupdate=$ (date +% Y%m%d%H%M) SQLuser=rootSQLpwd=passwordmysqldump=/usr/local/mysql/bin/mysqldumpmysql=/usr/local/mysql/bin/mysqlHIS_Date= `date-d $(date-d "- 30 day" +% Y%m%d) +% s` # take the timestamp Sql_Date= `date-d $(date-d "- 7 day" +% Y%m%d) +% s` # from 30 days ago

# enter the mysql database and clear the zabbix historical data

$mysql-u$SQLuser-p$SQLpwd-e "use zabbix; delete from history WHERE 'clock'

< $Sql_Date; delete from history_uint WHERE 'clock' < $Sql_Date; delete from history_str WHERE 'clock' < $Sql_Date; delete from history_text WHERE 'clock' < $Sql_Date; delete from history_log WHERE 'clock' < $Sql_Date; exit " #建立备份目录 if [ ! -e $backuppath ];then mkdir -p $backuppathfi #开始备份zabbix库 $mysqldump -u$SQLuser -p$SQLpwd zabbix >

$backuppath/zabbix$date.sql

# Delete backup data from 1 month ago

Find / data/mysql/backup-mtime + 30-type f-name\ zabbix*.sql-exec rm-f {}\

If the historical data in the database is too large, you can consider losing the historical data by truncate.

$mysql-u$SQLuser-p$SQLpwd-e "use zabbix; truncate table history; truncate table history_log; truncate table history_uint; truncate table history_str; truncate table history_text Exit "Thank you for reading, the above is the content of" how to back up the mysql database regularly ". After the study of this article, I believe you have a deeper understanding of the problem of how to back up the mysql database regularly, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Data backup database history historical data scripts learning content time tasks locations that is ideas situations articles more steps directories knowledge knowledge points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MariaDB NVidia OPPO Reno Linux