Linux Shell Script to backup MySQL database to remote server

Database is the most important file other than web file, hence the backup is very important for the web developer and web owner.

By referring to the current post, you may backup your database automatically from your Linux box.

###START###

#!/bin/bash

# Specify the temporary backup directory
BKUPDIR=”/home/mickgenie/tmp”

# Database Name
dbname=”dbname_here”
dbuser=”user_here”
dbpasswd=”password_here”

# store the current date
date=`date ‘+%Y-%m-%d’`

# Specify Ftp details
ftpserver=”FtpServerIP”
ftpuser=”username”
ftppass=”password”

# Dump the mysql database with the current date and compress it.
/usr/bin/mysqldump -u$dbuser -p$dbpasswd $dbname | gzip > $BKUPDIR/$date.$dbname.sql.gz

# Change directory to the backup directory
cd $BKUPDIR

# Upload the backup
ftp -n $ftpserver <<!EOF!
user $ftpuser $ftppass
binary
prompt
mput *.sql.gz
quit
!EOF!

# Remove the local backup file
/bin/rm -f /$BKUPDIR/$date.$dbname.sql.gz

###END###

With the above script, you may save it as file.sh and just schedule a cronjob to run it as your needed time.

cPanel: How to monitor crond services using chkservd

When you are using cPanel cron services, you might found out the cPanel services down without any clue.

With this article, Mick Genie will guide you how to check your crond services by using cPanel chkservd services.

cPanel offer chkservd services to automatically monitor daemon services from the Linux server.

To done this, follow the step as below.
1. Open the file /etc/chkserv.d/crond, pico /etc/chkserv.d/crond
2. Add the command as below to the file.
service[crond]=x,x,x,/etc/init.d/crond restart,crond,root
3. Open the file /etc/chkserv.d/chkservd.conf, pico /etc/chkserv.d/chkservd.conf
4. Add command below to last line of the above file.
crond:1
5. Restart chkservd services, /scripts/restartsrv chkservd

Scheduled MySQL database backup

Assume that you are using crontab to schedule your task which you are using Unix system.

To schedule a database backup, it will be very easy as step below.

1. Log into your SSH.
2. Type “crontab -e” without quote.
3. Enter the command below.

0 0 * * * date=`date -I` ; mysqldump -a -u[user] -p[password]  [dbname] | gzip > /path/folder/[dbname]_$date.sql.gz

where you shall replace the user, password and dbname to your database information.

With the command as above it will actually backup your database by 12.00AM daily and place to the desired path and name with date.