Automatic mysql backup on xampp windows 8? - mysql

I recently got in problem, my hard disk got crashed!
ibdata1 file at "F:\xampp\mysql\data\ibdata1" in my hard disk got corrupt!
The file size was 8 gb but when I was trying to copy after 6 gb it was showing redundancy check error!
I recently installed new xampp installation, just wondering if there is any way that it create automatic backup of files like webhosting service provider does.
Any help will be much appreciated.

To extend answer you could use script below (for XAMPP or other mysql configuration). After creating bat file it can be set up in Windows Scheduler as weekly task for example.
This script would create files like C:\backups\db1-2014-03-27.sql depending on date
mysqldumper.bat
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%yyyy%-%mm%-%dd%
C:\xampp\mysql\bin\mysqldump.exe db1 -h localhost -u user1 -pPassWord1 > C:\backups\db1-%date%.sql
C:\xampp\mysql\bin\mysqldump.exe db2 -h localhost -u user2 -pPassWord2 > C:\backups\db2-%date%.sql

I use a batch file that calls mysqldump and is executed on a schedule by Windows Task Scheduler.
First you need to create a batch file. Open a new text file and inlclude something similar to this. Save it as a .BAT. You have to update the location of your MySQL bin. on the first line. This should be in your xamppfiles directory.Then include the proper mysql user name, database name and password in the mysqldump command
REM Export all data from this database
cd C:\Program Files\MySQL\MySQL Server 5.6\bin
REM To export to file (structure only)
mysqldump --no-data [DATABASENAME] -h localhost -u [USER] -p[PASSWORD] > C:\databackup\database_ddl_backup.sql
mysqldump --no-create-info --no-create-db [DATABASENAME] -h localhost -u [USER] -p[PASSWORD] > C:\databackup\database_data.sql
Then you just have to schedule this batch file using Windows Task Scheduler. Here is an article that explains this for W7 and W8:
http://www.thewindowsclub.com/how-to-schedule-batch-file-run-automatically-windows-7

Just to extend a little more the given answers in case anyone wants to add time to the generated backup filenames.
Using this: https://stackoverflow.com/a/19741875
We can build the following batch file:
FOR /f %%a IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET DTS=%%a
SET date=%DTS:~0,8%-%DTS:~8,6%
C:\xampp\mysql\bin\mysqldump.exe db1 -h localhost -u user1 -pPassWord1 > C:\backups\db1-%date%.sql
C:\xampp\mysql\bin\mysqldump.exe db2 -h localhost -u user2 -pPassWord2 > C:\backups\db2-%date%.sql

Related

Windows Batch file to create MySQL dump adding variable

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.
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

MySQL Database backup automatically on a windows server

Is there a way to back up MySQL database automatically at certain times of the day for designated servers or send an email with an attachment.. Which ever do you think is the best and safest way to achieve this?
Best way to do this would be
mysqldump.exe --user=YourUserName --password=YourPassword --host=localhost --port=3306 --result-file="Path\dump.sql" --databases "DatabaseName1" "Database2"
mysqldump.exe --user=root --password=root --host=localhost --port=3306 --result-file="c:\www\db\backup.%date:~10,4%%date:~7,2%%date:~4,2%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "dbtest1" "dbtest2"
The pattern backup.%date:~10,4%%date:~7,2%%date:~4,2%.sql will create a unique name (backup20131010.sql) each time it will run
Now you just need to call this command in your task scheduler. That's it. :)
I would use Windows Task Scehduler/cron (depending on your system) and mysqldump. Scroll down in the link, it includes some insights how to achieve what you want.
You can add one of these commands to Windows task scheduler:
mysqldump –-user [username] –-password=[password] [database name] > [dump file]
or in a compact way:
mysqldump –u[username] –p[password] [database name] > [dump file]
or:
mysqldump -u[user] -p[password] --result-file="c:\<path>\backup.%DATE:~0,3%.sql" [database]
databaseW.2016,06,29-22,31,48-15.sql
#echo off
rem Backup Database (Daily,via Task Scheduler)
rem databaseW
set filename="c:\xampp\dbk\databaseW.%date:~6,4%,%date:~0,2%,%date:~3,2%-%time:~0,2%,%time:~3,2%,%time:~6,2%-%time:~9,2%.sql"
c:\xampp\mysql\bin\mysqldump.exe --user=root --password=dell#root --host=localhost --port=3306 --result-file=%filename% --default-character-set=utf8 --single-transaction=TRUE --databases "databaseW"
To create file whose name is based on the date and time, use %date% and %time%.
Note that the 2 variables are based on locale and cmd shell version
open the cmd windows
input echo %time% and echo %date%
mine is 22:11:16.80,06/29/2016 Wed
substr the variable through %variable:~startpos,length%
I want the time delimited by comma, so the cmd goes
echo %time:~0,2%,%time:~3,2%,%time:~6,2%,%time:~9,2%
to get a filename like databaseW.2016,06,29-22,31,48-15.sql
use set filename="databaseW.%date:~6,4%,%date:~0,2%,%date:~3,2%-%time:~0,2%,%time:~3,2%,%time:~6,2%-%time:~9,2%.sql"
check the date and time in advance
use the --result-file option instead of >; According to the Mysql Manuel, the charset of file saved using ">" is UTF-16, while the --result-file follows the --default-character-set
save to file BackpDay-databaseW.cmd
add it to a new task Action and set a trigger (Windows Task Scheduler)
I did the work, similar to what other people explained through... but with little difference and extra work:
1) I made a batch file
2) Ran that batch file through windows scheduler
3) Made appropriate schedule for that task
4) Inside the batch file these steps are executed:
4-1) Prepared a file name based on current date
4-2) Got a backup by mysqldump.exe in the corresponding directory & file name
4-3) Made a compress file through 7-Zip app(install it) & then delete the uncompressed backup
4-4) Put a copy on our network File-Server
Here is a script(.bat) sample:
#echo off
set current=%date:~10,4%%date:~4,2%%date:~7,2%
set filename="E:\MySQL Backups\DBName-%current%.sql"
set filename2="E:\MySQL Backups\DBName-%current%.zip"
echo %filename%
cd "E:\MySQL Backups"
C:\"Program Files"\MySQL\"MySQL Server 5.5"\bin\mysqldump.exe db_name --user=root --password=rootpass --host="127.0.0.1" --port=instancePort --result-file=%filename% --default-character-set=utf8 --single-transaction=TRUE
echo backup-finished
if exist %filename% (
"C:\Program Files\7-Zip\7z.exe" a %filename2% %filename%
echo zip-finished
del %filename%
)
if exist %filename2% (
copy %filename2% "\\192.168.x.x\MySQL Backups"
echo copy-finished
)

