restore all mysql database from a --all-database sql.gz file - backup

I've backed all my mysql databases with he following command
mysqldump -u root -ppasswod --all-databases | gzip > all.sql.gz
just wanted to know will I be able to restore all of the database with following command
gunzip < alldb.sql.gz | mysql -u root -ppassword -h localhost
can you also tell me how to back up all of mysql users too?
I cant test it because I'm not sure and I don't want to break any db on my current system

Yes. Generally, to restore compressed backup files you can do the following:
gunzip < alldb.sql.gz | mysql -u [uname] -p[pass] [dbname]
Please consult How to Back Up and Restore a MySQL Database
Note that the --all-databases option is applicable to backup only. The backup file itself will contain all the relevant CREATE DATABASE quux; commands for the restore.

This is the command I use to backup all databases in MySQL:
mysqldump -u USERNAME -p --all-databases --events --ignore-table=mysql.event --extended-insert --add-drop-database --disable-keys --flush-privileges --quick --routines --triggers | gzip > "all_databases.gz"
The '--all-databases' option tells the command to include all of the databases. If you want to specify one or more then remove that option and replace it with '--databases dbname1 dbname2 dbnameX'
To backup all of your mysql users, passwords, permissions then include the 'mysql' database in your backup. The --all-databases option includes this database in the backup.
The '--routines' option includes stored procedures and functions in the backup.
The '--triggers' option includes any triggers in the backup.
To restore from a *.gz mysqldump file:
gunzip < all_databases.gz | mysql -u USERNAME -p

To display a progress bar while importing a sql.gz file, download pv and use the following:
pv mydump.sql.gz | gunzip | mysql -u root -p
If PV command is not installed on your system then try below command relatively
In CentOS/RHEL
yum install pv
In Debian/Ubuntu
apt-get install pv
In MAC
brew install pv
Output Something like that -->
pv mydump.sql.gz | gunzip | mysql -u root -p dbname
Enter password:
255MiB 0:05:49 [ 748kiB/s] [===========> ] 30%

Related

How can I import a SQL Server RDS backup into a SQL Server Linux Docker instance?

I've followed the directions from the AWS documentation on importing / exporting a database from RDS using their stored procedures.
The command was similar to:
exec msdb.dbo.rds_backup_database
#source_db_name='MyDatabase',
#s3_arn_to_backup_to='my-bucket/myBackup.bak'
This part works fine, and I've done it plenty of times in the past.
However what I want to achieve now; is restoring this database to a local SQL Server instance; however I'm struggling at this point. I'm assuming this isn't a "normal" SQL Server dump - but I'm unsure what the difference is.
I've spun up a new SQL Server for Linux Docker instance; which seems all set. I have made a few changes so that the sqlcmd tool is installed; so technically the image I'm running is comprised of this Dockerfile; not much different.
FROM microsoft/mssql-server-linux:2017-latest
RUN apt-get update && \
apt-get install -y curl && \
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && \
apt-get update && \
apt-get install -y mssql-tools unixodbc-dev
This image works fine; I'm building it via docker build -t sql . and running it via docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=myPassword1!' -p 1433:1433 -v $(pwd):/backups sql
Within my local folder, I have my backup from RDS downloaded, so this file is now in /backups/myBackup.bak
I now try to run sqlcmd to import the data with the following command; and I'm running into an issue which makes me assume this isn't a traditional SQL dump. Unsure what a traditional SQL dump looks like, but the majority of the file looks garbled with ^#^#^#^# and of course other things.
/opt/mssql-tools/bin/sqlcmd -S localhost -i /backups/myBackup.bak -U sa -P myPassword1! -x
And finally; I get this error:
Sqlcmd: Error: Syntax error at line 56048 near command 'GO' in file '/backups/myBackup.bak'.
Final Answer
My final solution for this mainly came from using -Q and running a RESTORE query rather than importing with the file, but I also needed to include some MOVE options as they were pointing at Windows file paths.
/opt/mssql-tools/bin/sqlcmd -U SA -P myPassword -Q "RESTORE DATABASE MyDatabase FROM DISK = N'/path/to/my/file.bak' WITH MOVE 'mydatabase' TO '/var/opt/mssql/mydatabase.mdf', MOVE 'mydatabase_log' TO '/var/opt/mssql/mydatabase.ldf', REPLACE"
You should use the RESTORE DATABASE command to interact with your backup file instead of specifying it as an input file of commands to the database:
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P myPassword1! -Q "RESTORE DATABASE MyDatabase FROM DISK='/backups/myBackup.bak'"
According to the sqlcmd Docs, the -i flag you used specifies:
The file that contains a batch of SQL statements or stored procedures.
That flag likely won't work properly if given a database backup file as an argument.

Create and import pgsql database after pg_dump

