Mysql compressed backup. Creating zero byte empty file - mysql

$command = 'mysqldump.exe -u '.$username.' '.$database . ' > '.$location.'\\'.$filename;
system($command);
this can create sql file correctly.
Now i'm trying to create compressed backup.
$filename = "xyz.sql.gz";
$command = 'mysqldump.exe -u '.$username.' '.$database . ' |gzip > '.$location.'\\'.$filename;
system($command);
This is creating a gz file with zero byte size.
please help, where im am doing wrong.
I didnot put -p[password] as the user i used is without password.
Thanks in advance.

Are you missing Gzip application on windows? or is it in PATH or in same directory than script?

Related

Execute mysql dump using power shell

I am setting up a new Azure database using mysql server with a database. I want to dump a .sql file to this database using powershell
Executing the below code throws exception in '<', since it is reserved for future use, as a workaround I tired putting the entire statement in quotes.Still facing same issue.
$path = "C:\tools\mysql\current\bin\mysql.exe"
&"$path" -h $servername -u $username -p $databasename < filename.sql
The expected result is to dump the filename.sql into mysql server database.
Try something like this and see if it helps:
Use Get-Content to read the file and pipe | it to your command. Use & to run the command.
get-content 'c:\folder\backup.sql' | &"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe" -u user --password=pass dbnamedbname

mysqldump generates empty dump from my localhost

Good day,
I am trying to generate a dump of mysql db using bash.
I created a sample .sh using this link
#!/bin/bash
# Database credentials
user="root"
password=""
host="localhost"
db_name="sampledb"
# Other options
backup_path="C:\_backup"
date=$(date +"%d-%b-%Y")
# Set default file permissions
umask 177
# Dump database into SQL file
mysqldump --user=$user --password= --host=$host $db_name > $backup_path/$db_name-$date.sql
The dump file is created but it with 0kb (empty)
what part of my bash is the problem? (me, first time to create something like this)
UPDATED CODE
UPDATE
It seems that I need to add mysqldump path in my environment variable since
I am running it in win7.
Thanks in advance
Maybe the problem is coming from your password ?
I think that you should try to save it in a $password = ""
You are using a bash script (so presumably some flavour of linux/unix) but a Windows path, are you in cygwin? Even on cygwin you need a shebang at the top of the script (#!/bin/bash). Anyway you haven't added the password variable to the command line (or populated it, but I guess that's deliberate), try:
mysqldump --user=$user --password=$password --host=$host $db_name > $backup_path/$db_name-$date.sql
Your umask variable isn't doing anything. Using a password in a file like this can be insecure, make sure you have restricted access to the file to appropriate users.

PS Script to restore latest .sql backup file in to database

The scenario is that we have bunch of .sql dump files and new ones are created before every deployment.
If anything goes wrong with migration scripts someone has to manually drop/create schema and use command line to restore dump from the latest backup.
I am writing a PS script to automate this process.
find latest dump from given path
drop schema
create schema
restore dump.
I have accomplished first 3 steps but have wasted a lot of time on the 4th one:
Write-Host "Restoring: " $path
$command = '"mysql.exe -uUsername -pPassword ' + $dbname + ' < ' + $path + '"'
Write-Host $command
cmd /C $command
It says "The system cannot find the file specified."
If i use cmd $command without /C it starts cmd in powershell but doesn't execute $command.
I have tried different variations to execute the command in cmd but it doesn't seem to work, and the reason i will have to use cmd is because powershell doesn't play well with '<'.
I tried Invoke-Item and Invoke-Expression but can't guarantee i used correct syntax.
Any suggestions would be greatly appreciated.
Thanks

Create and restore Database using Batch file in Mysql and Windows XP

I am working in WindowsXP . My need is to create a database and restore that
database with data.sql file using batch script . This is needed while user configures the project . I tried the following method but it doesn't work for me.
// createdb.bat
// Password is empty
#echo off
mysql -u root -p < mysql.sql
//mysql.sql
CREATE DATABASE newdb ;
And i have both files in my Desktop . Any help please ?
Thanks for every one to reply my question . As Fuzzy Button said , that was a path issue . I have to set the path to xps.sql file . I have just created the createdb.bat with this code it is working well .
#echo off
set root=C:\Documents and Settings\Administrator
cd %root%
mysql -u root -e "CREATE DATABASE IF NOT EXISTS xps";
mysql -u root xps < xps.sql

Mysql ERROR at line 1153: Unknown command '\'

I am trying to import a mysqldump file via the command line, but continue to get an error. I dumped the file from my other server using:
mysqldump -u XXX -p database_name > database.sql
Then I try to import the file with:
mysql -u XXX -p database_name < database.sql
It loads a small portion and then gets stuck. The error I receive is:
ERROR at line 1153: Unknown command '\''.
I checked that line in the file with:
awk '{ if (NR==1153) print $0 }' database.sql >> line1153.sql
and it happens to be over 1MB in size, just for that line.
Any ideas what might be going on here?
You have binary blobs in your DB, try adding --hex-blob to your mysqldump statement.
You know what's going on - you have an extra single quote in your SQL!O
If you have 'awk', you probably have 'vi', which will open your line1153.sql file with ease and allow you to find the value in your database that is causing the problem.
Or... The line is probably large because it contains multiple rows. You could also use the --skip-extended-insert option to mysqldump so that each row got a separate insert statement.
Good luck.
I had the same problem because I had Chinese characters in my datasbase. Below is what I found from some Chinese forum and it worked for me.
mysql -u[USERNAME] -p[PASSWORD] --default-character-set=latin1
[DATABASE_NAME] < [BACKUP_SQL_FILE.sql]
I think you need to use path/to/file.sql instead of path\to\file.sql
Also, database < path/to/file.sql didn't work for me for some reason - I had to use use database; and source path/to/file.sql;.
If all else fails, use MySQLWorkbench to do the import. This solved the same problem for me.
I recently had a similar problem where I had done an sql dump on a Windows machine and tried to install it on a Linux machine. I had a fairly large SQL file and my error was happening at line 3455360. I used the following command to copy all text up to the point where I was getting an error:
sed -n '1, 3455359p' < sourcefile.sql > destinationfile.sql
This copied all the good code into a destination file. I looked at the last few lines of the destination file and saw that it was a complete SQL command (The last line ended with a ';') so I imported the good code and didn't get any errors.
I then looked at the rest of the file which was about 20 lines. It turns out that the export might not have completed b/c I saw the following php code at the end of the code:
Array
(
[type] => 1
[message] => Maximum execution time of 300 seconds exceeded
[file] => C:\xampp\htdocs\openemr\phpmyadmin\libraries\Util.class.php
[line] => 296
)
I removed the offending php code and imported the rest of the database.
I had special character in table names , like _\ and it give error when try to import that tables.
i fixed it by changing \ to \\ in dumped sql.
my table names where like rate_\ and i used this command to repair dump :
sed 's._\\._\\\\.g' dump.sql > dump2.sql
i didn't replace all backslashes , because i was not sure if there is some backslash somewhere in database that should not be replaces.
special characters in table name will be converted to # at sign in file name.
read http://dev.mysql.com/doc/refman/5.5/en/identifier-mapping.html
I have same error as,
Unknown command '\▒'.
when I ran this
mysql -u root -p trainee < /xx/yy.gz
So I'd followed these answers. But I did not got the restored db trainee. Then found that
yy.gz is zip file. So I restoring after unzip the file as:
mysql -u root -p trainee < /xx/yy.sql