MySQL full backup with mysqldump.exe

set dateStr=%date:~-7,2%-%date:~-10,2%-%date:~-4,4%
"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin\mysqldump.exe" -u user -p password --all-
databases --single-transaction --flush-logs --master-data=2 > full_backup_%dateStr%.sql
it works for another server we have. this is a new server but with the same database.
It only creates a 1 KB file with the content:
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
please help.
Found this question when I was trying to get my own Mysql database backed up and thought I'd share how I ended up doing it.
The code basically loops over all databases and creates a .sql file containing all structure and data for each database.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: MySQL Backup
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: MySQl DB user
set dbuser=BackupUser
:: MySQl DB users password
set dbpass=BackUpUserPassWord
:: Switch to the MySQL data directory and collect the folder names
pushd "C:\Server\databases\mysql\data"
:: Loop through the folders and use the file names for the sql files, collects all databases automatically this way
:: Pass each name to mysqldump.exe and output an individual .sql file for each
FOR /D %%F IN (*) DO (
"C:\Program Files\MySQL\bin\mysqldump.exe" --user=%dbuser% --password=%dbpass% --databases %%F > "C:\Server\backups\mysql\%%F.%TODAY%.sql"
)
To answer your specific question, check to see if you are running the script as administrator and that the user you are trying to backup using has relevant privileges to the databases. I was stuck on this very problem until I ran my bat file as administrator.
To check if its a user problem, try running the mysqldump with the root user and see if that helps, if it does you know its a problem with database rights..
To run as administrator right click the bat file, and select Run as administrator, and don't forget to set the checkbox "run with highest privileges" in the scheduled task if you are using that.
Code is copied from here:
http://www.syntaxwarriors.com/2012/backup-up-a-windows-server-using-only-free-tools/
I testes your code and it worked. I think you should check again your bat file for any syntax mistake.
set dateStr=%date:~-7,2%-%date:~-10,2%-%date:~-4,4%
"D:\Sync\Apps\Wamp\bin\mysql\mysql5.5.20\bin\mysqldump.exe" -u root --all-databases --single-transaction --flush-logs --master-data=2 > full_backup_%dateStr%.sql
I removed the -pPASSWORD part because there is no password for root on my local server. So, if you're going to C/P it from here, don't forget to change mysqldump's path and to add that -p part.

Mysql Windows "mysqldump -t" restore

Yes it's Windows sorry.
I'm using mysqldump with the option -T which creates a sql and a txt file per table.
mysqldump -u user -ppass db -T path
I use that option to be able to restore easily one table.
Now I'd like to restore all the tables.
mysql -u user -ppass db < path/*.sql
Obvously doesn't work
Also, I don't know where do my funcs/procs go.
You could use a FOR loop with the file wildcard (*.sql) to process each one, like this:
FOR /R %F in (*.sql) DO (
mysql -u user -ppass database %F
)
(Note that if you're running this from a batch file, the variable should be shown as %%F instead of just %F.)

How to create mysqldump batch file?

I tried to create a batch file that can backup all my databases. My systems details:
OS: Server -> Windows Server 2003, Testing/local machine -> Windows Vista
Databases: MySql 5.XX
Batch file:
#echo off
START C:\wamp\bin\mysql\mysql5.1.33\bin\mysqldump.exe --opt -h localhost -uroot -psecret testdb | gzip > dump.sql");
In my code, i try to dump the "testdb" database into dumb.sql file. Can I set the name into such like : "dbname_date_time.sql"??
I answered my own question. Whoever that has the privilleges, please close/delete this question. Thanks in advance :)
I will assume you have no problem with mysqldump command.
So, to manipulate date/time in bat file, you can use combinations of followings
echo %date% which gives me "木 2010/01/14" in my pc
echo %time% which gives me 4:02:15.28
for /f "tokens=1-5 delims=/" %%d in ("%date%") do echo %%e %%f gives me "01 14"
for /f "tokens=1-5 delims=:" %%d in ("%time%") do echo %%d %%e gives me "4 02" H/M
Build file name and rename your dump.sql to new filename
since you have mysql in your system, you can use the date/time functions that comes with mysql
execute it using the mysql client and use a for loop to get the result. Then pluck the return result into your dump file variable name