-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmysqldumper.bat
More file actions
174 lines (156 loc) · 5.55 KB
/
mysqldumper.bat
File metadata and controls
174 lines (156 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: The simple and quick MySQL database backup batch script.
:: It dumps all databases into a directory specified. It exports .sql file
:: for each database with the file name format of <db_name>_<YYYYMMDD>.sql by default.
:: When the argument --archive or -a is provided, sql file will be archived
:: into <db_name>_<YYYYMMDD>.zip.
::
:: Licensed under The MIT License
:: For full copyright and license information, please see LICENSE
::
:: @author Sithu <hello@sithukyaw.com>
:: @license http://www.opensource.org/licenses/mit-license.php MIT License
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@ECHO off
SETLOCAL EnableDelayedExpansion
REM Set variables
REM path to your mysql.exe installation path
SET mysqlDir=D:\xampp\mysql\bin
REM Store mysql login information in a file e.g., C:\Users\[username]\.my.cnf with your mysql information
REM
REM [client]
REM host=your_host
REM user=your_username
REM password=your_password
REM
REM To keep the password safe, the file should not be accessible to anyone but yourself.
REM To ensure this, set the file access mode to 400 or 600, e.g., chmod 600 [the-file]
REM @see http://dev.mysql.com/doc/refman/5.5/en/password-security-user.html
REM Set the fully qualified path name to the file here
SET mysqlLogin="%UserProfile%\.my.cnf"
REM The directory where you want to save your sql files
REM It will be created if it does not exist
SET backupDir=%UserProfile%\.mysqlbackup
REM The system databases which don't need to be dumped
REM Please make sure to include the trailing comma if you update this list
SET dbsIgnored=information_schema,cdcol,mysql,performance_schema,phpmyadmin,test,webauth,
REM Temp file
SET tmpFile=%TEMP%\mysqldbs.tmp
REM Argument capturing
SET paramName=
SET dbs=
SET archive=false
:argLoopStart
SET arg=%1
IF -!arg!-==-- GOTO argLoopEnd
REM Check for long parameter names (--argument)
IF "!arg!" == "--archive" (
SET archive=true
SET paramName=
) ELSE IF "!arg!" == "--dbs" (
SET paramName=--dbs
) ELSE IF "!arg:~0,2!" == "--" (
SET paramName=!arg!
REM Check for short parameter names (-a)
) ELSE IF "!arg!" == "-a" (
SET archive=true
SET paramName=
) ELSE IF "!arg!" == "-d" (
SET paramName=-d
) ELSE IF "!arg:~0,1!" == "-" (
SET paramName=!arg!
) ELSE (
REM This is a value for the previous parameter
IF "!paramName!" == "--dbs" ( SET dbs=!dbs! !arg! )
IF "!paramName!" == "-d" ( SET dbs=!dbs! !arg! )
)
SHIFT
GOTO argLoopStart
:argLoopEnd
:errorHandling
IF NOT EXIST %mysqlLogin% ( GOTO errConfig )
IF NOT EXIST %mysqlDir% ( GOTO errDir )
GOTO main
:errConfig
(ECHO [client] & ECHO host=localhost & ECHO user=root & ECHO password=) > %mysqlLogin%
ECHO NOTICE^^! at line 34 in mysqldumper.bat
ECHO Configuration file %mysqlLogin% has been created.
ECHO Update your mysql login information in the file as below
ECHO.
ECHO [client]
ECHO host=your_host
ECHO user=your_username
ECHO password=your_password
ECHO.
ECHO To keep the password safe, the file should not be accessible to anyone but yourself.
ECHO To ensure this, set the file access mode to 400 or 600, e.g., chmod 600 [the-file]
ECHO @see http://dev.mysql.com/doc/refman/5.5/en/password-security-user.html
GOTO end
:errDir
ECHO ERROR^^! at line 22 in mysqldumper.bat
ECHO Set the directory path where your mysql.exe was installed
GOTO end
:dumpDatabase
REM Function to dump a database and optionally archive it
REM Parameters: %1 = database name, %2 = today date, %3 = backup directory
ECHO Dumping %1 ...
mysqldump --defaults-extra-file=!mysqlLogin! --quick --opt --add-drop-database --single-transaction --no-tablespaces --databases %1 > %3\%1_%2.sql
ECHO ... Exported %3\%1_%2.sql
CALL :archiveFile %1 %2 %3
GOTO :EOF
:archiveFile
REM Function to archive a SQL file if archive flag is true
REM Parameters: %1 = database name, %2 = today date, %3 = backup directory
IF "!archive!" == "true" (
powershell -command "Compress-Archive -Path '%3\%1_%2.sql' -DestinationPath '%3\%1_%2.zip' -Force"
IF EXIST %3\%1_%2.zip (
DEL %3\%1_%2.sql
ECHO ... Archived to %3\%1_%2.zip
) ELSE (
ECHO ... Warning: Failed to create archive for %1_%2.sql
)
)
GOTO :EOF
:main
REM Get the current Date and Time in a locale-agnostic way
FOR /f "skip=1" %%x IN ('wmic os get localdatetime') DO (
IF NOT DEFINED timestamp SET timestamp=%%x
)
SET today=%timestamp:~0,4%%timestamp:~4,2%%timestamp:~6,2%
SET time24=%timestamp:~8,2%%timestamp:~10,2%%timestamp:~12,2%
SET backupDir=%backupDir%\%today%
REM check the backup directory
IF NOT EXIST %backupDir% (
MD %backupDir%
ECHO %backupDir% is created.
)
REM Change to mysql dir
CD !mysqlDir!
IF "!dbs!"=="" (
REM Get all databases name into a temp file
REM If the server was started with the --skip-show-database option,
REM you cannot use this statement at all unless you have the SHOW DATABASES privilege.
mysql --defaults-extra-file=!mysqlLogin! -e "SHOW DATABASES" > "!tmpFile!"
REM Process all database names in the file
FOR /F "delims=\ tokens=* skip=1" %%L IN (!tmpFile!) DO (
REM Skip if the database is in the ignored list
IF "!dbsIgnored:%%L,=!" EQU "!dbsIgnored!" (
CALL :dumpDatabase %%L !today! !backupDir!
)
)
) ELSE (
REM Dumping the given databases only
FOR %%L IN (!dbs!) DO (
CALL :dumpDatabase %%L !today! !backupDir!
)
)
:done
ECHO Done^^!
:end
REM Delete the temporary file
IF EXIST !tmpFile! ( DEL !tmpFile! )
REM Go back to the original directory to the script
CD %~dp0
ENDLOCAL