Difference between revisions of "Periodic backup of database"

From Notes_Wiki
(Created page with "=Periodic backup of database= Periodic backup of database can be taken by using scripts like: <pre> #!/bin/sh BACKUP_DIR=<backup_dir>/$(date +%Y-%m-%d) mkdir -p $BACKUP_DIR m...")
 
m
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Periodic backup of database=
[[Main_Page|Home]] > [[Shell scripting]] > [[Useful bash shell scripts]] > [[Periodic backup of database]]
 
'''Use [[rsnapshot]] and [[Backing up and restoring MySQL database]] instead of below'''


Periodic backup of database can be taken by using scripts like:
Periodic backup of database can be taken by using scripts like:
Line 10: Line 12:
exit 0
exit 0
</pre>
</pre>
Replace: &lt;backup_dir&gt; appropriately.
Replace: &lt;backup_dir&gt;, &lt;user-name&gt;, &lt;password&gt; and &lt;database-name&gt; appropriately.


This script can be put in '<tt>/etc/cron.hourly</tt>' or '<tt>/etc/cron.daily</tt>' or '<tt>/etc/cron.weekly</tt>' or '<tt>/etc/cron.monthly</tt>' based on frequency of backups desired.  
This script can be put in '<tt>/etc/cron.hourly</tt>' or '<tt>/etc/cron.daily</tt>' or '<tt>/etc/cron.weekly</tt>' or '<tt>/etc/cron.monthly</tt>' based on frequency of backups desired.  
Line 16: Line 18:
*Make sure to do 'chmod +x' on script so that it is executable.
*Make sure to do 'chmod +x' on script so that it is executable.
*If you do not want to be bothered by cron output of above script then add '<tt>&gt;/dev/null 2&gt;&amp;1</tt>' at end of all commands
*If you do not want to be bothered by cron output of above script then add '<tt>&gt;/dev/null 2&gt;&amp;1</tt>' at end of all commands
[[Main_Page|Home]] > [[Shell scripting]] > [[Useful bash shell scripts]] > [[Periodic backup of database]]

Latest revision as of 13:40, 7 April 2022

Home > Shell scripting > Useful bash shell scripts > Periodic backup of database

Use rsnapshot and Backing up and restoring MySQL database instead of below

Periodic backup of database can be taken by using scripts like:

#!/bin/sh
BACKUP_DIR=<backup_dir>/$(date +%Y-%m-%d)
mkdir -p $BACKUP_DIR
mysqldump -u <user-name> -p<password> <database-name> > $BACKUP_DIR/`date +%Y-%m-%d\:%H-%M-%S`.sql

exit 0

Replace: <backup_dir>, <user-name>, <password> and <database-name> appropriately.

This script can be put in '/etc/cron.hourly' or '/etc/cron.daily' or '/etc/cron.weekly' or '/etc/cron.monthly' based on frequency of backups desired.

  • Make sure to do 'chmod +x' on script so that it is executable.
  • If you do not want to be bothered by cron output of above script then add '>/dev/null 2>&1' at end of all commands



Home > Shell scripting > Useful bash shell scripts > Periodic backup of database