I have the following batch-file on windows-10 which is very close to working with only one small problem.
The .sql result file is written outside of the desired folder. The batch file properly creates the folder and the file, but fails to change into the desired directory when it creates the .sql result file.
For example, it creates D:\Backups\MySQL\MyDatabase__04-27-2018-14-22, and writes the file MyDatabase__04-27-2018-14-22.sql in the MySQL folder, rather than inside the folder with the exact same name.
I've tried variations of changing the directory with no success.
set dt=%date:~-10,2%-%date:~-7,2%-%date:~-4,4%-%time:~0,2%-%time:~3,2%
set dt=%dt: =0%
if exist "D:\Backups\" (mkdir D:\Backups\MySQL\MyDatabase__%dt%
CD ..
CD ..
CD /D D:\Backups\MySQL\MyDatabase__%dt%
START /WAIT C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="D:\Backups\MySQL\MyDatabase_%dt%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
) else if exist "H:\Backups\" (mkdir H:\Backups\MySQL\MyDatabase__%dt%
CD ..
CD ..
H:
CD H:\Backups\MySQL\MyDatabase__%dt%
START /WAIT C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="H:\Backups\MySQL\MyDatabase_%dt%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
) else if exist "G:\Backups\" (mkdir G:\Backups\MySQL\MyDatabase__%dt%
CD ..
CD ..
G:
CD G:\Backups\MySQL\MyDatabase__%dt%
START /WAIT C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="G:\Backups\MySQL\MyDatabase_%dt%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
) else mkdir C:\Users\Scotty\Desktop\Backups\MySQL\MyDatabase__%dt%
CD ..
CD ..
C:
CD C:\Users\Scotty\Desktop\Backups\MySQL\MyDatabase__%dt%
START /WAIT C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="C:\Backups\MySQL\MyDatabase_%dt%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
CD..
CD..
CD C:\Users\Scotty\Desktop\BackupBatchFiles\
The actual result is that the mysqldump result file is placed outside the directory created with the same name. I am expecting the .sql result file to be written INSIDE the directory that the batch file created with the same name.
Well, all lines with mysqldump.exe specify parameter --result-file with
Backups\MySQL\MyDatabase_%dt%.sql
instead of
Backups\MySQL\MyDatabase_%dt%\MyDatabase_%dt%.sql
I suggest following for the batch file which avoids repetitive code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DateTime=%date:~-10,2%-%date:~-7,2%-%date:~-4,4%-%time:~0,2%-%time:~3,2%"
set "DateTime=%DateTime: =0%"
set "DoLocalBackup="
for %%I in (D H G) do if exist "%%I:\Backups\" set "BackupFolder=%%I:\Backups\MySQL\MyDatabase__%DateTime%" & goto MakeBackup
echo ERROR: Failed to find network backup drive.
:LocalBackup
set "BackupFolder=%UserProfile%\Desktop\Backups\MySQL\MyDatabase__%DateTime%"
set "DoLocalBackup=1"
:MakeBackup
mkdir "%BackupFolder%" 2>nul
if exist "%BackupFolder%\" (
C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="%BackupFolder%\MyDatabase_%DateTime%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
) else (
echo ERROR: Failed to create backup folder: "%BackupFolder%"
)
if not defined DoLocalBackup goto LocalBackup
endlocal
CD /D "%UserProfile%\Desktop\BackupBatchFiles\"
This batch file creates a backup in first found backup folder on a network drive and additionally one more backup in a subdirectory of current user's desktop directory.
Another solution is even easier because it just chooses from one of four possible backup locations as the batch file code in question.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DateTime=%date:~-10,2%-%date:~-7,2%-%date:~-4,4%-%time:~0,2%-%time:~3,2%"
set "DateTime=%DateTime: =0%"
for %%I in (D: H: G: "%UserProfile%\Desktop") do if exist "%%~I\Backups\" set "BackupFolder=%%~I\Backups\MySQL\MyDatabase__%DateTime%" & goto MakeBackup
echo ERROR: Failed to find network backup drive or local backup folder.
goto EndBackup
:MakeBackup
mkdir "%BackupFolder%" 2>nul
if exist "%BackupFolder%\" (
C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="%BackupFolder%\MyDatabase_%DateTime%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
) else (
echo ERROR: Failed to create backup folder: "%BackupFolder%"
)
:EndBackup
endlocal
CD /D "%UserProfile%\Desktop\BackupBatchFiles\"
Here is also a third solution which is similar to second solution, but continues the FOR loop if creation of the backup folder failed on a specified possible backup storage location.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DateTime=%date:~-10,2%-%date:~-7,2%-%date:~-4,4%-%time:~0,2%-%time:~3,2%"
set "DateTime=%DateTime: =0%"
set "BackupFolder=Backups\MySQL\MyDatabase__%DateTime%"
for %%I in (D: H: G: "%UserProfile%\Desktop") do if exist "%%~I\Backups\" (
mkdir "%%~I\%BackupFolder%" 2>nul
if exist "%%~I\%BackupFolder%\" (
C:\xampp\mysql\bin\mysqldump.exe --user=root --password= --host=localhost --port=3306 --result-file="%%~I\%BackupFolder%\MyDatabase_%DateTime%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "MyDatabase"
goto EndBackup
) else echo ERROR: Failed to create backup folder: "%%~I\%BackupFolder%"
)
echo ERROR: Failed to find any specified backup folder or to create the
echo current date backup folder in one of the backup folders.
:EndBackup
endlocal
CD /D "%UserProfile%\Desktop\BackupBatchFiles\"
Further I would suggest to change the third line to:
set "DateTime=%date:~-4,4%-%date:~-7,2%-%date:~-10,2%-%time:~0,2%-%time:~3,2%"
This results in international date format yyyy-MM-dd which is better than dd-MM-yyyy because of directories/files sorted alphabetically are at the same time sorted chronological using international date format.
Note: The date/time format used for date/time strings of referenced environment variables DATE and TIME as used in third line depends on which country/region is configured for the account which is used running this batch file. See for example Why does %date% produce a different result in batch file executed as scheduled task? for a region independent solution to get current date/time in a specified format.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
echo /?
endlocal /?
for /?
goto /?
if /?
mkdir /? or md /?
set /?
setlocal /?
Related
I want to copy a file in C:\file.txt to a netword drive which unc path is \fic-bleu\MPM-DICIR\SERVICE-TUNNELS\
I see in Google i have to use pushd and popd but it only modify the current prompt. And i can't use the mysqldump command from this new prompt.
I tried :
cd C:\xampp\mysql\bin
pushd \\ficbleu\MPM-DICIR\SERVICE-TUNNELS
mysqldump.exe --opt --user=root --host=localhost service_tunnels > service_tunnels.sql
popd \\ficbleu\MPM-DICIR\SERVICE-TUNNELS
And
cd C:\xampp\mysql\bin
mysqldump.exe --opt --user=root --host=localhost service_tunnels > pushd \\ficbleu\MPM-DICIR\SERVICE-TUNNELS\service_tunnels.sql
Maybe i have first to save in my computer the dumpsql file and then copy bu i can't find a way to do that :(
Finally i established this script :
#echo off
cd C:\xampp\mysql\bin
mysqldump.exe --opt --user=root --host=localhost service_tunnels > service_tunnels.sql
C:\Windows\System32\xcopy "C:\xampp\mysql\bin\service_tunnels.sql" "\\ficbleu\MPM-DICIR\SERVICE-TUNNELS\01. MAIN COURANTE - MAINTENANCE\Saves\service_tunnels.sql"
Pause
But they said that there are too many arguments so i found a solution in google that says to add quotes to drive path and now its says that is invalid drive path...
Correct answer, need to map network drive as a letter (Y:\ for me)
#echo off
cd C:\xampp\mysql\bin
mysqldump.exe --opt --user=root --host=localhost service_tunnels > service_tunnels.sql
C:\Windows\System32\robocopy "C:\xampp\mysql\bin" "Y:\01. MAIN COURANTE - MAINTENANCE\Saves" *.sql
Pause
If I run this from a command prompt it works:
C:\Program Files\MariaDB 5.5\bin>MySQL.exe -u root -p --password=xyz --port=3322 --protocol=TCP < "C:\temp folder\CREATE_database.sql"
So I know the .sql script is fine.
But in my NSIS script I see a command prompt window pop up but disappear quickly and the database isn't created:
ExecWait '"C:\Program Files\MariaDB 5.5\bin\mysql.exe" -u root -p --password=root --port=3322 --protocol=TCP < "C:\temp folder\CREATE_database.sql"'
Am I not passing in the parameters to MySQL.exe properly?
ExecWait just creates a simple process, it does not support stdin/stdout redirection.
You can use cmd.exe as a helper:
Section
nsExec::ExectoStack 'cmd.exe /c if 1==1 "c:\path\to\myapp.exe" -param1 -param2 < "stdin file.txt"'
pop $0
pop $1
DetailPrint "Exit code=$0"
DetailPrint "Output=|$1|"
SectionEnd
or you can use the ExecDos plug-in.
I'm trying to set up a simple script to backup a mysql database:
#!/bin/bash
#
# Dumps mydatabase to ~/Developer/mydatabase_dumps/
#
dumpfile="~/Developer/mydatabase_dumps/$(date +"%Y-%m-%d-%H-%M-%S").sql"
echo "Dumping mydatabase to $dumpfile ..."
mysqldump -u root -p mydatabase > "$dumpfile"
echo "Dump completed."
That shouldn't be too complicated, but I always get "No such file or directory":
user$: pwd
/home/myname/Developer/bash_scripts
user$: ls -l
-rwxrw-r-- 1 myname myname 268 Apr 27 15:15 dbdump # name of the script
user$: dbdump # ./dbdump or sh dbdump produce the same result
Dumping mydatabase to ~/Developer/mydatabase_dumps/2017-04-27-15-22-06.sql ...
/home/myname/Developer/bash_scripts/dbdump: line 8: ~/Developer/mydatabase_dumps/2017-04-27-15-22-06.sql: No such file or directory
Dump completed.
The script was written on Ubuntu, so it shouldn't be a problem with Windows line endings. Changing the permissions to 777 didn't work either. I've run a bit out of ideas here, would be glad if someone could point me in the right direction.
I have been pouring through multiple forms and stack overflow questions looking for the correct syntax for creating a batch fill that will generate one .sql dump file for each database.
:: Name of the database user
set dbuser=user
:: Password for the database user
set dbpass=password
:: Loop through the data structure in the data dir to get the database names
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql" -u%dbuser% -p%dbpass% -e "show databases" ^| FOR /D %%F IN (read databases) DO (
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump" --user=%dbuser% --password=%dbpass% --databases %%F > "D:\Backup\%%F_6am_mon.sql"
)
Structure of code mainly came from Jon Lucas at http://kb.hyve.com/wiki/Backup%20All%20MySQL%20Databases%20and%20Output%20To%20Seperate%20Files
I had other code but this one seemed to write to a file.
I've code that works on non-windows, but I need something that will work in windows server 2008 R2 cmd
Can anyone shed some light for me?
ALSO TRIED------------
FOR /F "usebackq delims=" %%F IN ("C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump" -u%user% -p%password% -e "show databases;") DO (
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump" --user=%user% --password=%password% --databases %%F > "D:\Backup\%%F_6am_mon.sql"
)
If you can install Powershell then this script should work;
foreach ($i in Get-ChildItem -Path "c:\Users\jelemans\Documents" -Directory)
{
echo "backing up - $i.name"
$theDate = get-date -format yyyymmdd
$backupName = "backup$theDate"
backup($backupName)
}
Function backup($dbName)
{
$path = "D:backupPath$dbName"
mysqldump -uxxx -pyyy $dbName > $path
}
witht the correct path set for your db (not my test folder).
Try this script for unix;
for var in "$#"
do
echo "Backing up $var"
backupName=backup$var`date +'%Y%m%d'`
mysqldump --user=%user% --password=%password% $var > "\Backup\$backupName"
done
with the edits for your user and password.
Execute this to run it from the data directory (or add cd in the script);
find * -type d -exec testScript {} +
changing the name of "testScript" to match your script name. Note that this script will try to do mysql as well. you need a mod if you want to skip that one.
txt files that i download from a FTP server and fill a table with this files automatically with a .bat file.
I can already download the files and add only one file to the table here is my code so far:
#echo off
color 17
cd C:/Users/Silvia/Documents/Roboto
rename *.txt actual.txt
cd "C:\Users\Silvia\Documents\MySQL Server 5.6\bin"
mysql -h localhost -u root -ppass <C:\Users\Silvia\Documents\Roboto\crear.bat
pause
exit
And here is the crear.bat code:
use Operacion15;
create table db(PATENTE varchar(4), ADUANA varchar(3), PEDIMENTO varchar(7), RFC varchar(13));
LOAD DATA LOCAL INFILE "C:/Users/Silvia/Documents/Roboto/actual.txt"
INTO TABLE db
FIELDS TERMINATED BY '|'
(PATENTE, ADUANA, PEDIMENTO, RFC);
How can i make a cicle to run in all the downloaded files?
ill appreciate your help, thanks
For a first look how the FOR /F command works, start with next command typed in a command line window (you could use Copy and Paste:
for /F %G in ('dir /b "C:/Users/Silvia/Documents/Roboto\*.txt"') DO #echo %~fG
Then you could modify your code as follows:
#echo off
SETLOCAL
color 17
rem download directory
set "_pathDownload=C:\Users\Silvia\Documents\Roboto"
set "_pathToCrear=%_pathDownload%"
rem make sure a "backup_done" folder exists
set "_pathBackup=%_pathDownload%\backup_done"
md "%_pathBackup%\" 2>NUL
rem working directory
set "_pathMySQLbin=C:\Users\Silvia\Documents\MySQL Server 5.6\bin"
pushd "%_pathMySQLbin%"
rem LOCAL INFILE: elicit from crear.bat
for /F "tokens=5*" %%G in (
'type "%_pathToCrear%\crear.bat"^|FINDSTR INFILE'
) do (
set "_fileActual=%%~G"
)
rem main loop
for /F %%G in ('dir /b "%_pathDownload%\*.txt"') DO (
rem remove ECHO from next three ECHO-ed lines as soon as debugged
rem moreover: at same time, remove ^ ahead of < from mysql line
ECHO copy /B /Y "%%~fG" "%_fileActual%"
ECHO mysql -h localhost -u root -ppass ^<"%_pathToCrear%\crear.bat"
rem (optional) backup file treated
ECHO move /Y "%%~fG" "C:\Users\Silvia\Documents\Roboto\backup_done\"
ECHO(
rem previous ECHO( line: for ECHO-ed output only to make it easy to survey
)
pause
exit
Note:
backup_done folder: it's a sample name (as well as the path);
doubled % percentage sign for using in a batch file (%%G instead of %G);
items in a path are delimited with a \ backslash sign;
echo copy …, echo mysql … and echo move …: operational commands are ECHOed for debugging purposes only (to see eventual script behaviour): remove all echo prefix no sooner than code debugged;
there is a difference in path to the actual.txt file: …\Silvia\… in the batch file you have provided vs. …/Garber/… in the crear.bat code
Edit: code improved and commented
If your downloaded files allow to be merged before sending to mysql, then the main loop part in your script could be a bit more simple (with the only mysql call) as follows:
rem
rem include part of previous script here up to 'rem main loop'
rem
rem main loop
rem merge downloaded files
type nul > "%_fileActual%"
for /F %%G in ('dir /b "%_pathDownload%\*.txt"') DO (
rem remove ECHO from next ECHO-ed lines as soon as debugged
rem moreover: at same time, remove all ^ ahead of > from mysql line
if /I "%%~fG"=="%_fileActual%" (
rem no merge
) else (
ECHO type "%%~fG" ^>^> "%_fileActual%"
rem (optional) backup file treated
ECHO move /Y "%%~fG" "C:\Users\Silvia\Documents\Roboto\backup_done\"
ECHO(
)
)
mysql -h localhost -u root -ppass <"%_pathToCrear%\crear.bat"
pause
exit