Chinaunix首页 | 论坛 | 博客
  • 博客访问: 384055
  • 博文数量: 87
  • 博客积分: 2810
  • 博客等级: 少校
  • 技术积分: 825
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-28 22:34
文章分类

全部博文(87)

文章存档

2010年(25)

2009年(43)

2008年(19)

分类: Mysql/postgreSQL

2008-12-08 21:46:14

一 下载该shell脚本

二 针对你的操作系统环境,修改这个脚本里面的设置

三 把脚本加入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
阅读(1290) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~