1. prepare the shell script vi/home/zhangy/database_bak.sh #! /Bin/sh # File:/home/zhangy/database_bak.sh # DatabaseinfobakupmysqlTANK2009/11/04 DB_USERroot # Username DB_PASS ******* # Password DB_NA
I. prepare shell scripts
Vi/home/zhangy/database_bak.sh
#! /Bin/sh
# File:/home/zhangy/database_bak.sh
# Database info bakupmysql TANK 2009/11/04
DB_USER = "root" # User name
DB_PASS = "*********" # Password
DB_NAME = "myblog" # Name of the data to be backed up
# Others vars
DATE = 'date + % Y _ % m _ % d' # date to be backed up
YESTERDAY = 'date-d yesterday + % Y _ % m _ % d' # Delete the backup from YESTERDAY
BIN_DIR = "/usr/local/mysql/bin"
BCK_DIR = "/home/zhangy/database_bak" # backup path
Cd $ BCK_DIR
# Delete the backup of the database in the past because I still have 2 GB hard disk space in linux, which is depressing.
If [-f $ YESTERDAY $ DB_NAME ". SQL"]
Then
Rm-f $ YESTERDAY $ DB_NAME ". SQL"
Fi
# Backup
$ {BIN_DIR}/mysqldump -- opt-u $ {DB_USER}-p $ {DB_PASS }$ {DB_NAME }>$ {BCK_DIR}/$ {DATE }$ {DB_NAME}. SQL
II. regular execution
Put shell in crontab.
# Check whether crond is started
[Root @ BlackGhost cron] # ps-e | grep crond
21519? 00:00:00 crond
# Open crontab
[Root @ BlackGhost cron] # crontab-e
# Add a line in it
00 18 ***/home/zhangy/database_bak.sh
# Check if any added
[Root @ BlackGhost cron] # crontab-l
#
# Do not edit this file manually !! USE crontab-e INSTEAD.
#
#
01 ***/usr/sbin/run-cron/etc/cron. hourly
02 00 ***/usr/sbin/run-cron/etc/cron. daily
22 00 ** 0/usr/sbin/run-cron/etc/cron. weekly
42 00 1 **/usr/sbin/run-cron/etc/cron. monthly
56 06 ***/home/zhangy/www/bb. php
51 23 ***/sbin/shutdown-h now
00 18 ***/home/zhangy/database_bak.sh
Then exit