I am attempting to have a batch file run every night at midnight (via task scheduler) so that a backup of my mysql db is generated (using mysqldump), encrypted (using 7-zip), and moved to the appropriate folder. I modified a version of the code found at: https://sqlbackupandftp.com/blog/how-to-automate-mysql-database-backups-in-windows. The following is my modified version:
rem credentials to connect to mysql server
set mysql_user=***********
set mysql_password=***********
rem archive password
set encryption_password=***********
rem backup storage period (in days)
set max_keep_days=7
rem path to backup compression utility
set seven_zip_path=C:\Program Files\7-Zip\
rem backup file name generation
set backup_name=C:\Users\**********\Documents\Temp\all-databases-%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%-%time::=.%
rem backup creation
mysqldump --user %mysql_user% --password=%mysql_password% --all-databases >> %backup_name%.sql
if %ERRORLEVEL% neq 0 eventcreate /ID 1 /L APPLICATION /T ERROR /SO mysql-backup-script /D "Backup failed: error during dump creation" && exit
rem backup compression
"%seven_zip_path%7z" a -p%encryption_password% %backup_name%.zip %backup_name%.sql
if %ERRORLEVEL% neq 0 eventcreate /ID 1 /L APPLICATION /T ERROR /SO mysql-backup-script /D " Backup failed: error during archive creation" && exit
rem delete temporary .sql file
del %backup_name%.sql
rem copy backup to storage
robocopy C:\Users\**********\Documents\Temp\ C:\Users\**********\Desktop\Backups\ /e
rem local backup copy
del %backup_name%.zip
eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO mysql-backup-script /D "Backup successful"
(I removed the network path sections, as I am first trying to focus on getting this to run locally before re-incorporating those sections. Also, the backup_name variable didn't work until I used a full path)
This code does properly run when I run this myself, while logged in, etc. However, if I lock the pc, as I did last night, the file ran but failed once it reached the mysqldump line.
The inclusion of %TIME% variable worked when the time was after 12pm, but after midnight and in the morning there suddenly was an extra space in the file name which caused an error.
(to those who are having similar issues, please see Mofi's replies as they will seriously help you out)
Related
I am new to MySQL so please have some patience...
I have been tasked with refreshing a particular UAT Schema, however when I've restored the Schema not all of the tables are present.
Details are as follows; -
The size of the schema is 75Gb
The six largest tables contain 2.3Gb, 3.3Gb, 4.8Gb, 13.6Gb, 15.2Gb and 33.8Gb of data.
The method for the backup is a Windows Server scheduled task running a .bat file, the command for the backup is:
REM BH 26/05/19
#echo on
net use z: /delete /y
Net use z: "\\THE NETWORK SHARE NAME"
set mydate=%date:/=%
set mytime=%time::=%
set mytimestamp=%mydate: =_%_%mytime:.=_%
set dbname=schemaname
set filename="E:\Backup\%dbname%-%mytimestamp%.sql"
set filename2="E:\Backup\%dbname%-%mytimestamp%.tz"
echo %filename%
cd "E:\Backup"
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqldump.exe" %dbname% --user=??? --password=??? --host=localhost --port=3306 --quick --lock-tables=false --result-file=%filename% --default-character-set=utf8 --single-transaction=TRUE
I am using MySQL Workbench to perform the restore, the restore completes but there are over a 150 tables missing.
Note the following: -
Account running the backups has access to all areas of MySQL
New Dump files are created every three hours
I have been using the latest dump files
Many Thanks
I am tring to use this to install mysql from the command line
cls
echo off
SET ProgFiles86Root="%ProgramFiles(x86)%"
IF NOT %ProgFiles86Root%=="" ( SET ProgFiles86Root=%ProgramFiles% )
echo Starting MySQL install
msiexec /i "mysql-5.5.11-win32.msi" /qn
echo MySQL installed successfully
echo Configurating MySQL Server...
"%ProgFiles86Root%\MySQL\MySQL Server 5.5\bin\mysqlinstanceconfig.exe" -i -q ServiceName=MySQL RootPassword=mysql ServerType=DEVELOPER
DatabaseType=MIXED Port=3306 Charset=utf8
echo MySQL has been installed successfully
setx PATH "%%ProgFiles86Root%\MySQL\MySQL Server 5.5\bin%;"
cd /
c:
mysql --user=root --password=mysql -e "CREATE USER 'myuser'#'localhost' IDENTIFIED BY '123456';"
mysql --user=root --password=mysql -e "GRANT ALL ON mydatabase.* TO 'myuser'#'192.168.0.%' IDENTIFIED BY '123abc' WITH GRANT OPTION; FLUSH
PRIVILEGES;
But i get the error
Starting MySQL install
MySQL installed successfully
Configurating MySQL Server...
The system cannot find the path specified.
MySQL has been installed successfully
SUCCESS: Specified value was saved.
'mysql' is not recognized as an internal or external command,
operable program or batch file.
'mysql' is not recognized as an internal or external command,
operable program or batch file.
The command line says Mysql was installed, but it wasnt, as i cannot fined it in the uninstall section in control panel
Mistake 1: Assigning program files folder path with and without double quotes to ProgFiles86Root
This line
SET ProgFiles86Root="%ProgramFiles(x86)%"
assigns the string "%ProgramFiles(x86)%" with the double quotes and all possibly existing trailing spaces and tabs to environment variable ProgFiles86Root.
The IF condition in line
IF NOT %ProgFiles86Root%=="" ( SET ProgFiles86Root=%ProgramFiles% )
works because the value of the environment variable ProgFiles86Root is with double quotes. The double quotes are also compared by command IF, not just the strings enclosed by the double quotes.
But if this IF condition is true because of batch file is running on a 32-bit Windows, the value of environment variable ProgramFiles is assigned to the environment variable ProgFiles86Root without double quotes.
For more details on how to assign a string correct to an environment variable see the answer on How to set environment variables with spaces? and also the answers linked there.
Let us next look on:
"%ProgFiles86Root%\MySQL\MySQL Server 5.5\bin\mysqlinstanceconfig.exe"
This is either expanded for example on 64-bit Windows to
""C:\Program Files (x86)"\MySQL\MySQL Server 5.5\bin\mysqlinstanceconfig.exe"
with double quotes inside a double quoted file name with path which is of course not good, or for example on 32-bit Windows to
"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqlinstanceconfig.exe"
which would be a correct specification of the executable.
Mistake 2: Invalid command line in batch file
The next mistake is on line:
DatabaseType=MIXED Port=3306 Charset=utf8
This line does not contain a command to execute for Windows command interpreter and therefore must result in an error message.
I'm quite sure that this line contains options for the mysqlinstanceconfig.exe command line. The same mistake is made on last line of batch code in question with the string PRIVILEGES; which should be at end of the command line above.
Mistake 3: Attempt to REPLACE user PATH with a custom folder path
The command setx is for REPLACING user (as done here) or system PATH (only with option /m which of course requires administrator privileges) and does not have any effect on local PATH of currently running command process interpreting the commands in the batch file.
It is of course a very bad idea to REPLACE the user PATH by the string you specified on the setx command line although there is by default no user PATH defined. This computer could start not working anymore as expected if this attempt to replace the user path would be successful.
It looks like you want to just append the path to directory bin of MySQL Server 5.5 to local PATH to run the executable mysql without path. But why not running mysql.exe with full path?
Mistake 4: Setting a different current directory is done wrong
cd /
c:
The first line does nothing. To set current directory to root of current drive it would be necessary to run cd \ as the backslash character is the directory separator on Windows and the forward slash is for options. The command CD interprets the slash as beginning of an option and therefore ignores it as there is no option specified after the forward slash. Run in a command prompt window cd /? for help on this command.
The second line makes the current directory on drive C: the current directory for the currently running command process. Which directory is the current directory on drive C: is not defined by this code.
Mistake 5: Attempt to run mysql.exe without path and file extension
The command setx hopefully for all users failed although this is very unlikely. But even if setx is successful, the current command process interpreting the batch file has already its local copy of all environment variables from parent process created on starting the batch file. Therefore running mysql without file extension and path must fail, except MySQL was already installed before and its directory bin was already added to system or user PATH before the batch file processing started.
Solution:
Use this batch code:
#echo off
cls
if "%ProgramFiles(x86)%" == "" (
set "MySQLServerPath=%ProgramFiles%\MySQL\MySQL Server 5.5\bin"
) else (
set "MySQLServerPath=%ProgramFiles(x86)%\MySQL\MySQL Server 5.5\bin"
)
echo Starting MySQL install ...
%SystemRoot%\System32\msiexec.exe /i "mysql-5.5.11-win32.msi" /qn
echo MySQL installed successfully.
echo Configurating MySQL Server ...
"%MySQLServerPath%\mysqlinstanceconfig.exe" -i -q ServiceName=MySQL RootPassword=mysql ServerType=DEVELOPER DatabaseType=MIXED Port=3306 Charset=utf8
echo MySQL has been configured successfully.
cd /D C:\
rem if not "%PATH:~-1%" == ";" set "PATH=%PATH%;"
rem set "PATH=%PATH%%MySQLServerPath%"
"MySQLServerPath\mysql.exe" --user=root --password=mysql -e "CREATE USER 'myuser'#'localhost' IDENTIFIED BY '123456';"
"MySQLServerPath\mysql.exe" --user=root --password=mysql -e "GRANT ALL ON mydatabase.* TO 'myuser'#'192.168.0.%' IDENTIFIED BY '123abc' WITH GRANT OPTION; FLUSH PRIVILEGES;"
The path to directory for MySQL binaries is not added to system PATH by this batch code if not done on installation or configuration of MySQL. If for some unknown reason the local PATH must be extended with path to MySQL binaries directory, uncomment the two lines near end of batch file above by removing the command REM from both lines.
But if you really want to add MySQL binaries directory to user or system PATH which I really don't recommend then read very carefully the comments and answers on
Why are other folder paths also added to system PATH with SetX and not only the specified folder path?
How can I use a .bat file to remove specific tokens from the PATH environment variable?
How to check if directory exists in %PATH%?
So what I have is about 80-90 MySQL DBs to restore and using < gives the 'saved for future use' error when used directly through PowerShell. The next step I tried was using just straight & cmd /c which also prompted the same error.
I tried the code below, but I get either Access Denied or 'system can't find the file specified' and I'm trying to figure out where it's messing up. Any help is definitely appreciated.
PowerShell
$list= gci "C:\Scripts\Migration\mysql\" -name
$mysqlbinpath="C:\Program Files\MySQL\MySQL Server 5.6\bin\mysql"
$mysqluser="admin"
$mysqlpass=getpass-mysql
pushd "C:\Program Files\MySQL\MySQL Server 5.6\bin"
foreach($file in $list){
$dbname=$file.Substring(0, $file.LastIndexOf('.'))
$location='C:\Scripts\Migration\mysql\'+$file
& $bt $mysqluser, $mysqlpass, $dbname, $location
write-host "$file restored"
}
The batch file called by $bt
Batch
"cmd /c "mysql" -u %1 -p%2 --binary-mode=1 %3 < %4"
getpass-mysql is a function I have that pulls the MySQL root pass (our root user is 'admin') as a string.
I hope this isn't a duplicate as I checked other posts first and tried those solutions (perhaps incorrectly applied) and they didn't seem to help.
I have updated the code block with the changes I have made. I am currently receiving the error about using the right syntax near ' ■-'.
I'm running MySQL queries from within Windows command prompt by utilizing a batch file.
The file contains:
#echo off
cd "c:\Program Files\MySQL\MySQL Server 5.6\bin"
echo Please type the root password
set /p password=
mysql -uroot -p%password% < C:\Temp\GenerateDB.sql
echo Please wait while script is executing and when finished successfully
Pause
mysql -uroot -p%password% < C:\Temp\UpgradeSchema.sql
echo Please wait while script is executing and when finished successfully
Pause
Exit
My only problem is that I cannot know if the first query (GenerateDB.sql) is complete or not in order to proceed to the next one(UpgradeSchema.sql).
Is there a way to get a prompt when it is complete or even better track real time progress as when you run the script from MySQL Command Client?
Many thanks in advance
I initially labeled this something different but then figured out my problem, but a different problem came up so now I'm editing this some.
First off let me start. I'm not a "coder" and everything I'm doing is all from research. What I'm trying to accomplish is running a batch file on a loop. It connects to MySQL on my VPS just fine. It will then save the return of the MySQL command to text file when information is there. The problem is when there isn't new information in the database, it just saves it as a blank text file. I would rather have it not save to text at all if there's no information on the DB. This is what I have so far. I'm also running this on Windows Server 2008.
#echo off
:loop
timeout /T 5
mysql --host=111.11.111.11 --port=3306 --user=user_name --password=password --database=db_name --execute="SELECT * FROM tablename LIMIT 1" > results.txt
if exist c:\users\administrator\desktop\results.txt goto end
goto loop
:END
start program.exe
exit
So what I need is something that saves to text if the MySQL command returns information, but if the MySQL command returns empty it continually does the loop until something is there.
Have you tried to save the result to a variable and do a check?
#echo off
:loop
timeout /T 5
Myans = mysql --host=111.11.111.11 --port=3306 --user=user_name --password=password --database=db_name -- execute="SELECT * FROM tablename LIMIT 1"
if not %Myans%=="" ( Myans >> results.txt )
if exist c:\users\administrator\desktop\results.txt goto end
goto loop
:END
start program.exe
exit