I am new to postgres. I have exported a large, complex database with the following command in the terminal
pg_dump -U USERNAME DBNAME > dbexport.pgsql
Now that I have transferred this .pgsql file to a different computer, what is the right command to automatically create and restore the exact same database as was exported? Any suggestions would be appreciated
The way you dumped the database, the information about the database itself is not included in the dump (which is a plain SQL file).
You can either use the -C option to include CREATE DATABASE in the dump (the dump has to be restored with psql), or you use the custom format:
pg_dump -F c -U postgres DBNAME -f dbexport.pgsql
That can be restored with pg_restore like this:
pg_restore -C -d postgres -U postgres dbexport.pgsql

MySQL mysqldump.exe is only backing up schemes

I have this command which is only creating statements to re-create the database and schemes, but it's not backing up any of the data:
"C:\Program Files (x86)\MySQL\MySQL Workbench CE 6.0.6\mysqldump.exe" --user=myuser --password=mypassword --host=localhost --port=3306 --result-file="Z:\mysql-backup\backup.%date:~10,4%%date:~7,2%%date:~4,2%.sql" --default-character-set=utf8 --single-transaction=TRUE --databases "wpzb" "wptt"
What am I missing?
With the basic command:
mysqldump -u myuser -pmypassword --databases wpzb watt > dump_file.sql
You should get all the data and schema in one file
It might be best to execute the command for each database separately:
mysqldump -u myuser -pmypassword --default-character-set=utf8 --single-transaction=TRUE wpzb > wpzb_dump_file.sql
This way you have the schema and data for one database in one file!
Since the database is local and on the default port you can just use:
-u myuser -pmypassword
And leave the localhost and port attributes off.
I tested with no-data=true in the my.ini file on Windows and running mysqldump now dumps no data.
my.ini
[client]
no-data=true
backup: # mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql

Error in Mysqldump command

I have a data base named "mig". it has 10 tables. now i want to create a same database in another system so I am using mysqldump command but it shows error.
I entered command as follows :
mysqldump -u root -p root mig >file.sql;
This is the error i got :
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'mysql
dump -u root -p root mig >file.sql' at line 1
I am getting the same error when I use ,
mysqldump -u root -proot mig >file.sql;
How can i fix this ?
Simply try-
mysqldump -u root mig> file.sql
Edit
mysqldump is not a MySQL command, it is a command line utility. You must call it from your shell command line. I hope you are not calling this from MySQL prompt.
When providing password on the command line you should leave no space after -p.
It should look smth like:
mysqldump -u root -proot mig >file.sql;
You can use some tools like MySQL Workbench or SQLyog to import the dump file.
Free version: https://code.google.com/p/sqlyog/wiki/Downloads
When you execute mysqldump from command line, you must have mysql_home/bin directory in your classpath variable or command-line must be pointing to it.
try using
mysqldump -u root -proot mig >(abs_path)/file.sql;
This works for me on my local. Open Terminal and execute the following code (Make sure your are NOT on the MySQL prompt):
mysqldump -uroot -p mig > file.sql
It will ask you to input the password on the next line, for security the password won't be shown.
If you get Access Denied, means the mysql credentials are wrong (or the user you use don't have the right permissions to generate a dump), so make sure you have a valid username and password. I hope it helps.
mysqldump will not run from mysql cli, you will have to run it from windows command prompt:
mysqldump -u username -p database_name > output_file_name.sql;
If you are getting error on running above command 'mysqldump is not recognized as an internal or external command' then navigate to < MySQL Installation Directory/bin/ > and then run the command.
i have the same problem, my situation was i connect from client in local computer to server in SQL instance of Google. Since i read Sahil Mittal said this is comman utilty, i just put in terminal the same command adding -h parameter.
mysqldump -h ip.del.host -u root -p database_name > database_desired_name.sql

pg_dump: [archiver (db)] connection to database "mydb" failed: FATAL: database "mydb" does not exist

ok, following these instructions I've runned:
$ PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
in the same folder where the database is (app_db.sql) but I keep getting:
pg_dump: [archiver (db)] connection to database "mydb" failed: FATAL: database "mydb" does not exist
Why is this happening and what can I do solve it?
Thanks
You have critically misunderstood how PostgreSQL works.
app_db.sql is not a database, it is a database dump. A sequence of text commands that describe the data in the database that can be replayed to create the database.
It isn't like a Microsoft Access .dbx file or a SQLite3 database file, a database stored in a single file that can be manipulated directly. The only way to work with a PostgreSQL database is via a client/server connection to the PostgreSQL server, which stores the actual database contents in a system dependent location like /var/lib/pgsql which you never need to manipulate directly.
To use the database dump, you must restore it into a new empty database using the psql command, eg:
$ createdb mydb
$ psql -f app_db.sql mydb
This will probably fail with permissions errors if you try to run it exactly as written above. You will need to create yourself a user, give that user ownership of mydb, and possibly edit pg_hba.conf to allow yourself to authenticate depending on your system settings.
A more realistic example for a user with unix login name bob might be:
$ sudo -u postgres createuser bob
$ sudo -u postgres createdb -O bob mydb
$ psql -f dpp_db.sql -1 -v ON_ERROR_STOP=1 mydb
I strongly recommend that you read the PostgreSQL tutorial and the user manual.