How to take mysql dump of the mysql db every hour - mysql

hey I am newbie to scripting in linux.I want to take a sqldump of my database every hour, I have gone thorough couple of blogs i was able to write a script which will take the dump of my database but I do I make it run every hour in the crontab.
Kindly help me out.

Set up a crontab entry like this:
0 * * * * /usr/bin/mysqldump --user=sqluser --password=sqlpass -A > /tmp/database.sql
This will run the command /usr/bin/mysqldump --user=sqluser --password=sqlpass -A > /tmp/database.sql on the hour, every hour. This will dump all database schemas into the file /tmp/database.sql (adjust as required for your setup) using the username sqluser and the password sqlpass (again, adjust for your setup)
For more information about crontab syntax, you can refer to this page

Related

Backup mysql database daily based on day name

I'm using ubuntu 16.04 with nginx installed, currently i run daily backup using cron like this:
#crontab -u root -e
0 2 * * * mysqldump -u username -p"password" production | gzip -c > production.gz
this will backup my database everydate at 2am, the problem here is i need to backup database based on day name, so the backup database name will be suited based on dayname, for example the file name will look like this:
production_monday.gz
production_tuesday.gz
production_wednesday.gz
production_thursday.gz
production_friday.gz
production_saturday.gz
production_sunday.gz
how can i set the cron to produce the file like above ? cron schedule will auto rewrite the file based on the day name
My suggestion would be to create a shell script that finds the current day of the week (date +%A), then write the mysqldump output to a file formatted as "prefix"_"dayofweek" for you to zip. Then from cron, just execute this shell script rather than the mysqldump directly.
You may also find this answer helpful.

MySQL DataBase automatic reset after 1 hour for Demo Mode

I wish to create a demo mode for my CMS and is there anyway with which I can set my MySQL database automatically reset after 1 hour of time.
If you have access to the hosting console, you need a database running with the initial data set. Only once, you need to get a database dump in a file:
mysqldump -u DBUSER -pDBPASS --opt DBNAME > /path/to/my/backup.sql
Then, create a cron job (run crontab -e) to run a database restore using your dump file (see http://www.adminschoice.com/crontab-quick-reference for more information about cron tabs)
mysql -u DBUSER -pDBPASS DBNAME < /path/to/my/backup.sql
For example:
# crontab -e
00 * * * * mysql -u root -p123456 demo < /path/to/my/demo_backup.sql
This will restore your database to the original state every hour (minute 00)
Note: You need to consider, also, that if some user is actually trying your demo and the database is running the reset process, the user data will be lost in the middle of the session.

Linux Backup MySQL failed when assign to crontab

Hi fellow StackOverFlow,
Today , I have a very weird scenario in my Unix machine.
I'm currently using this command manually to save my current database backup.
/usr/bin/mysqldump -u root -p'thetechnofreak' admin_test > /mnt/databasesql/admin$(date +%Y-%m-%d-%H.%M.%S).sql.gz
In my crontab , I access it using
crontab -e
Then I add the following to the list
30 2 * * * /usr/bin/mysqldump -u root -p'thetechnofreak' admin_test > /mnt/databasesql/admin$(date +%Y-%m-%d-%H.%M.%S).sql.gz
I realize that it's not doing it automatically, is there anything that i've missed?
Is there a flagging option or a logging method to know whether the backup is done successfully or not.
Thanks in advance.
Try this with the escaping of a \ prior to the %
30 2 * * * /usr/bin/mysqldump -u root -p'thetechnofreak' admin_test > /mnt/databasesql/admin$(date +\%Y-\%m-\%d-\%H.\%M.\%S).sql.gz
See Using the % Character in Crontab Entries by M. Ducea

Cron job not running to backup mysql database

I am trying to backup my mysql database using linux crontab like below command,
$crontab -e
15 * * * * /usr/bin/mysqldump -u XXX-pXXX demobackupDB > /home/user/DBbackup/$(date +%F)_backup.sql
But i dont find that .sql file in the specific folder after every 15 mins.
How to find what is error and fix it?
You have wrong crontab. What you specified is "run task at 15 minutes after each hour".
You need this crontab to run task every 15 minutes:
*/15 * * * * /usr/bin/mysqldump -u XXX-pXXX demobackupDB > /home/user/DBbackup/$(date +%F)_backup.sql
If your job is still not running there could be many reasons of this.
You can start debugging with the simple test like this:
* * * * * echo hi >> /home/user/test
(You should see a new line "hi" appended to /home/user/test file once a minute)
If this is not the case see probable reasons for ubuntu here: https://askubuntu.com/q/23009.
Also see cron log: https://askubuntu.com/q/56683/103599
You can create a file with your statement and make it executable
echo '/usr/bin/mysqldump -u XXX-pXXX demobackupDB > /home/user/DBbackup/$(date +%F)_backup.sql' > backupjob
Make them executable
chmod +x backupjob
And run it
sudo ./backupjob
If everything is fine, the mysql-backup-file should be created. If not, go on with debugging your statement else change your cronjob entry:
cronjob -e
# My MySQL-Backup-Job
*/15 * * * * /yourpath/backup
...
Important: The Crontab-Table must end with an empty line!
For further Informations: Ubuntu - Cron

How to clean the MySQL database on a daily basis

So, I have some kind of records in my database that should be deleted after 24 hours. How do I make my MySQL database to update itself? Is this even possible? Thanks
You can write cron jobs for that.
In linux you can use crontab for that documentation
On Windows you can set cron jobs like this
0 0 * * * mysql -u USER -pPASSWORD -h HOST DATABASE < path/to/sql/script
look into man crontab