Backup MySQL via php, in a Symfony application - mysql

I'm trying to do a php script to backup my database.
Here's what I've tried so far :
$command = "mysqldump -u [username] -p [password] [databasename] | gzip > db.sql.gz";
$this->output = system($command);
How do I get the password and
username from databases.yml ?
How can I do a script that sends me
the backup file, instead of saving it
on the server (à la phpmyadmin) ?

You can create a symfony task. If you pass in an environment (i.e. dev, prod) or connection you get access to the Doctrine connection manager and create a connection. You can use that connection to make a database dump or get the connection details from the connection manager.
You can use the doctrine insert sql task as template for your task. I've done similar in the past.

Related

How to re-setup Database configurations in Icinga2 server?

I am using ubuntu 16.04 and mysql DB ,i configured DB for icinga2 server and i created separate user in database.
somedays back i deleted that user from db now i am not able access icingaweb2 UI,
Getting below error while trying to access to the link.
All configured authentication methods failed. Please check the system log or Icinga Web 2 log for more information.
I followed the below link to install icinga2 server
Please Help.
I resolved the issue by re-creatinging a setup token in in the Icinga Web 2 by using bellow command
sudo icingacli setup token create
i got some error's while creating setup token and it is easily resolved by using [1]: https://monitoring-portal.org/index.php?thread/40111-icingacli-does-not-work/
If you still have access to the MySQL DB you should do a backup first of your icinga2 DB.
mysqldump -u [username] -p [password] [databasename] > [backupfile.sql]
Then purge MySQL or follow the guide on how to reset your root password
After you have access to the DB again if you purged MySQL use this to bring your data back in.
mysqldump -u [username] -p [password] [databasename] < [backupfile.sql]
If you only lost your password to IcingaWeb2 or now have access to MySQL then create a MD5 based BSD password algorithm:
openssl passwd -1 "password"
Note: The switch to openssl passwd is the number one (-1) for using the MD5 based BSD password algorithm.
Insert the user into the database using the generated password hash:
````
INSERT INTO icingaweb_user (name, active, password_hash) VALUES ('icingaadmin', 1, 'hash from openssl');
Source

Is it possible to programmatically load an SQL dump file using RMySQL?

I'm storing a database structure in the form of a .sql dump file in my package's configuration folder (stored in inst/config).
At the moment I'm using a command line which requires password input:
cat 'filename.sql' | mysql -p databasename
In fact I'm suggesting to the user how to load the dump with this function:
loaddbstructure <- function(sqlfile = system.file("config/dump.sql",
package="mypackagename", mustWork = TRUE)){
message("This message gives 2 options to load the database structure.\n\n",
"(1) If a user called 'R' is created in MySQL, you can run this from a shell command line: \n",
sprintf("$ cat '%s' | mysql -u R -p tradeflows", sqlfile),
"\n\n",
"(2) Another option is to call this from a mysql client:\n",
"mysql> connect tradeflows;\n",
sprintf("mysql> source %s;", sqlfile))
}
Is it possible to programmatically load this .sql file with a command from RMySQL, using a database connection created with the credentials provided in ~/.my.cnf?
Add connection credentials to the mysql option file located in ~/.my.cnf:
[client]
user = R
password = password
Then you can load the database dump with an R command that calls a system bash command (Linux specific):
system("cat 'dump.sql' | mysql databasename")

How do you use SQLCMD to login to SQL Server Express, user no password set

