Ok, i have searched high and low and have been unable to find an answer that works for what i am trying to do. I have two databases, we'll call them DB1 and DB2. I have a cron job that runs every night at 4am that backs up DB1 and stores its data in a SQL file archive, we'll call this file db_backup.sql. The file is stored in a folder on the server, we'll call it ROOT/backups/db_backup.sql.
Info
database names: DB1 and DB2
backup filename: db_backup.sql
backup file path: ROOT/backups/db_backup.sql
What i'm trying to do:
I want to use the db_backup.sql file to build DB2. I am basically trying to set up database replication where i replicate DB1 out to DB2. Don't know of any other way to do this on shared hosting servers than what i'm trying to explain. I am trying to use php to import the db_backup.sql file into DB2.
My Environment:
The website and databases are on a shared hosting account with godaddy (yes, i would love to get dedicated servers to set up real replication, but can't afford it for now). The databases are mysql in phpmyadmin.
Is this something that is possible? Any help would be greatly appreciated! Let me know if you have any questions as well.
I'm not sure if I understand your problem. You need to copy the db_backup file to the second host where you have access to the database and load the sql file.
From a shell:
mysql -hhost -uusername -ppassword databasename < db_backup.sql
This will restore the tables on the second machine.
Should be as simple as, setting up a CRON job on server 2 to call a script on Server 1 which dishes out the SQL file, then that cron job script would import / rebuild the DB.
Make sure to require a hash (via get or post) before giving out the DB SQL on server 1, or else anyone could read it / have your database dump.
Can you not avoid PHP altogether and connect directly to the database remotely to perform the backup either via the command line or using a scripting language more suitable for long running processes?
Related
We are developing a web-base application using PHP and Mysql. As you know there are limitations for backing up huge DBs using the web interface, so I need a solution to back up our huge DB by the following conditions:
The process can be done without web interface and must be run in command line (e.g. as a Linux service)
The process should use minimal usage of server resources (in the other word, while backup process is running, clients can use services on server)
Some features are vital (like backup, restore, compression & split backup file) and some features are optional (e.g. Mailing and FTP , ...)
Script must be writing in PHP or Perl.
Is there any ideas?
Look at mysqldump for command line backup
For complete database backup:
mysqldump -u USER_NAME -p DB_NAME > DB_NAME.sql
If your db is Huge and you don't want to take a backup of complete database. You can take backup of only single / multiple tables
mysqldump -u USER_NAME -p DB_NAME TABLE_NAME > TABLE_NAME.sql
I'd avoid scripting it at all and just use the tools that MySQL has inbuilt. Here's a nice tutorial on mysqldump for you:
http://www.thegeekstuff.com/2008/09/backup-and-restore-mysql-database-using-mysqldump/
you maybe want to consider seting up a replication to a 2nd server, and you have a live backup, if you want to store old backups you can use up the 2nd servers resources to make thous
use nice to run the php script, with value 19 it will make the process have very low priority
nice -n 19 php /path/to/backupscript.php
I am trying to make a complete file & mySQL backup of my site each night.
I was thinking the best way of doing it would be to have a cronjob run each night that would login to a remote server and replicate all of the local files.
Then, I need to figure out a way to take backups of all the mysql databases (currently there are three) and upload them all to the remote server as well.
This sounds like a huge project and I don't know whether to reinvent the wheel here, or if there is some script out there which basically does the same thing already.
Use cronjob to run a bash script
mysqldump the databases
tar -cvf the files
wput it all to your remote server
Also you can set a variable like now=$(date +"%Y_%m_%d") to use in your file names
You can use the mysqldump command to backup the database to a file and then upload to a different server
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
have you thought about MySQL replication? Maybe that fits better to your needs and you doesn't need php to do it
http://dev.mysql.com/doc/refman/5.5/en/replication.html
I have develop one web site in php.
I know how to upload it using ftp(filezilla).
But i dont know how to upload database and i can't do any thing without uploading this database.
please help me to upload my database.
Thank you.
tl;dr version
Ask your hosting provider what methods they support.
More detail
It all depends on your hosting. Do you have a simple hosting account with only FTP access (i.e. no command line or remote desktop access)? In these cases your host usually has phpMyAdmin installed or an alternative web based database management system.
It's unlikely, but you could also try connecting to your MySQL database from your computer. You can download the command line tools or simply get MySQL Workbench, and put in your hostname and test the connection.
If you do have shell access, you can dump your local database with mysqldump and upload the resulting SQL file, then import that on the server. A similar technique can be used with remote desktop.
Normally you could make a gzipped .tar.gz of your DB dump and upload it with a file client to your space... once done unzip it and restore from dump using a mysql commandline or some GUI provided by the site admin.. i'm assuming your DB is MySql as its the obvious choice and you have the privilege to access the DB.... this question seems more suited for serverfault as it seems to have less relation to programming in general. Anyhow
To Tar:
tar –cvzf tarfilename foldername
Untar:
tar –xvzf tarfilename
Example:
tar –cvzf abc.tar.gz abc
This will zip [tar] the abc folder with the name abc.tar.gz
tar –xvzf filename.tar.gz
This will unzip [untar] the file as abc folder.
To backup and restore from dump please refer this link as it has some brilliant illustration.
If you're using MySQL, just export the database from your localhost (phpmyadmin)(click on the database and then search for the export tab ).
When you export it, you need to import it to your online host (your domain).
And to do this, you have to have access to the cpanel of your host.
After logging in to your cpanel , search for phpmyadmin (or whatever database you're using).
Create a database and import your database.
I'm thinking about building a php script that flushes, locks and copys a mysql data folder. Because I need to lock the tables and typical dump takes 5 minutes plus, I was thinking if I do a flush, lock and file copy of the data folder, it should be quicker. Anyone any experience of this and know if this is a viable solution?
Look to a XtraBackup also if you are planning to do non-stop backups of your data.
The MySQL devs are way ahead of you. The rolled your exact method with some syntactic sugar and proper error checking in a command called mysqlhotcopy.
Might be too late now but what phase does all this break down into? If your server is spending most of the five minutes copying the files instead of the actual flushing then your problem is simply a slow disk.
I think the best answer to the question is the following Windows command:
set bin=C:\Program Files\MySQL\MySQL Server 5.6\bin
"%bin%/mysql" -e "FLUSH TABLES WITH READ LOCK; UNLOCK TABLES;" --user=UserName --password=Password DatabaseName
This quickly forces all MySQL data for a database to its MySQL three or four files in the MySQL data folder, from which they can be copied to some other folder. You'll have to customize this command for your particular version of MySQL and for your database and admin user.
I couldn't get the other answers to work, but this BAT/CMD command works fast and well in my experience.
The only other suggestion I can make is to use the MySQL Workbench (which comes with MySQL) to Stop the MySQL server. When it is stopped it is flushed to disk. Don't forget to Start your MySQL server when you are finished using the files directly (at which time MySQL reads the database files from disk).
Note: if you simply copy the data files for a database to the data folder of another instance of MySQL that is already open, you won't see the data in MySQL applications! You would think that MySQL would check the date/time modified of the disk files to detect updates, but it doesn't. And it doesn't keep its files locked, showing Windows that they are in use, which they are. So strange.
I have a fairly small MySQL database (a Textpattern install) on a server that I do not have SSH access to (I have FTP access only). I need to regularly download the live database to my local dev server on demand; i.e., I would like to either run a script and/or have a cron job running. What are some good ways of doing this?
Some points to note:
Live server is running Linux, Apache 2.2, PHP 5.2 and MySQL 4.1
Local server is running the same (so using PHP is an option), but the OS is Windows
Local server has Ruby on it (so using Ruby is a valid option)
The live MySQL db can accept remote connections from different IPs
I cannot enable replication on the remote server
Update: I've accepted BlaM's answer; it is beautifully simple. Can't believe I didn't think of that. There was one problem, though: I wanted to automate the process, but the proposed solution prompts the user for a password. Here is a slightly modified version of the mysqldump command that passes in the password:
mysqldump -u USER --password=MYPASSWORD DATABASE_TO_DUMP -h HOST > backup.sql
Since you can access your database remotely, you can use mysqldump from your windows machine to fetch the remote database. From commandline:
cd "into mysql directory"
mysqldump -u USERNAME -p -h YOUR_HOST_IP DATABASE_TO_MIRROR >c:\backup\database.sql
The program will ask you for the database password and then generate a file c:\backup\database.sql that you can run on your windows machine to insert the data.
With a small database that should be fairly fast.
Here's what I use. This dumps the database from the live server while uploads it to the local server.
mysqldump -hlive_server_addresss -ulive_server_user -plive_server_password --opt --compress live_server_db | mysql -ulocal_server_user -plocal_server_password local_server_db
You can run this from a bat file. You can ever use a scheduled task.
Is MySQL replication an option? You could even turn it on and off if you didn't want it constantly replicating.
This was a good article on replication.
I would create a (Ruby) script to do a SELECT * FROM ... on all the databases on the server and then do a DROP DATABASE ... followed by a series of new INSERTs on the local copy. You can do a SHOW DATABASES query to list the databases dynamically. Now, this assumes that the table structure doesn't change, but if you want to support table changes also you could add a SHOW CREATE TABLE ... query and a corresponding CREATE TABLE statement for each table in each database. To get a list of all the tables in a database you do a SHOW TABLES query.
Once you have the script you can set it up as a scheduled job to run as often as you need.
#Mark Biek
Is MySQL replication an option? You could even turn it on and off if you didn't want it constantly replicating.
Thanks for the suggestion, but I cannot enable replication on the server. It is a shared server with very little room for maneuver. I've updated the question to note this.
Depending on how often you need to copy down live data and how quickly you need to do it, installing phpMyAdmin on both machines might be an option. You can export and import DBs, but you'd have to do it manually. If it's a small DB (and it sounds like it is), and you don't need live data copied over too often, it might work well for what you need.