Bash script and cron (Cgi + Perl + Cron)


Bash Shell Script (mysqlbackup)

mysqldump -uroot -ppwd –opt db1 > /sqldata/db1.sql
mysqldump -uroot -ppwd –opt db2 > /sqldata/db2.sql

cd /sqldata/
tar -zcvf sqldata.tgz *.sql
cd /scripts/
perl emailsql.cgi

The first mysqldump statement has 4 parameters passed to it :-

* -u = your MySQL username. (substitute root with your username)
* -p = your MySQL password. (substitute pwd with your password)
* –opt adds the most common and useful command line options, resulting in the quickest possible export. This option automatically includes –add-drop-table, –add-locks, –extended-insert, –quick and –use-locks.
* the database name to extract. (substitute db1 with your database name)
* the > /sqldata/db1.sql redirects all the output to a file called db1.sql in a directory /sqldata/.

Perl script (emailsql.cgi)

After creating the script, you need to make it executable by CHMODing the file permissions to 700.

#!/usr/bin/perl -w

use MIME::Lite;

$msg = MIME::Lite->new(

From =>,
To =>,
Subject => sqldata.tgz MySQL backup!,
Type => text/plain,
Data => “Hi You,\n MySQL database backups.”);

Path =>”/sqldata/sqldata.tgz”,
Filename =>”sqldata.tgz”);


Adding the Script to Cron

0 2 * * * /myscripts/mysqlbackup
0 5 * * 0 /myscripts/reindex

There are five fields for setting the date and time separated by a space, followed by the name of the script you wish to run. The five time settings are in the following order.

* Minutes – in the range of 0 – 59
* Hour – in the range of 0 – 23
* Day of month – in the range 1 – 31
* Month – in the range 1 -12
* Day of week – in the range 0 – 6 (0 = Sunday)

Any field with a * means run every possible match, so for example a * in the day of month field will run the script every single day of the month at the specified time. (…)

The above example shows my current crontab. The file has two entries, one for each script I wish to run. The first entry tells cron to run the mysqlbackup script every morning at 2am. The second entry runs my search engine indexer every Sunday morning at 5am.”

How to backup your MySQL tables and data every night using a bash script and cron CGI interactive

Cron tab + mod perl

# every monday rebuild all, including pdf
30 03 * * 1 /usr/local/modperl-docs/bin/site_build_force_pdf_index
# update all (only changes/no pdf) every 6 hours
15 6,12,18 * * * /usr/local/modperl-docs/bin/site_build_index
# update all (only changes and pdfs) once a day
15 0 * * * /usr/local/modperl-docs/bin/site_build_pdf_index

The mod_perl documentation


Tutorial sur l’utilisation de Mysql avec Perl


[UNIX] Crontab


Internals: Replacing Task Scheduler with Cron


~ by netlex on October 18, 2006.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: