Home Database Mysql Tutorial Detailed explanation of how mysql backup script

Detailed explanation of how mysql backup script

May 03, 2017 pm 01:56 PM
mysql

Script description

Back up all data every 7 days, and back up binlog every day, which is an incremental backup.

(If there is little data, just back up the complete data once a day, which may not be possible. Incremental backup is necessary)

The author is not very familiar with shell scripts, so many places are written very stupidly:)

Open bin log

In mysql 4.1 version, By default, there are only error logs and no other logs. You can open the bin log by modifying the configuration. There are many methods, one of which is to add the following to the mysqld section of /etc/my.cnf:

[mysqld]

log-bin

The main function of this log is incremental backup or replication (there may be other uses).

If you want incremental backup, you must open this log.

For mysql with frequent database operations, this log will become very large, and there may be multiple.

Flush-logs in the database, or use mysqladmin, mysqldump to call flush-logs and use parameters delete-master-logs, these log files will disappear and new log files will be generated (empty at first).

So if you never back up, opening the log may not be necessary.

Complete You can call flush-logs at the same time as the backup, and flush-logs before the incremental backup to back up the latest data.

Full backup script

If there is a lot of database data, we usually take a few days or Back up data once a week to avoid affecting application operation. If the amount of data is relatively small, then it doesn’t matter if you back up once a day.

Download Assuming that our data amount is relatively large, the backup script is as follows: (Refer to a mysql on the Internet Backup script, thanks:))

#!/bin/sh
# mysql data backup script
# by scud http://www.jscud.com
# 2005-10-30
#
# use mysqldump --help,get more detail.
#
BakDir=/backup/mysql
LogFile=/backup/mysql/mysqlbak.log
DATE=`date +%Y%m%d`
echo " " >> $LogFile
echo " " >> $LogFile
echo "-------------------------------------------" >> $LogFile
echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile
echo "--------------------------" >> $LogFile
cd $BakDir
DumpFile=$DATE.sql
GZDumpFile=$DATE.sql.tgz
mysqldump --quick --all-databases --flush-logs
--delete-master-logs --lock-all-tables > $DumpFile
echo "Dump Done" >> $LogFile
tar czvf $GZDumpFile $DumpFile >> $LogFile 2>&1
echo "[$GZDumpFile]Backup Success!" >> $LogFile
rm -f $DumpFile
#delete previous daily backup files:采用增量备份的文件,如果完整备份后,则删除增量备份的文件.
cd $BakDir/daily
rm -f *
cd $BakDir
echo "Backup Done!"
echo "please Check $BakDir Directory!"
echo "copy it to your local disk or ftp to somewhere !!!"
ls -al $BakDir
Copy after login

The above script backs up mysql to the local /backup/mysql directory, and the incremental backup files are placed in the /backup/mysql/daily directory.

Note: The above script does not transfer the backed up files to other remote computers, nor does it delete the backup files from a few days ago: the user needs to add relevant scripts or operate manually.

Incremental Backup

The amount of data in incremental backup is relatively small, but it must be operated on the basis of full backup. Users can weigh time and cost and choose the method that is most beneficial to them.

Usage of incremental backup bin log, the script is as follows:

#!/bin/sh
#
# mysql binlog backup script
#
/usr/bin/mysqladmin flush-logs
DATADIR=/var/lib/mysql
BAKDIR=/backup/mysql/daily
###如果你做了特殊设置,请修改此处或者修改应用此变量的行:缺省取机器名,mysql缺省也是取机器名
HOSTNAME=`uname -n`
cd $DATADIR
FILELIST=`cat $HOSTNAME-bin.index`
##计算行数,也就是文件数
COUNTER=0
for file in $FILELIST
do
COUNTER=`expr $COUNTER + 1 `
done
NextNum=0
for file in $FILELIST
do
base=`basename $file`
NextNum=`expr $NextNum + 1`
if [ $NextNum -eq $COUNTER ]
then
echo "skip lastest"
else
dest=$BAKDIR/$base
if(test -e $dest)
then
echo "skip exist $base"
else
echo "copying $base"
cp $base $BAKDIR
fi
fi
done
echo "backup mysql binlog ok"
Copy after login

The incremental backup script is flush-logs before backup. Mysql will automatically put the logs in the memory into the file, and then generate a new log file, so we only need to backup The first few are enough, that is, the last one is not backed up.

Because there may be multiple log files generated from the last backup to this backup, it is necessary to detect the files. If it has been backed up, there is no need to back it up.

Note: Similarly, users also need to transmit remotely by themselves, but there is no need to delete it. The program will automatically generate it after a complete backup.

Access Settings

The script is finished. In order to make the script run, the corresponding user name and password need to be set. Both mysqladmin and mysqldump require user names and passwords. Of course, they can be written in the script, but it is not convenient to modify. Assume We use the root user of the system to run this script, then we need to create a .my.cnf file in /root (that is, the home directory of the root user) with the following content

[mysqladmin]
password =password
user= root
[mysqldump]
user=root
password=password
Copy after login

Note: Set this file only for root Readable. (chmod 600 .my.cnf )

This file describes that the program uses the root user of mysql to back up data, and the password is the corresponding setting. This way there is no need to write the user name and password in the script.

Automatically run

In order to make the backup program run automatically, we need to add it to crontab.

There are two methods, one is to put the script into / according to your choice etc/cron.daily,/etc/cron.weekly such directories.

One is to use crontab -e to put it into the root user's scheduled tasks, for example, a full backup is run every Sunday at 3 am. Daily backup runs every Monday to Saturday at 3am.

The above is the detailed content of Detailed explanation of how mysql backup script. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles