0

I have created the following windows batch file backup.bat:

cls
mysqldump -u root -p my_database_name > BACKUP.sql

My problem is that batch create the same file in the same path that it has been run from. I want to add some timestamp or date to the file name BACKUP.sql to be something like BACKUP_2330255555588.sql to make different files in the same place.

Another side: for restore I need a batch file that prompt input for the file name.

1 Answer 1

3

You can try following snippets:

Backup file:

SET backupPath="C:\backups\"
FOR /F "TOKENS=2-4 DELIMS=/ " %A IN ('DATE /T') DO SET date=%%C-%%A-%%B
For /f "tokens=1-4 delims=/: " %a in ('echo %time%') do (set mytime=%a-%b-%c-%d)
SET timestamp=%date%_%mytime%
mysqldump -u ROOT --password=PASS my_database_name > "%backupPath%BACKUP%timestamp%.sql"

Recovery file with prompt:

@echo off
echo "Running backup script"
set /p BackupFilePath= Enter the recovery file path? 
@echo on
mysql -u ROOT --password=PASS my_database_name<%BackupFilePath%

Bru

Sign up to request clarification or add additional context in comments.

1 Comment

There is another issue, if there is some error in mysqldump, it write an corrupted file. I need to know how to make the batch file recognize that error and delete the file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.