天天看点

数据库结构版本控制

#!/bin/bash

###################################

# $id$

# author: [email protected]

# home: http://netkiller.github.com

# select `user`, `host`, `password` from `mysql`.`user`;

# create user 'backup'@'localhost' identified by 'sajepom6bapomofod7xo3e1a52vepe';

# grant select, lock tables on *.* to 'backup'@'localhost';

# flush privileges;

# show grants for 'backup'@'localhost';

backup_host="localhost"

backup_user="backup"

backup_pass="chen"

backup_dbname="test aabbcc"

backup_dir=~/backup

####################################

name=backup.mysql.struct

basedir='/www'

prog=$basedir/bin/$(basename $0)

logfile=/var/tmp/$name.log

pidfile=/var/tmp/$name.pid

mysqldump="/usr/bin/mysqldump"

mysqldump_opts="-h $backup_host -u$backup_user -p$backup_pass --skip-comments -d"

umask 0077

##############################################

#rotate=60

loop=30

function backup(){

test ! -d "$backup_dir" && echo "error: $backup_dir isn't a directory." && exit 0

cd $backup_dir

for dbname in $backup_dbname

do

test ! -d "$backup_dir/$backup_host" && mkdir -p "$backup_dir/$backup_host"

$mysqldump $mysqldump_opts $dbname > $backup_dir/$backup_host/$dbname.sql

done

timepoint=$(date -u +%y-%m-%d.%h:%m:%s)

git add .

git commit --quiet -m "$timepoint" > /dev/null

}

function start(){

if [ -f "$pidfile" ]; then

echo $pidfile

exit 2

fi

test ! -w $backup_dir && echo "error: $backup_dir is un-writeable." && exit 0

for (( ; ; ))

backup

sleep $loop

done &

echo $! > $pidfile

function stop(){

   [ -f $pidfile ] && kill `cat $pidfile` && rm -rf $pidfile

function init(){

if [ ! -d $backup_dir ]; then

mkdir -p "$backup_dir"

git init

    fi

case "$1" in

  start)

   start

;;

  stop)

   stop

  status)

   ps ax | grep $(basename $0) | grep -v grep | grep -v status

  restart)

start

init)

init

*)

echo $"usage: $0 {init|start|stop|status|restart}"

exit 127

esac

exit $?