mysqldump
bitsCN.com 今天有个需求要在 Windows 下实现数据库自动备份,拼凑了一下解决办法。实现的特性
可指定多个数据库
按照 年/月/日 的方式组织备份
可选的使用 WinRAR 压缩备份
使用计划任务实现定时备份
具体代码
备份
@echo off & setlocal ENABLEEXTENSIONS
:: ---------- 配置项 ----------
:: 备份放置的路径,加 /
set BACKUP_PATH=D:/Backup/
:: 要备份的数据库名称,多个用空格分隔
set DATABASES=database1 database2 database3
:: MySQL 用户名
set USERNAME=root
:: MySQL 密码
set PASSWORD=123456
:: MySQL Bin 目录,加 /
:: 如果可以直接使用 mysqldump(安装时添加 MySQL Bin 目录到了环境变量),此处留空即可
set MYSQL=D:/SERVER/MySQL/bin/
:: WinRAR 自带命令行工具的可执行文件路径,长文件名注意用 Dos 长文件名书写方式
set WINRAR=C:/Progra~1/WinRAR/Rar.exe
:: ---------- 以下请勿修改 ----------
set YEAR=%date:~0,4%
set MONTH=%date:~5,2%
set DAY=%date:~8,2%
:: 如果在 dos 下输入 time 返回的不是 24 小时制(没有 0 填充),请自行修改此处
set HOUR=%time:~0,2%
set MINUTE=%time:~3,2%
set SECOND=%time:~6,2%
set DIR=%BACKUP_PATH%%YEAR%/%MONTH%/%DAY%/
set ADDON=%YEAR%%MONTH%%DAY%%HOUR%%MINUTE%%SECOND%
:: create dir
if not exist %DIR% (
mkdir %DIR% 2>nul
)
if not exist %DIR% (
echo Backup path: %DIR% not exists, create dir failed.
goto exit
)
cd /d %DIR%
:: backup
echo Start dump databases...
for %%D in (%DATABASES%) do (
echo Dumping database %%D ...
%MYSQL%mysqldump -u%USERNAME% -p%PASSWORD% %%D > %%D.%ADDON%.sql 2>nul
:: winrar
if exist %WINRAR% (
%WINRAR% a -k -r -s -m1 -ep1 %%D.%ADDON%.rar %%D.%ADDON%.sql 2>nul
del /F /S /Q %%D.%ADDON%.sql 2>nul
)
)
echo Done
:exit
增加计划任务
@echo off
:: ------- 配置项 -------
:: 要执行的文件名
set FILE=D:/cron/cron_backup.bat
:: 计划频率类型
set FREQUENCY=DAILY
:: 频率,与上面的计划频率类型对应
set MODIFIER=1
:: 该计划执行的时间(24 小时制)
set DATETIME=00:30:00
:: 计划的名字
set NAME="Backup Cron Job"
:: 计划执行用户,不建议修改
set USER="System"
:: ------- 以下请勿修改 -------
schtasks /Create /RU %USER% /SC %FREQUENCY% /MO %MODIFIER% /ST %DATETIME% /TN %NAME% /TR %FILE%
pause
其中的
:: 计划频率类型,可选项:
:: MINUTE 分钟
:: DAILY 天
:: HOURLY 小时
:: WEEKLY 周
:: MONTHLY 月
:: 频率,与上面的计划频率类型对应
:: MINUTE: 1 到 1439 分钟
:: HOURLY: 1 - 23 小时
:: DAILY: 1 到 365 天
:: WEEKLY: 1 到 52 周
:: MONTHLY: 1 到 12,或 FIRST, SECOND, THIRD, FOURTH, LAST, LASTDAY
使用方法
将上面的两段代码分别保存为 cron_backup.bat 和 cron_backup_add.bat,并根据注释修改相关参数
确定好目录之后,运行 cron_backup_add.bat 将计划任务添加到系统中。
Windows Server 2003 / XP 可以通过 开始,控制面板,计划任务进入计划任务管理界面;
Windows Vista / 7 可以通过 计算机,右键,管理,系统工具,任务计划程序,任务计划程序库,进入计划任务管理界面。
手动运行该任务,查看备份目录来确认下备份是否成功。
待改进
增加删除多少天之前备份的功能bitsCN.com

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)