分类: Mysql/postgreSQL
2008-12-08 21:46:14
二 针对你的操作系统环境,修改这个脚本里面的设置
三 把脚本加入crontab里,设定每小时运行shell脚本,例如
# Backup database every 1 hr to /nas
@hourly /root/scripts/db1hr.backup.sh >/dev/null 2>&1
以下是该shell脚本的源代码:
#!/bin/bash # A simple shell script to backup all MySQL Server Database # Dump all MySQL database every hour from raid10 db disk to /nas/mysql # Each dump will be line as follows: # Directory: /nas/mysql/mm-dd-yyyy # File: mysql-DBNAME.04-25-2008-14:23:40.gz # Full path: /nas/mysql/mm-dd-yyyy/mysql-DBNAME.04-25-2008-14:23:40.gz # ------------------------------------------------------------------------- # Copyright (c) 2005 nixCraft project < # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit for more information. # ------------------------------------------------------------------------- NOW=$(date +"%m-%d-%Y") # mm-dd-yyyy format FILE="fs-full-$NOW.tar.gz" BAK="/nas/mysql/${NOW}" ### Server Setup ### #* MySQL login user name *# MUSER="root" #* MySQL login PASSWORD name *# MPASS="YOUR-PASSWORD" #* MySQL login HOST name *# MHOST="127.0.0.1" #* MySQL binaries *# MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" GZIP="$(which gzip)" # assuming that /nas is mounted via /etc/fstab if [ ! -d $BAK ]; then mkdir -p $BAK else : fi # get all database listing DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')" # start to dump database one by one for db in $DBS do FILE=$BAK/mysql-$db.$NOW-$(date +"%T").gz # gzip compression for each backup file $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE done