How do I do this properly. I'm trying to name the sql file that is being produced by mysqldump into the current date and time. I've already some research in this site and found a code in here: How to get current datetime on Windows command line, in a suitable format for using in a filename?
Tried to mixed it up with my current code and I came up with this one. The file is named into the current date and time but its only a 1kb file and does not produce a .sql file.
It is supposed to be a 7 kb sql file.
#For /f "tokens=2-4 delims=/ " %%a in ('date /t') do #(set mydate=%%c-%%a-%%b)
#For /f "tokens=1-2 delims=/:" %%a in ('time /t') do #(set mytime=%%a%%b)
#echo mydate= %mydate%
#echo mytime= %mytime%
mysqldump -u root -p --add-drop-table --create-options --password= onstor >c:\%mydate%_%mytime%.sql
UPDATE
I don't think there's a problem with the mysqldump command since it works well when I do it this way. The code below just uses the date as its filename.
#For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do #(
Set Month=%%A
Set Day=%%B
Set Year=%%C
)
#echo DAY = %Day%
#echo Month = %Month%
#echo Year = %Year%
mysqldump -u root --add-drop-table --create-options --password= onstor >c:\%Day%-%Month%-%Year%.sql
Please help, thanks.
On Linux, simply put $(date +%Y-%m-%d-%H.%M.%S) to show date and time in the file name, so it looks like:
mysqldump -u <user> -p <database> | bzip2 -c > <backup>$(date +%Y-%m-%d-%H.%M.%S).sql.bz2
(This command also compresses the file using bzip2)
I think the syntax of your mysqldump command is wrong;
mysqldump -u root -p --add-drop-table --create-options --password= onstor
You use both -p and --pasword=, you should only use one option. And there is a space before the password.
Just try to run the mysqldump command on the commandline to see error messages. Alternatively add 2>&1 at the end of the command in the batchfile. Then you would also see error messages in the output file.
mysqldump -u root --add-drop-table --create-options --password=onstor >c:\%mydate%_%mytime%.sql 2>&1
For those who want to get this to work in crontab, in that case it's a bit different:
30 01 * * * /opt/bitnami/mysql/bin/mysqldump -u root --password=MySecretPass database_name | gzip > /path/backup/`date "+\%d-\%m-\%y_\%H:\%M"`.gz
Related
im trying to create a batch file that goes thorugh each text file in a folder and looks for specific words such as "msg" "file" "size" in each line. If those words are found then it sends and me an email.
Im using SQL server to send the email, and im calling the email stored procedure from my batch file like this:
set MYDB= yourDBname
set MYUSER=youruser
set MYPASSWORD=yourpassword
set MYSERVER=yourservername
sqlcmd -S %MYSERVER% -d %MYDB% -U %MYUSER% -P %MYPASSWORD% -h -1 -s "," -W -Q "exec yourstoredprocedure"
I just need help writing the script which checks for specific words in each line in each .txt file
Just give a try for this batch file :
#echo off
Title Search String into text files
Set "ROOT=%~dp0"
set "String2Search=msg size file"
For %%a in (%String2Search%) do (
FOR /f "delims=" %%f IN ('dir /b /s "%ROOT%\*.txt"') DO (
(find /I "%%a" "%%f" >nul 2>&1) && ( Call :FoundString "%%a" "%%f" ) || ( Call :NoFound "%%a" "%%f" )
)
)
pause & exit
::*************
:FoundString
echo found %1 on file %2
goto :eof
::*************
:NoFound
echo no string like %1 found on file %2
goto :eof
::*************
Few suggestions as i cannot comment.I had done a project few years back and i had to do search files with a particular extension and read them to find particular words and do something with them.I remember few things only.I hope it helps you.
To find all txt files:
find /home/user/Downloads/etc -name '*txt'
To read a whole file and search for "msg" "file" "size":
while read -r LINE
do
grep -i "msg" | grep -i "file" | grep -i "size"
to check all in one line
Or you can do it executing one by one without "pipelining".
P.S I don't have linux installed otherwise I would have checked before posting.Sorry if not correct.
This should do the job:
#ECHO OFF
CD C:\wherever\your\.txt\files\are\at
FOR /R %%G IN ("*.txt") DO (FINDSTR /I /C:"msg" /C:"file" /C:"size" "%%G" >nul && GOTO match_found)
GOTO no_match
:match_found
ECHO Match found^!
set MYDB= yourDBname
set MYUSER=youruser
set MYPASSWORD=yourpassword
set MYSERVER=yourservername
sqlcmd -S %MYSERVER% -d %MYDB% -U %MYUSER% -P %MYPASSWORD% -h -1 -s "," -W -Q "exec yourstoredprocedure"
:no_match
ECHO No match found^!
PAUSE
If the batch file is in the same folder as the .txt files you can delete the CD line.
If the search should be case-sensitive remove the /I option.
To satisfy your question as asked, this should suffice:
#Echo Off
FindStr /IR "\<msg\> \<file\> \<size\>" "C:\Users\NT-Hero\*.txt">Nul||Exit /B
Rem Your 'match found' commands here
If you wanted to also search in sub-directories change the FindStr options to /SIR. (See FindStr /? for more options).
Can someone please help me out?
Currently, I've got this:
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
set ip=%ip:~1%
Now, I need to create a abcd.bat file (from this .bat file) that will contain a line in below format:
abcy.exe -c %ip% -i2 -u -b 45M -p5600 -t 200 -B 1.2.3.4
Here's an option:
#Set "ts=#abcy.exe -c%%ip%% -i2 -u -b 45M -p5600 -t 200 -B 1.2.3.4"
#For /F "Tokens=2Delims=:" %%A In ('IPConfig^|Find "IPv4"') Do #Set "ip=%%A"
#(Call Echo %ts%)>"abcd.bat"
I've assigned your output to a variable, %ts%, on the first line so that you can easily adjust the output text string, (just leave %%ip%%, which has a leading space, alone).
Add this to your snippet:
echo abcy.exe -c %ip% -i2 -u -b 45M -p5600 -t 200 -B 1.2.3.4 > new.bat
I am looking for a solution - a script to automate the process of taking backup of a database in PostgreSQL. As of now I do it manually, that is by right clicking on the db and clicking the backup option.
I did some research and ended up with a script which solves the issue pretty much, ie:
#ECHO OFF
#setlocal enableextensions
#cd /d "%~dp0"
SET PGPATH=C:\PostgreSQL\9.4\bin\
SET SVPATH=d:\
SET PRJDB=Test
SET DBUSR=postgres
FOR /F "TOKENS=1,2,3 DELIMS=/ " %%i IN ('DATE /T') DO SET d=%%i-%%j-%%k
FOR /F "TOKENS=1,2,3 DELIMS=: " %%i IN ('TIME /T') DO SET t=%%i%%j%%k
SET pg_dump=%PRJDB%_%d%_%t%.backup
#ECHO OFF
%PGPATH%pg_dump -h localhost -p 5432 -U postgres %PRJDB% > %SVPATH%%pg_dump%
echo Backup Taken Complete %SVPATH%%pg_dump%
pause
It did take the backup, but the file generated was a sql file, though I did change the extension to .backup. As a result, if I need to restore the DB, and try to restore from the file generated it is not possible. Can someone please provide me with a solution to this problem.
Thanks in advance.
Following script can be used to get the Postgres backup with .backup extension
#echo off
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
set datestr=%day%_%month%_%year%
echo datestr is %datestr%
set BACKUP_FILE=C:\Users\slan\Desktop\backup_test\DBNAME_%datestr%.backup
echo backup file name is %BACKUP_FILE%
SET PGPASSWORD=YOUR_PASSWORD
echo on
bin\pg_dump -i -h localhost -p 5432 -U postgres -F c -b -v -f %BACKUP_FILE% YOUR_DB_NAME
you must have PostgreSQL's bin folder(Ex. C:\PostgreSQL\9.4\bin) along with this script otherwise this will not work as you expect
To schedule the task you can use Windows Task scheduler, here is an example for how to ?? - How to schedule a Batch File to run automatically in Windows 10 / 8 / 7
You can use PGagent for scheduling batch it have multiple advantages over windows scheduler bat scripts
I got a batch file to do mysqldump. The code is like this:
#echo off
echo Starting Backup of Mysql Database on server
for /F "tokens=2,3,4 delims=/ " %i in ('date /t') do set myDate=%k%i%j
set bkupfilename=%myDate%.sql
echo Backing up to file: %bkupfilename%
C:\xampp\mysql\bin\mysqldump --routines -u <user> -p<pwd> <database> > D:\MYSQL_DAILY_BACKUPS\"<database>%bkupfilename%"
When I run it on cmd console in Win7 by typing the batch file, it won't work and complain about:
C:\xampp\mysql\bin>mysqldumpbatch
Starting Backup of Mysql Database on server
kj was unexpected at this time.
But when I run it by copy pasting the code directly to command prompt it run just fine and produce file 20152401.sql. Anyone know why?
The single % variant only works from the command line. Try replacing with %% like so:
#echo off
echo Starting Backup of Mysql Database on server
for /F "tokens=2,3,4 delims=/ " %%i in ('date /t') do set myDate=%%k%%i%%j
set bkupfilename=%myDate%.sql
echo Backing up to file: %bkupfilename%
C:\xampp\mysql\bin\mysqldump --routines -u <user> -p<pwd> <database> > D:\MYSQL_DAILY_BACKUPS\"<database>%bkupfilename%"
Let me know if that works?
This works:
SET server=MyServer
SET db=MyDb
FOR /F "usebackq tokens=1" %%i IN (`sqlcmd -S %server% -d %db% -w200 -h-1 -E -Q "set nocount on; select REPORTING_DATE FROM dbo.CURRENT_REPORTING_DATE"`) DO set REPORTING_DATE=%%i
ECHO The Reporting Date is %REPORTING_DATE%
But when I try to fully qualify the path to sqlcmd...
SET sqlcmdexe="C:\Program Files\Microsoft SQL Server\100\Tools\Binn\sqlcmd.exe" SET server=MyServer SET db=MyDb
FOR /F "usebackq tokens=1" %%i IN (` %sqlcmdexe% -S %server% -d %db%
-w200 -h-1 -E -Q "set nocount on; select REPORTING_DATE FROM dbo.CURRENT_REPORTING_DATE"`) DO set REPORTING_DATE=%%i ECHO The Reporting Date is %REPORTING_DATE%
I get the error:
The system cannot find the path specified.
...presumably because of the spaces in the folder name.
How do I change the path to a tilde path (w/o spaces) or better yet, quote it so that this statement executes properly?
Note that there is a backwards tic before %sqlcmdexe% , not sure why I don't see it, at least in IE6. Yes, 6!
How do I change the path to a tilde path (w/o spaces)
As I don't have sqlcmd.exe installed, I use a different example. See for example this:
#echo off
set sqlcmdexe=C:\Program Files\Internet Explorer\Connection Wizard\icwconn2.exe
echo %sqlcmdexe%
for /f "tokens=*" %%a in ("%sqlcmdexe%") do set sqlcmdexe=%%~sa
echo %sqlcmdexe%
Run on my system, the output is:
C:\temp>envtest
C:\Program Files\Internet Explorer\Connection Wizard\icwconn2.exe
C:\PROGRA~1\INTERN~1\CONNEC~1\icwconn2.exe
But I don't know if this solves your problem.
You have to use the quotes to work with Strings... but You have never use spaces next to equal sign:
set "sqlcmdexe=c:\Program Files\Internet Explorer\Connection Wizard\icwconn2.exe"
echo.%sqlcmdexe%
Hope it helps =)