Configure MySQL to Periodically Dump all Data - mysql

I would like to configure MySQL to periodically dump all data to a .sql file so that if something happens to my server and I need to start over, I can use the file to put all of the data back in place. How can I do this? I can take suggestions either involving use of the MySQL Workbench user interface to configure it or using a query.

You'll want to look into mysqldump.exe. This is probably the easiest way to dump the entire database into a raw SQL file.
Then you'll want to set up a script that calls mysqldump.exe with the appropriate parameters and set up a Windows Scheduled Task or Linux chron job to call the script.

You can use mysqldump command. Assuming you want to make a backup of all databases now:
mysqldump -hMY_HOST.COM -uDB_USERNAME -pDB_PASSWORD USERNAME_DATABASENAME > MysqlDump.sql
To run periodic backups, you can setup a server cron which runs the mysqldump command after some interval (e.g. 24 hours)
mysqldump -hMY_HOST.COM -uDB_USERNAME -pDB_PASSWORD USERNAME_DATABASENAME > MysqlDump.sql
After creating the dump file. Setup another cron to copy this dump to the target server(say a local server) make this execute with same interval of above cron. You can use scp (secure copy) for this:
scp user#MY_HOST.COM:/some/path/file user2#MY_HOST2.COM:/some/path/file
NOTE: The mysqldump command can cause high server load depending on the database size (make sure you are executing them when server having minimum load).
Cron Job:
To setup a cron job, you put the above backup command in a .sh file, then you create the cron by running:
sudo crontab -e // Open your root crontab file
0 0 * * * USERNAME /path/to/script.sh // Run a cronjob at midnight every day
More on cronjobs: http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

Related

how do i automated MySQL data base restore everyday with crontab?

i have some demo website user can register and login and share post add products with some function but its for demo purpose only so daily 10 to 15 people register and test how its work
but i don't need everyone data i have new mysql.sql file in this SQL don't have any much data i want to do automate task with crone tab
every day its will delete current database and upload my mysql.sql file
how can i do this?
os:ubuntu 19.04
Initially, if your database contains stored procedure as well, just make sure your restore file contains query to delete the stored procedure as well before restoring the database.
To delete stored procedures add the following line in your mysql.sql file.
DELETE FROM mysql.proc WHERE db like '%{{database_name}}%' AND type = 'PROCEDURE'
After this you have to add a cron job which will restore your database everyday, to do this open terminal and type sudo crontab -e
Now enter 0 13 * * * mysql -u {{user_name}} -p {{password}} {{database_name}} < {{path_to_your_sql_file}} assuming you have to restore the database at 1 PM daily.
After adding the job, save the file.
Once the job is added you can check the it by typing sudo crontab -l in terminal
Since all you want to do is "reinstall" your DB on a daily basis (is that correct?). You can add to your install script on the first line:
DROP database <your databese>
# Here you re-create your DB again (your current sql script)
Let's say you call this script "reinstall.sql"; you can add to your cron table the following line (which runs the command everyday # 1am):
0 1 * * * mysql -u username -p password database_name < /path/to/your/reinstall.sql
To open the cron table you can do this:
sudo crontab -l -u [USER_WITH_RIGHTS_TO_EDIT]
Hope it helps!
MySQL keeps all DB data inside one directory. By default this directory resides within the MySQL installation and is called data. For example if your default installation is at c:/Users/prakash/mysql-8.0.17, a directory named data will be available inside it.
In principle, you will have to keep a fresh copy of this data directory (without any online user information, as it was when you built the database first time by running DDL scripts) somewhere, let us say at c:/Users/prakash/mysql-8.0.17/fresh. You can then write a crone job to achieve following and schedule it at any convenient time.
Shutdown the database
Delete data directory (recursively)
Copy fresh directory (recursively) where data directory resides
Rename the copied directory to data
Restart the database

Prevent verbose output undumping from mysqldump

I've dumped a table on a remote server from one database (MySQL 5.5) to a file. It took the server about 2 seconds to perform the operation. Now I'm trying to undump data from the file into another DB (same version) on the server.
The server outputs the data being processed on the screen in spite of the fact I didn't specify --verbose parameter. How can I prevent the output?
It takes the server some 10 minutes to perform the operation. Is that time acceptable or can I make it much faster? If yes, how can I do this?
Loading (undumping) is via the mysql commandline tool:
mysql -u user -p dbname < mydump.sql

How to backup mysql database every 10 minutes

