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
Related
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 /?
In MySQL,
I want to script a process that automatically grabs mysql binlogs (could be any number of logs) and applies them to the mysql server.
`>dir C:\binlogs
file1
file2
file3
file4
`
` mysqlbinlog utility command is `
`> mysqlbinlog.exe file1 file2 file3 file4 | mysql -uroot `
How do I script this process in using a batch file
An alternative could be:
> mysqlbinlog.exe file1 > file.sql
> mysqlbinlog.exe file2 >> file.sql
> mysqlbinlog.exe file3 >> file.sql
> mysqlbinlog.exe file4 >> file.sql
`> mysql.exe -uroot < file.sql`
Just don't know how to script this via batch
#echo off
setlocal enabledelayedexpansion
set filelist=
for %%# in (*) do set filelist=!filelist!"%%#"
echo %filelist%
This script prints all files in the current directory in one line using the following format: "file1" "file2" "file3". There is one trailing space, which shouldn't matter however since the purpose is to use the list as parameters. In case it does, cut off the last character:
echo %filelist:~,-1%
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.
I am using adityasatrio's batch file to backup local MySQL dbs and like to be able to only keep the 30 latest backup files. Using root:root for this example.
#echo off
set dbUser=root
set dbPassword=root
set backupDir="D:\MySQLDumps\dbs\"
set mysqldump="C:\wamp\bin\mysql\mysql5.6.17\bin\mysqldump.exe"
set mysqlDataDir="C:\wamp\bin\mysql\mysql5.6.17\data"
set zip="C:\Program Files\7-Zip\7z.exe"
:: get date
for /F "tokens=2-4 delims=/ " %%i in ('date /t') do (
set yy=%%i
set mon=%%j
set dd=%%k
)
:: get time
for /F "tokens=5-8 delims=:. " %%i in ('echo.^| time ^| find "current" ') do (
set hh=%%i
set min=%%j
)
echo dirName=%yy%%mon%%dd%_%hh%%min%
set dirName=%yy%%mon%%dd%_%hh%%min%
:: switch to the "data" folder
pushd %mysqlDataDir%
:: iterate over the folder structure in the "data" folder to get the databases
for /d %%f in (*) do (
if not exist %backupDir%\%dirName%\ (
mkdir %backupDir%\%dirName%
)
%mysqldump% --host="localhost" --user=%dbUser% --password=%dbPassword% --single-transaction --add-drop-table --databases %%f > %backupDir%\%dirName%\%%f.sql
%zip% a -tgzip %backupDir%\%dirName%\%%f.sql.gz %backupDir%\%dirName%\%%f.sql
del %backupDir%\%dirName%\%%f.sql
)
popd
Now I have had a good look at the following questions:
Batch file to delete files older than N days
https://serverfault.com/questions/49614/delete-files-older-than-x-days
Batch file that keeps the 7 latest files in a folder
Windows batch file only keeping the last 30 files
and am now wondering if I can simply add (https://stackoverflow.com/a/14267137/1010918)
for /f "skip=30 delims=" %%A in ('dir /a:-d /b /o:-d /t:c *.sql ^2^>nul') do if exist "%%~fA" echo "%%~fA"
(yes I will later change echo to del but first I like to see what will happen)
or (https://stackoverflow.com/a/13368077)
for /f "skip=30 eol=: delims=" %%F in ('dir /b /o-d *.sql') do #del "%%F"
at the end of the batch file, right under
del %backupDir%\%dirName%\%%f.sql
to make this happen?
I have never done this before but have searched for automated local backup apps/php scripts/mysqldump commands/etc for MySQL dbs, even had a go with Workbench only to discover that no scheduling can be set in the community edition (thank you Oracle).
All the other apps either need to have someone open the app and hit "run now" or want you to pay for setting up a schedule (no thanks).
I think this can be done with the tools at hand on a Windows 7 and later versions machine. Please help me add this functionality to the script, that would be great, thank you.
edit1:
When adding the quote commands nothing happens.
Also the created backup directory only displays the time but not the year, month and day. Doing further research to find out why. Any ideas?
This comment delete all but X most recent folders talks about deleting the 5 latest folders, though when I use it like this
for /f "skip=2 delims=" %%a in ('dir %backupDir%\%dirName% /o-d /b') do rd /S /Q "%backupDir%\%dirName%\%%a"
the error is the following.
The system cannot find the file specified.
The system cannot find the path specified.
edit2:
Below is the code that with #foxidrive help sets the folder name as I like to have it, but the last bit, trying to only keep the 3 latest folders (for testing purposes only 3) and delete the rest of the folders in the backupDir does not seem to work out.
Thank you for any help.
#echo off
set dbUser=root
set dbPassword=root
set "backupDir=D:\MySQLDumps\dbs\"
set "mysqldump=C:\wamp\bin\mysql\mysql5.6.17\bin\mysqldump.exe"
set "mysqlDataDir=C:\wamp\bin\mysql\mysql5.6.17\data"
set "zip=C:\Program Files\7-Zip\7z.exe"
rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.
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=%YY%-%MM%-%DD% %HH%-%Min%-%Sec%"
echo "dirName"="%dirName%"
pause
:: switch to the "data" folder
pushd "%mysqlDataDir%"
:: create backup folder if it doesn't exist
if not exist "%backupDir%\%dirName%\" mkdir "%backupDir%\%dirName%"
:: iterate over the folder structure in the "data" folder to get the databases
for /d %%f in (*) do (
echo processing folder "%%f"
"%mysqldump%" --host="localhost" --user=%dbUser% --password=%dbPassword% --single-transaction --add-drop-table --databases %%f > "%backupDir%\%dirName%\%%~nxf.sql"
"%zip%" a -tgzip "%backupDir%\%dirName%\%%~nxf.sql.gz" "%backupDir%\%dirName%\%%~nxf.sql"
del "%backupDir%\%dirName%\%%~nxf.sql"
)
popd
:: delete all but the latest 3 folders
for /f "skip=3 delims=" %%A in ('dir /b /ad /o-n "%backupDir%\%dirName%\*"') do #echo rd /s /q "%backupDir%\%dirName%\%%~A"
pause
With the help of #foxidrive above I managed to get the date of the folders as I wanted them to be, them being YYYY-MM-DD HH-MIN-SEC.
In these folders are the the gzipped .sql databses stored thanks to adityasatrio's MySQL Backup Batch Script.
With the help of #Magoo from this answer https://stackoverflow.com/a/17521693/1010918 I managed to get all folders (nameDir) deleted while keeping the latest N folders (nameDir) and also not touching any files that might be in the directory (backupDir).
Here is the complete working script.
Feel free to remove any occurrence of pause and and echo to not see what is going on inside the command prompt.
Additionally add this to Windows Task Scheduler and you have yourself a solid backup solution for a local development environment that makes use of MySQL databases.
Please thank the people that helped me get this done. Without you guys I would have had to use a costly Windows app only to locally save MySQL databases.
(..and for our next trick we are going to email an error log to ourselves if there are errors while backing up the .sql files.. but that is another question and story for another day.. )
#echo off
set dbUser=root
set dbPassword=root
set "backupDir=D:\MySQLDumps"
set "mysqldump=C:\wamp\bin\mysql\mysql5.6.17\bin\mysqldump.exe"
set "mysqlDataDir=C:\wamp\bin\mysql\mysql5.6.17\data"
set "zip=C:\Program Files\7-Zip\7z.exe"
:: https://stackoverflow.com/a/31789045/1010918 foxidrive's answer helped me get the folder with the date and time I wanted
rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.
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=%YYYY%-%MM%-%DD% %HH%-%Min%-%Sec%"
:: remove echo here if you like
echo "dirName"="%dirName%"
:: switch to the "data" folder
pushd "%mysqlDataDir%"
:: create backup folder if it doesn't exist
if not exist "%backupDir%\%dirName%\" mkdir "%backupDir%\%dirName%"
:: iterate over the folder structure in the "data" folder to get the databases
for /d %%f in (*) do (
:: remove echo here if you like
echo processing folder "%%f"
"%mysqldump%" --host="localhost" --user=%dbUser% --password=%dbPassword% --single-transaction --add-drop-table --databases %%f > "%backupDir%\%dirName%\%%~nxf.sql"
"%zip%" a -tgzip "%backupDir%\%dirName%\%%~nxf.sql.gz" "%backupDir%\%dirName%\%%~nxf.sql"
del "%backupDir%\%dirName%\%%~nxf.sql"
)
popd
:: delete all folders but the latest 2
:: https://stackoverflow.com/a/17521693/1010918 Magoo's answer helped me get what I wanted to do with the folders
:: for /f "skip=2 delims=" %G in ('dir /B /ad-h /o-d') DO echo going to delete %G
:: below following my version with rd (remove dir) command and /s and /q
:: remove echo before rd to really delete the folders in question!!
:: attention they will be deleted with content in them!!
:: change the value after skip= to what you like, this is the amount of latest folders to keep in your backup directory
for /f "skip=2 delims=" %%a in (' dir "%backupDir%\" /b /ad-h /o-d') do echo rd /s /q "%backupDir%\%%a"
:: remove pause here if you like and add the file to Windows Task Manager
pause
This is a little more resilient to spaces in folder names, and the date and time routines have been altered
- run it and first check that the "dirName"= folder is in the right format
- and the line at the end should echo the del commands for keeping the lastest 3 backups.
Test the archiving routine and then
remove the echo before the del keyword if it all looks right to you.
#echo off
set dbUser=root
set dbPassword=root
set "backupDir=D:\MySQLDumps\dbs\"
set "mysqldump=C:\wamp\bin\mysql\mysql5.6.17\bin\mysqldump.exe"
set "mysqlDataDir=C:\wamp\bin\mysql\mysql5.6.17\data"
set "zip=C:\Program Files\7-Zip\7z.exe"
rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.
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=%YY%%MM%%DD%_%HH%%Min%"
echo "dirName"="%dirname%"
pause
:: switch to the "data" folder
pushd "%mysqlDataDir%"
:: create backup folder if it doesn't exist
if not exist "%backupDir%\%dirName%\" mkdir "%backupDir%\%dirName%"
:: iterate over the folder structure in the "data" folder to get the databases
for /d %%f in (*) do (
echo processing folder "%%f"
"%mysqldump%" --host="localhost" --user=%dbUser% --password=%dbPassword% --single-transaction --add-drop-table --databases %%f > "%backupDir%\%dirName%\%%~nxf.sql"
"%zip%" a -tgzip "%backupDir%\%dirName%\%%~nxf.sql.gz" "%backupDir%\%dirName%\%%~nxf.sql"
del "%backupDir%\%dirName%\%%~nxf.sql"
)
popd
::keep 3 newest backup *.sql files
for /f "skip=3 delims=" %%a in ('dir "%backupDir%\%dirName%\*.sql" /b /o-d /a-d') do echo del "%backupDir%\%dirName%\%%a"
pause
What I want to do is run a backup task in Coldfusion (probably in a scheduled task) which will back up the structure and data in a MySql database.
The hosting server I use always blocks the use of cfexecute for security purposes so I can't use mysqldump.
e.g.
<cfexecute name="c:\program files\mysql\mysql server 4.1\bin\mysqldump"
arguments="--user=xxx --password=yyy dharma"
outputfile="#expandPath("./ao.sql")#" timeout="30"/>
(From Raymond Camden)
Are there any other options available to me ?
Backing up database files is a good idea, but if you back them up to the same drive, and the drive fails, you are screwed. I backup my databases daily to my local system. Here is the script I use in a .bat file
#ECHO OFF
#REM Set dir variables. Use ~1 format in win2k
SET basedir={directory where zip files will be put}
SET workdir={Working directory}
SET mysqldir=c:\PROGRA~1\mysql\mysqls~1.5\bin
SET gzipdir=c:\PROGRA~2\GnuWin32\bin
SET mysqlpassword={db password}
SET mysqluser={db user}
SET host={host IP or domain name}
for /f "tokens=1-4 delims=/ " %%a in ('date/t') do (
set mm=%%a
set dd=%%b
set yy=%%c
)
ECHO Check connection
PING -n 1 %host%|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS
IF ERRORLEVEL 1 goto :END
:SUCCESS
ECHO Connection found, run backup
#REM Change to mysqldir
CD %mysqldir%
#REM dump database. This is all one line
mysqldump -h %host% -u %mysqluser% -p%mysqlpassword% --databases {space delimited list of databases to backup >%workdir%\backup.sql
#REM Change to workdir
CD %workdir%
#REM Zip up database
%gzipdir%\gzip.exe backup.sql
#REM Move to random file name
MOVE backup.sql.gz %basedir%\%yy%_%mm%_%dd%_backup.gz
#REM Change back to base dir
CD %basedir%
:END
ECHO No connection, do not run
I use Windows task scheduler to run this every night. You could probably update it to remove older backups.
You will need to make sure you have gzip installed.
This will put copies of the DB on your local system - I then use a backup service to back up the backups to another offsite system.