MySQL自动备份和手工恢复
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 以下是windows下的批处理: 1set hour_str=%time:~0,2% 2if "%hour_str:~0,1%"==" " set "hour_str=0%hour_str:~1%" 3set fileName=%date:~0,4%-%date:~5,2%-%date:~8,2%_%hour_str%%time:~3,2%%time
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入
以下是windows下的批处理:
1set hour_str=%time:~0,2%
2if "%hour_str:~0,1%"==" " set "hour_str=0%hour_str:~1%"
3set fileName=%date:~0,4%-%date:~5,2%-%date:~8,2%_%hour_str%%time:~3,2%%time:~6,2%
4D:
5cd D:\autobak\
6mysqldump 数据库名(改我) -u root -p密码(改我) >%fileName%.sql
7jar cf %fileName%.zip %fileName%.sql
8del %fileName%.sql
使用前,需要保证环境变量的path中加入jar和mysqldump等路径,批处理解释如下:
1、前三句只是为了生成文件名,这里第2行:if "%hour_str:~0,1%"==" " set "hour_str=0%hour_str:~1%"是为了解决上午时,小时变成一位,导致文件名中包含一个空格,从而创建文件失败的问题,即假设9点的时候,把“空格9”变成“09”。
2、第4、5行是为了切换到保存的目录,也可以在第6、7行写死,当然,如果想自动获取路径也可以,用:
%~d0
cd %~dp0
替换4、5行,可以自动放到当前bat批处理所在盘的根目录,适合懒人:)
3、第6行是导出数据库为sql脚本,但这里不见得全库导出,也可以只导某个表,具体命令就不说了。
4、第7行是用jar压缩一下,由于导出的是文本,压缩率是很高的,这样很节省空间。(当然也可以用zip、rar等,只是我搞java开发,用习惯了jar。)
5、第8行是删除未压缩的文件,只保留压缩后的zip包。
以上批处理创建后,在定时任务中添加一个任务,定时执行就可以了,我一般给一些小系统都设置从早上8点开始,每隔4个小时备份一次,然后执行到晚上8点。另外,由于文件名都是基于时间的,那么很容易就可以实现保留最近7天、以及同时上传到异地ftp服务器的功能,这些大家可以自由发挥。
linux、mac下应该也都类似,大家自己写个sh脚本就行了,至于mysql的恢复,更加简单,把sql解压出来后,执行:
mysql -u root -p密码 数据库名 这样就可以自动还原回来,如果数据库已经存在,会自动覆盖(也可能是增量,看导出的sql了)。 其实以上脚本其实也适合其它数据库,只是sqlserver、db2等都有自己的自动备份机制,倒不是必须用这个。

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

Ouyi, also known as OKX, is a world-leading cryptocurrency trading platform. The article provides a download portal for Ouyi's official installation package, which facilitates users to install Ouyi client on different devices. This installation package supports Windows, Mac, Android and iOS systems. Users can choose the corresponding version to download according to their device type. After the installation is completed, users can register or log in to the Ouyi account, start trading cryptocurrencies and enjoy other services provided by the platform.

Gate.io is a highly acclaimed cryptocurrency trading platform known for its extensive token selection, low transaction fees and a user-friendly interface. With its advanced security features and excellent customer service, Gate.io provides traders with a reliable and convenient cryptocurrency trading environment. If you want to join Gate.io, please click the link provided to download the official registration installation package to start your cryptocurrency trading journey.

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...
