[code]01 5 * * * root /home/mysqlbackup/backup.sh
31 5 * * * root /home/mysqlbackup/back.sh
01 8 * * * root /home/mysqlbackup/backup.sh
40 9 * * * root /home/mysqlbackup/back.sh
41 9 * * * root /home/mysqlbackup/backup.sh
31 12 * * * root /home/mysqlbackup/back.sh
01 15 * * * root /home/mysqlbackup/backup.sh
31 15 * * * root /home/mysqlbackup/back.sh
01 18 * * * root /home/mysqlbackup/backup.sh
31 18 * * * root /home/mysqlbackup/back.sh
01 21 * * * root /home/mysqlbackup/backup.sh
31 21 * * * root /home/mysqlbackup/back.sh
01 23 * * * mysql /home/mysqlbackup/backup.sh
31 23 * * * mysql /home/mysqlbackup/back.sh[/code]我的想法:我是想把我的MYSQL数据库自动备份,在网上找了很多的资料,终于写好了备份脚本,共2个,就是上面看到的back.sh以及backup.sh。我在终端中SH都可以进行备份的,证明这个脚本应该不是问题所在。
那自动备份我采用的是 CRONTAB的方法,参看例子可以发现我写的自己还认为是正确的。
可是怎么也不能自动地进行备份,用service crond restart看出,这个服务是启动的。
我就觉得应该是书写格式是正确的。
我想问下老鸟,我以上代码中显示的是不是正确的?
如果可以,能不能帮我改下。我的要求就是每天在8点、12点、17点半、21点50、12点、凌晨5点各自备份一下。
谢谢。
[ 本帖最后由 byqy 于 2008-8-14 15:09 编辑 ]
byqy 于 2008-08-14 13:49:56发表:
[code]Aug 14 00:01:01 localhost crond[14837]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 01:01:01 localhost crond[14989]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 02:01:01 localhost crond[15150]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 03:01:01 localhost crond[15270]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 04:01:01 localhost crond[15441]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 04:02:01 localhost crond[15457]: (root) CMD (run-parts /etc/cron.daily)
Aug 14 04:02:02 localhost anacron[15472]: Updated timestamp for job `cron.daily' to 2008-08-14
Aug 14 05:01:01 localhost crond[15905]: (mysql) CMD (/home/mysqlbackup/backup.sh)
Aug 14 05:01:01 localhost crond[15907]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 05:31:01 localhost crond[15970]: (mysql) CMD (/home/mysqlbackup/back.sh)
Aug 14 06:01:01 localhost crond[16024]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 07:01:01 localhost crond[16132]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 08:01:01 localhost crond[16241]: (mysql) CMD (/home/mysqlbackup/backup.sh)
Aug 14 08:01:01 localhost crond[16243]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 08:31:01 localhost crond[16306]: (mysql) CMD (/home/mysqlbackup/back.sh)
Aug 14 09:01:01 localhost crond[16360]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 10:01:01 localhost crond[16468]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 11:01:01 localhost crond[16576]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 12:01:01 localhost crond[16685]: (mysql) CMD (/home/mysqlbackup/backup.sh)
Aug 14 12:01:01 localhost crond[16687]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 12:31:01 localhost crond[16750]: (mysql) CMD (/home/mysqlbackup/back.sh)
Aug 14 13:01:01 localhost crond[16804]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 14:01:01 localhost crond[16912]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 15:01:01 localhost crond[17021]: (mysql) CMD (/home/mysqlbackup/backup.sh)
Aug 14 15:01:01 localhost crond[17022]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 15:31:01 localhost crond[17086]: (mysql) CMD (/home/mysqlbackup/back.sh)
Aug 14 16:01:01 localhost crond[17140]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 17:01:02 localhost crond[17294]: (root) CMD (run-parts /etc/cron.hourly)
Aug 14 17:04:23 localhost crond[17329]: (CRON) STARTUP (V5.0)
Aug 14 17:04:33 localhost crond[17348]: (CRON) STARTUP (V5.0)
Aug 14 17:05:14 localhost crond[17370]: (CRON) STARTUP (V5.0)
Aug 14 09:28:01 localhost crond[17370]: (*system*) RELOAD (/etc/crontab)
Aug 14 09:28:07 localhost crond[17988]: (CRON) STARTUP (V5.0)
Aug 14 09:30:01 localhost crond[17993]: (root) CMD (/home/mysqlbackup/back.sh)
Aug 14 09:31:01 localhost crond[18000]: (root) CMD (/home/mysqlbackup/backup.sh)[/code]这个是日志
byqy 于 2008-08-14 13:44:59发表:
不知道日志在什么地方。
脚本我觉得没什么错,因为,我SH运行都可以的啊
脚本一[code]#!/bin/sh
# File: /home/mysql/backup.sh
# Database info
DB_NAME="znzjw"
DB_USER="znzjw"
DB_PASS="znzjw123456"
# Others vars
BIN_DIR="/usr/bin"
BCK_DIR="/home/mysqlbackup/backupfiles"
DATE=`date +%F%T`
# TODO
$BIN_DIR/mysqldump --opt -u$DB_USER -p$DB_PASS $DB_NAME | gzip > $BCK_DIR/db_$DATE.gz
~[/code]脚本二[code]#!/bin/bash
#this is a shellscript for auto db backup
#put this into the crontab schedule then it will work
#
#author: Cao Haitao
#2007-04-30
#settings
#set the datebase name,database login name,password,backup files' dir,backup log files' dir
#database name to backup
dbname=znzjw
#database username to login
dbuser=znzjw
#password to login
dbpasswd=znzjw123456
#mysqldump's path
mysqldump=/usr/bin/mysqldump
#logfile
logfile=/home/mysqlbackup/logfiles/baklog.log
#backup files' dir
backupdir=/home/mysqlbackup/backupfiles/
newfile="$backupdir"mydb.$(date +%Y-%m-%d).sql
oldfile="$backupdir"mydb.$(date +%Y-%m-%d --date='7 days ago').sql
#delete ole file
if [ -f $oldfile ]
then
rm -f $oldfile
echo "[$oldfile] delete ole file success!" >> $logfile
else
echo "[$oldfile] no old backup file." >>$logfile
fi
if [ -f $newfile ]
then
echo "[$newfile] the backup file is already exist." >> $logfile
else
if [ -z $dbpasswd ]
then
$mysqldump -u $dbuser --opt $dbname > $newfile
else
$mysqldump -u $dbuser -p$dbpasswd --opt $dbname > $newfile
fi
echo "[$newfile] create new file success." >> $logfile
fi
dbname=znzjw
#database username to login
dbuser=znzjw
#password to login
dbpasswd=znzjw123456
#mysqldump's path
mysqldump=/usr/bin/mysqldump
#logfile
logfile=/home/mysqlbackup/logfiles/baklog.log
#backup files' dir
backupdir=/home/mysqlbackup/backupfiles/
newfile="$backupdir"mydb.$(date +%Y-%m-%d).sql
oldfile="$backupdir"mydb.$(date +%Y-%m-%d --date='7 days ago').sql
#delete ole file
if [ -f $oldfile ]
then
rm -f $oldfile
echo "[$oldfile] delete ole file success!" >> $logfile
else
echo "[$oldfile] no old backup file." >>$logfile
fi
if [ -f $newfile ]
then
echo "[$newfile] the backup file is already exist." >> $logfile
else
if [ -z $dbpasswd ]
then
$mysqldump -u $dbuser --opt $dbname > $newfile
else
$mysqldump -u $dbuser -p$dbpasswd --opt $dbname > $newfile
fi
echo "[$newfile] create new file success." >> $logfile
fi[/code]
zgt0 于 2008-08-14 13:29:33发表:
出现什么样的错误,当时的有什么提示,还有日志。这些你都要提供啊。
jackzh 于 2008-08-14 12:58:30发表:
检查脚本中shell设置是否正确。
或将脚本贴出来瞧瞧。