I want to backup MySQL database every 10 minutes. how i can do it. I don't know how to use procedure or function for it.
I have used
mysqldump -u root -p mydatabase > mydb_backup.sql
I also want to add date and time in end of backup database name. I should only keep latest 3 backup database in system and destroy other database.
How about a backup every second? Well, actually it is "continually". It is called "Replication".
You build another mysql server (machine) as the Slave.
Then copy the data to the Slave, and do CHANGE MASTER on the Slave to have it continually replicate from the Master (which is your current instance of mysql).
AutoMySQLBackup has some great features to:
backup a single database, multiple databases, or all the databases on the server;
each database is saved in a separate file that can be compressed (with gzip or bzip2);
it will rotate the backups and not keep them filling your hard drive (as normal in the daily backup you will have only the last 7 days of backups, the weekly if enabled will have one for each week, etc.).
or you can find more info here 10 Ways to Automatically & Manually Backup MySQL Database
If you are working in unix or linux you can use crontab for scheduling.
To add the the time and date to the backup file you can use a syntax similar to the following
mysqldump -u root -p mydatabase > mydb_backup_`date+"%Y%m%d%H%M%S"`.sql

How to schedule a MySQL database backup in a remote Ubuntu server to a Dropbox folder in Windows PC?

I have a MySQL database that I want to daily backup to my Dropbox folder on my Windows PC.
How can I do that automatically from Windows 7?
One of the simplest ways to backup a mysql database is by creating a dump file. And that is what mysqldump is for. Please read the documentation for mysqldump.
In its simplest syntax, you can create a dump with the following command:
mysqldump [connection parameters] database_name > dump_file.sql
where the [connection parameters] are those you need to connect your local client to the MySQL server where the database resides.
mysqldump will create a dump file: a plain text file which contains the SQL instructions needed to create and populate the tables of a database. The > character will redirect the output of mysqldump to a file (in this example, dump_file.sql). You can, of course, compress this file to make it more easy to handle.
You can move that file wherever you want.
To restore a dump file:
Create an empty database (let's say restore) in the destination server
Load the dump:
mysql [connection parameters] restore < dump_file.sql
There are, of course, some other "switches" you can use with mysqldump. I frequently use these:
-d: this wil tell mysqldump to create an "empty" backup: the tables and views will be exported, but without data (useful if all you want is a database "template")
-R: include the stored routines (procedures and functions) in the dump file
--delayed-insert: uses insert delayed instead of insert for populating tables
--disable-keys: Encloses the insert statements for each table between alter table ... disable keys and alter table ... enable keys; this can make inserts faster
You can include the mysqldump command and any other compression and copy / move command in a batch file.
My solution to extract a backup and push it onto Dropbox is as below.
A sample of Ubuntu batch file can be downloaded here.
In brief
Prepare a batch script backup.sh
Run backup.sh to create a backup version e.g. backup.sql
Copy backup.sql to Dropbox folder
Schedule Ubuntu/Windows task to run backup.sh task e.g. every day at night
Detail steps
All about backing up and restoring an MySQL database can be found here.
Back up to compressed file
mysqldump -u [uname] -p [dbname] | gzip -9 > [backupfile.sql.gz]
How to remote from Windows to execute the 'backup' command can be found here.
plink.exe -ssh -pw -i "Path\to\private-key\key.ppk" -noagent username#server-ip
How to bring the file to Dropbox can be found here
Create a app
https://www2.dropbox.com/developers/apps
Add an app and choose Dropbox API App. Note the created app key and app secret
Install Dropbox API in Ubuntu; use app key and app secret above
$ wget https://raw.github.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh
$ chmod +x dropbox_uploader.sh
Follow the instruction to authorize access for the app e.g.
http://www2.dropbox.com/1/oauth/authorize?oauth_token=XXXXXXX
Test the app if it is working right - should be ok
$ ./dropbox_uploader.sh info
The app is created and a folder associating with it is YourDropbox\Apps\<app name>
Commands to use
List files
$ ./dropbox_uploader.sh list
Upload file
$ ./dropbox_uploader.sh upload <filename> <dropbox location>
e.g.
$ ./dropbox_uploader.sh upload backup.sql .
This will store file backup.sql to YourDropbox\Apps\<app name>\backup.sql
Done
How to schedule a Ubuntu can be view here using crontab
Call command
sudo crontab -e
Insert a line to run backup.sh script everyday as below
0 0 * * * /home/userName/pathTo/backup.sh
Explaination:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command
Or simply we can use
#daily /home/userName/pathTo/backup.sh
Note:
To mornitor crontab tasks, here is a very good guide.

How to do automatic mysql db backup using mysql workbench

How to do automatic mysql db backup using mysql workbench?
Is it possible?
Mysql bench is not required for automatic backup,
setup a crontab and use mysqldump (or equivalent) will do the same job and is easier.
example:
crontab -e
/* backup every day at 00:00:00 */
0 0 * * * mysqldump -u root -ppassword YOUR_DATABASE > /backup/YOUR_DATABASE.sql
If you have a webserver with PHP I'd suggest MySqlDumper
It supports:
Automatic backups
Emails backups
Compresses backups
Rotates backups
etc.
ATM scheduled backups are not plannned in Workbench:
http://bugs.mysql.com/bug.php?id=50074
At the same time Workbench is designed to have powerful scripting shell, so you can try to
investigate what commands can be used for backup and call them directly from shell.
(But I agree this is too general idea):
http://dev.mysql.com/doc/workbench/en/wb-extending.html