I installed SQL Server 2012 Express and I'm learning from a book on SQL Server administration. My task is to write a batch file to copy data from a table and export it to an Excel spreadsheet.
I've got the instance name right and my username but log in fails because of password. When I enter Management Studio and click connect I see my username but password is blank, I successfully connect to my databases this way. How do I set a password for my account? I think I can't log in using SQLCMD because I don't have this set. My username is the same as my windows username, using the password that I log on to windows with doesn't work.
SQLCMD -S CRAIG-PC\SQLExpress -U CRAIG-PC\Craig -P *********
-d AdventureWorks
-Q "SELECT * FROM Sales.SalesOrderDetail" -o C:\ExportDir\export.csv
This is the command in the batch file that I'm trying to connect and query the database. If I take out -P ******* SQL Server is waiting for a password. If I don't use a username or password connection is refused.
Remote connections are enabled using tcp, but this is a local install.
Use the '-E' switch to make your connection a trusted (Window's authentication) one, then you don't need user name and password at all (provided you have sufficient rights on your machine)
http://technet.microsoft.com/en-us/library/ms162773.aspx
Note in my python code below, I don't implement the user or password.
Hope that helps
_query = "CREATE DATABASE %s ON (FILENAME = '%s'), (FILENAME = '%s') FOR ATTACH;" %(_database, mdffilepath, logfilepath)
command_process = SubP.Popen(['sqlcmd','-b', '-E',
'-S', _server,
'-d', 'master',
'-q', _query],
stdin = SubP.PIPE,
stdout = SubP.PIPE,
stderr = SubP.STDOUT,
shell = True)
_stdOut, _stdError = command_process.communicate()
Check the name of the database, ie "AdventureWorks" should be "AdventureWorks2012"
sounds like you are using windows authentication you can remove the -u and -p. when those are not provided sqlcmd will use windows authentication.

Syncing remote database to local?

I'm hoping I can use a shell script that will pull a sql dump down from my production site and into my local database. Ideally, I'd like to be able to run something like this:
sync_site example_com example_local
Where the first argument is the production database and the second is the local database. The remote database is always on the same server, behind SSH, with known MySQL credentials.
Figured it out:
ssh me#myserver.com mysqldump -u user -ppass remote_db | mysql -u user -ppass local_db

Migrating existing database to Amazon RDS

How can I import existing MySQL database into Amazon RDS?
I found this page on the AWS docs which explains how to use mysqldump and pipe it into an RDS instance.
Here's their example code (use in command line/shell/ssh):
mysqldump acme | mysql --host=hostname --user=username --password acme
where acme is the database you're migrating over, and hostname/username are those from your RDS instance.
You can connect to RDS as if it were a regular mysql server, just make sure to add your EC2 IPs to your security groups per this forum posting.
I had to include the password for the local mysqldump, so my command ended up looking more like this:
mysqldump --password=local_mysql_pass acme | mysql --host=hostname --user=username --password acme
FWIW, I just completed moving my databases over. I used this reference for mysql commands like creating users and granting permissions.
Hope this helps!
There are two ways to import data :
mysqldump : If you data size is less than 1GB, you can directly make use of mysqldump command and import your data to RDS.
mysqlimport : If your data size is more than 1GB or in any other format, you can compress the data into flat files and upload the data using sqlimport command.
I'm a big fan of the SqlYog tool. It lets you connect to your source and target databases and sync schema and/or data. I've also used SQLWave, but switched to SqlYog. Been so long since I made the switch that I can't remember exactly why I switched. Anyway, that's my two cents. I know some will object to my suggestion of Windows GUI tools for MySQL. I actually like the SqlYog product so much that I run it from Wine (works flawlessly from Wine on Ubuntu for me).
This blog might be helpful.
A quick summary of a GoSquared Engineering post:
Configuration + Booting
Select a maintenance window and backup window when the instance will be at lowest load
Choose Multi-AZ or not (highly recommended for auto-failover and maintenance)
Boot your RDS instance
Configure security groups so your apps etc can access the new instance
Data migration + preparation
Enable binlogging if you haven't already
Run mysqldump --single-transaction --master-data=2 -C -q dbname -u username -p > backup.sql on the old instance to take a dump of the current data
Run mysql -u username -p -h RDS_endpoint DB_name < backup.sql to import the data into your RDS instance (this may take a while depending on your DB size)
In the meantime, your current production instance is still serving queries - this is where the master-data=2 and binlogging comes in
In your backup.sql file, you'll have a line at the top that looks like CHANGE MASTER TO MASTER_LOG_FILE=’mysql-bin.000003′, MASTER_LOG_POS=350789121;
Get the diff since backup.sql as an SQL file mysqlbinlog /var/log/mysql/mysql-bin.000003 --start-position=350789121 --base64-output=NEVER > output.sql
Run those queries on your RDS instance to update it cat output.sql | mysql -h RDS_endpoint -u username -p DB_name
Get the new log position by finding end_log_pos at the end of the latest output.sql file.
Get the diff since the last output.sql (like step 6) and repeat steps 7 + 8.
The actual migration
Have all your apps ready to deploy quickly with the new RDS instance
Get the latest end_log_pos from output.sql
Run FLUSH TABLES WITH READ LOCK; on the old instance to stop all writes
Start deploying your apps with the new RDS instance
Run steps 6-8 from above to update the RDS instance with the last queries to the old server
Conclusion
Using this method, you'll have a small amount of time (depending on how long it takes to deploy your apps + how many writes your MySQL instance serves - probably only a minute or two) with writes being rejected from your old server, but you will have a consistent migration with no read downtime.
A full and detailed post explaining how we (GoSquared) migrated to RDS with minimal downtime (including error debugging) is available here: https://engineering.gosquared.com/migrating-mysql-to-amazon-rds.
I am completely agree with #SanketDangi.
There are two ways of doing this one way is as suggested using either mysqldump or mysqlimport.
I have seen cases where it creates problem while restoring data on cloud gets corrupt.
However importing applications on cloud has became much easier now a days. You try uploading your DB server on to public cloud through ravello.
You can import your database server itself on Amazon using ravello.
Disclosure: I work for ravello.
Simplest example:
# export local db to sql file:
mysqldump -uroot -p —-databases qwe_db > qwe_db.sql
# Now you can edit qwe_db.sql file and change db name at top if you want
# import sql file to AWS RDS:
mysql --host=proddb.cfrnxxxxxxx.eu-central-1.rds.amazonaws.com --port=3306 --user=someuser -p qwe_db < qwe_db.sql
AWS RDS Customer data Import guide for Mysql is available here : http://aws.amazon.com/articles/2933
Create flat files containing the data to be loaded
Stop any applications accessing the target DB Instance
Create a DB Snapshot
Disable Amazon RDS automated backups
Load the data using mysqlimport
Enable automated backups again
If you are using the terminal this is what worked for me:
mysqldump -u local_username -plocal_password local_db_name | mysql -h myRDS-at-amazon.rds.amazonaws.com -u rds-username -prds_password_xxxxx remote_db_name
and then i used MYSQL WorkBench (free download) to check it was working because the command line was static after pressing submit, i could have probably put -v at end to see it's output
Note: there is no space after -p
Here are the steps which i have done and had sucess.
Take the MySQLdump of the needed database.
mysqldump -u username -p databasename --single-transaction --quick --lock-tables=false >databasename-backup-$(date +%F).sql
( Dont forget to replace the username as root – most of the times, and databasename -> Db name of database which you are going to migrate to RDS )
Once prompted, enter your password.
Once done, login to the RDS Instance from your MySQL server ( Make sure the security groups are configured to allow the connection from Ec2 to RDS )
mysql -h hostaddress -P 3306 -u rdsusername -p
( Dont forget to replace hostaddress with the address of your RDS Instance and rdsusernmae with username for your RDS Instance, when prompted give the password too )
You find that hostaddress under – Connectivity & security -> Endpoint & port under RDS Database From AWS Console.
Once logged in, create the database using MySQL commands :
create database databasename;
\q
Once Database is created in RDS, Import the SQL file created in Step 1 :
mysql -h hostaddress -u rdsusername -p databasename < backupfile.sql
This should import the SQL file to RDS and restore the contents into the new database.
Reference from : https://k9webops.com/blog/migrate-an-existing-database-on-mysql-mariadb-to-an-already-running-rds-instance-on-the-aws/