当前位置:网站首页>Windows and Linux regularly backup MySQL database
Windows and Linux regularly backup MySQL database
2022-06-26 10:44:00 【MervynLammm】
List of articles
Backup scripts
I wrote one before Mysql Backup and restore commands , Today's backup script is mentioned in this article mysqldump.
mysqldump yes mysql The built-in client-side tool to dump the database for backup . It is often used to back up databases
# mysqldump -h host -P port -u user -p password [ Options ] > Dump target file
mysqldump -h127.0.0.1 -P3306 -uroot -p --all-databases > E:\all.sql
# among --all-databases To dump all databases
# There are often --databases, Specify one or more databases
# --ignore-table, Exclude a table
# Other options search and learn by yourself
Timing task
Linux
Linux Use crontab Implement automatic script execution .
- establish
backup.shfile , Writing backup statements , Grant execution permissionchmod a+x backup.sh - Set up
crontabTiming task- Open profile
crontab -e - Join a scheduled task
0 0 * * * /root/backup.sh, For example, every day 0 When the script is executed - preservation
- Open profile
Windows
- establish
backup.batfile , Writing backup statements . - Search task scheduler and open .
- Create basic tasks
- Fill in the name
- Triggers are filled in according to their own needs
- Select backup script file
- preservation
Delete obsolete backup files
Backing up the database every day will eventually lead to too many backup files , It is useless to back up files 、 Outdated issues . You can add delete file statements to a scheduled task script . Implementation keeps only files after the specified date .
Linux
find /root/backup -mtime +7 -name "all*.sql" -exec rm -Rf {
} \;
# lookup /root/backup Under the folder ,7 Days ago , The name is "all*.sql" The file of , Delete
# mtime Is the time condition for file filtering ,+n by n Days ago, ,-n by n Days.
Combined with backup statements
find /root/backup -mtime +7 -name "all*.sql" -exec rm -Rf {
} \;
mysqldump -h127.0.0.1 -P3306 -uroot -p123456 --all-databases > /root/backup/all_$(date "+%Y%m%d%H%M%S").sql
# Backup the database to the specified directory /root/backup in , The file is named all_ Date and time at the time of backup .sql
# Delete 7 Specified file days ago
# It can be realized by cooperating with scheduled tasks , Automatically back up the database every day , And only the most recent... Is kept in the backup folder 7 Days of backup data .
Windows
forfiles /p "E:\backup" /m "all*.sql" /d -7 /c "cmd /c del /f /q /a @path"
# /p specify the path to a file
# /m Find the filename
# /d Specify Date ,-7 by 7 Days ago,
# /c Run command line
# @path Return the full path of the file
# Delete the specified directory 、 name , Seven days ago
Combined with backup statements
mysqldump -h127.0.0.1 -P3306 -uroot -p123456 --all-databases > /root/backup/all_$(date "+%Y%m%d%H%M%S")[email protected] off
:: demonstration : Delete the specified days before the specified path ( The last modification date of the document shall prevail ) The file of .
:: If the demonstration results are correct , hold del Ahead echo Get rid of , You can really delete .
:: This example requires Win7/Win2008/Win2012/Win2016 Self contained forfiles Command support
rem Specify the storage path of the file to be deleted
set SrcDir=E:\backup\
rem Specify the number of days
set DaysAgo=7
forfiles /p %SrcDir% /m "all*.sql" /d -%DaysAgo% /c "cmd /c echo del /f /q /a @path"
mysqldump -h127.0.0.1 -P3306 -uroot -p123456 --all-databases > E:\backup\all_%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%.sql
# Backup the database to the specified directory E:\backup\ in , The file is named all_ Date and time at the time of backup .sql
# Delete 7 Specified file days ago
# It can be realized by cooperating with scheduled tasks , Automatically back up the database every day , And only the most recent... Is kept in the backup folder 7 Days of backup data .
Reference material
The batch bat Script deletes files before the specified number of days
Linux Batch delete files by time ( Delete N It's a document )
边栏推荐
- 【LeetCode】59. Spiral matrix II
- Hcia-dhcp experiment
- VS或Qt编译链接过程中出现“无法解析的外部符号”的原因:
- Oracle11g 启动数据库时报错 ORA-27154: post/wait create failed
- 【Leetcode】76. Minimum covering substring
- Solution to the problem of compilation error due to repeated third-party package names
- Servlet learning notes II
- 开发者,微服务架构到底是什么?
- Tape library simple record 1
- Leetcode intermediate node of linked list
猜你喜欢

Express (I) - easy to get started

118. Yanghui triangle

How to change the QR code material color of wechat applet

【软件项目管理】期末复习知识点整理

See how I store integer data in the map < string, string > set

Swiftui development experience: data layer of application design for offline priority

量化投资学习——经典书籍介绍

MySQL 10th job - View

MySQL 9th job - connection Query & sub query

Basic MySQL
随机推荐
[online simulation] Arduino uno PWM controls the speed of DC motor
2. merge two ordered arrays
Pytest configuration file
SwiftUI 开发经验之为离线优先的应用程序设计数据层
How QT uses quazip to compress and decompress files
Swiftui development experience: data layer of application design for offline priority
AIX basic operation record
六月集训(第26天) —— 并查集
echo $?
MySQL第十三次作业-事务管理
MySQL Chapter 6 Summary
SQL Server foundation introduction collation
用同花顺手机炒股是安全的吗?如何用同花顺炒股
AdaptiveAvgPool2D 不支持 onnx 导出,自定义一个类代替 AdaptiveAvgPool2D
Omni channel, multi scenario and cross platform, how does app analyze channel traffic with data
36 qtextedit control input multiline text
Record the handling of oom problems caused by too many threads at one time
Blog article index summary -- Software Engineering
8-图文打造LeeCode算法宝典-最小栈与LRU缓存机制算法题解
Global and Chinese market for baked potato chips 2022-2028: Research Report on technology, participants, trends, market size and share