Development

MYSQL backup and compress with a batch script

Here is how to do a MYSQL backup and compress using 7zip though a batch file or .bat file this makes the process easy to run with windows scheduler to ensure your database is always backed up.

Edit the following with your MYSQL database and connection info. Also make sure that you have the 7za.exe where it is defined. You can also change the backups save directory.

@echo off
color 0E
title MySQL backup.

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" & set "MS=%dt:~15,3%"

set "dirname=%DD%_%MM%_%YY%_%HH%%Min%"
 
set basedir=C:
set workdir=c:\Users\Administrator\Documents\
set zip ="C:\7za\7za.exe"
set mysqldir=c:\xampp\mysql\bin
set mysqlpassword=PASSWORDHERE
set mysqluser=USERHERE
set mysqldb=DATABASEHERE
 
cd %mysqldir%
 
mysqldump -u %mysqluser% -p%mysqlpassword% -p%mysqldb% >%workdir%\backup.sql

7za a -t7z "%dirname%.7z" %workdir%\backup.sql

MOVE %dirname%.7z %workdir%

 

Share
Tags: DevMYSQL

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago