Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > Programming Articles
Automated MySQL Datestamp Backup using CRON via shell
Posts: n/a

 

Show Printable Version Email this Page Subscription
01-16-2006, 10:00 PM

To automatically make a backup of your database using *nix cron:

Requires: Shell access, ability to run bash and add (hah) scripts to cron.xxx.

Code:
#! /bin/bash
# Automated database datestamp backup

mysqldump --opt -Q -u dbusername -pPassword dbname > /path/to/backups/`date --iso-8601`.sql
Open it up, replace:
  • dbusername
  • Password
  • dbname

With the proper information. Note that there is no space between -pPassword, it's intentional.

Then replace "/path/to/backups/" with the actual path that you want to put them in. Make sure the directory exists.

Put the backup.sh file in the appropriate cron folder. I'm running mine weekly, so mine is in /etc/cron.weekly/. CHMOD it +x (chmod +x backup.sh)

Output of it is a database file, named (the date).sql.

Putting it in cron.weekly will run it every Sunday night at Midnight, and give you a file that looks like this:

Code:
[cron.weekly]# sh backup.sh
[cron.weekly]# ls /home/backups/
2006-01-13.sql
[cron.weekly]#
Note: If you don't have access to /etc on your webserver, but have a crontab for your account at your host, you should use the absolute directory path and save it to a location within your host.

An example would be: /var/www/vhosts/yourdomain.tld/httpdocs/backups

Ideally this would work well in conjunction with a script on a local box (assuming your site is hosted remotely) that could shell in and download the backups automatically as well. I'll try and update this with exact instructions on how to do that if I can.

Information on automating the SSH transfer process in general can be found here and is pretty thorough, but I haven't tested it yet.

Combining multiple backups into one single cron script:

If your other databases are all accessible from the same shell prompt and user, you can do this one of two ways. If whatever user you're using for cron has permission to run mysqldump, you can:

Put them all in different folders with the same name, like so. Make sure the target folder exists, it might bark at you if it doesn't. Basically just run the dump command however many times you need to run it, with the respective names/passwords on each line.

I put in a sleep 5 just to give a small pause between operations. All it does is tell the OS to pause for 5 seconds before running the next command. It's probably not necessary, but MySQL might become unhappy if you run one command directly after another - it just gives your CPU/Memory a chance to cycle if necessary.

Code:
#! /bin/bash
# Automated database datestamp backup

mysqldump --opt -Q -u dbusername1 -pPassword1 dbname1 > /path/to/backups/1/`date --iso-8601`.sql 
sleep 5
mysqldump --opt -Q -u dbusername2 -pPassword2 dbname2 > /path/to/backups/2/`date --iso-8601`.sql 
sleep 5
mysqldump --opt -Q -u dbusername2 -pPassword3 dbname3 > /path/to/backups/3/`date --iso-8601`.sql
Or, you can put them all in the same directory with tags in front of each to name them, like this. I'm just putting databasename_ in front of each as an example, you can put whatever you want in front of the `date text.

Code:
#! /bin/bash
# Automated database datestamp backup

mysqldump --opt -Q -u dbusername1 -pPassword1 dbname1 > /path/to/backups/vbulletin_`date --iso-8601`.sql 
sleep 5
mysqldump --opt -Q -u dbusername2 -pPassword2 dbname2 > /path/to/backups/wiki_`date --iso-8601`.sql 
sleep 5
mysqldump --opt -Q -u dbusername2 -pPassword3 dbname3 > /path/to/backups/photopost_`date --iso-8601`.sql
So if you're only allowed one cron job, just stack as many commands into it as you need.
Reply With Quote
  #2  
Old 03-12-2006, 12:36 AM
Blackhat's Avatar
Blackhat Blackhat is offline
 
Join Date: Mar 2005
Posts: 323
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I get this error message when running it

/bin/sh: /home/xxxxxx/backup.sh: /bin/bash
: bad interpreter: No such file or directory

and the path to bash is "bin/bash"


Thanks
Reply With Quote
  #3  
Old 03-18-2006, 08:01 PM
Carnage Carnage is offline
 
Join Date: Jan 2005
Location: uk
Posts: 760
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

you know, what i've never seen is an intelligant backup script. Backups are a time consuming process for the server and can easilyl bog things down esp if you have huge forums... Why has no-one written a backup script that takes this into account.

Two optimisations that i can think of straight away:

First you dont NEED to backup everything. There are tables like forums, options, plugins and probably alot of others that only change very infrequently.

Post and thread tables only really need to backup new/changed data.

Has anyone got a good script for doing something like the above?
Reply With Quote
  #4  
Old 05-29-2006, 02:45 PM
Guest210212002
Guest
 
Posts: n/a
Default

FWIW, My DB is around 500MB (200k posts, attachments in the FS) and the whole process takes about 30 seconds. The reason I do it this way is that if my server dies, I don't want to have to repair some tables form a corrupt DB and search for the stable ones - I want to just dump the whole thing into a new DB in one line and be back up and running. I use my datestamp filesystem script as well, and then just tar them both up into a big tarball that I use yet another script to grab with wget from my home server nightly.

I can see where this would be an issue for really big boards, but again that's why I posted it as a How-To and didn't make it into a plugin/hack.
Reply With Quote
  #5  
Old 05-30-2006, 02:52 PM
Guest210212002
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by Blackhat
I get this error message when running it

/bin/sh: /home/xxxxxx/backup.sh: /bin/bash
: bad interpreter: No such file or directory

and the path to bash is "bin/bash"


Thanks
You shouldn't need to type /bin/sh, it should be in your path. Try just running: sh backup.sh from the directory the script is in.
Reply With Quote
  #6  
Old 08-05-2006, 12:24 AM
armstrong armstrong is offline
 
Join Date: Aug 2005
Location: Philippines
Posts: 48
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm new to this; pardon if this question is stupid:

What happens if there's activity in the forum during the backup? Won't that corrupt the backup? Won't it be necessary to temporarily close the forum while backup is in progress?
Reply With Quote
  #7  
Old 10-21-2006, 01:28 AM
Guest210212002
Guest
 
Posts: n/a
Default

It won't corrupt the backup, and you don't need to close your forums.

(Pardon the late reply)
Reply With Quote
  #8  
Old 02-23-2007, 03:04 PM
fly fly is offline
 
Join Date: Oct 2003
Posts: 1,215
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I found this script to be very helpful when backing up the db. My apologies, I don't mean to piss on your wheaties.

http://www.debianhelp.co.uk/mysqlscript.htm
Reply With Quote
  #9  
Old 05-05-2007, 09:26 AM
Guest210212002
Guest
 
Posts: n/a
Default

No apology needed, just passing along the handy things I do on my own server.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:48 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04390 seconds
  • Memory Usage 2,262KB
  • Queries Executed 22 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (4)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (9)post_thanks_postbit_info
  • (8)postbit
  • (4)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • postbit_imicons
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete