svn 备份脚本(包含mysql数据库)
备份svn 数据,分两个脚本
svn_data.sh 用于备份svn 数据到,远程rsync 服务器
mysql_bak.sh 用于备份mysql 数据库到,远程rsync 服务器
cat /root/sh/svn_data.sh
- #!/bin/bash
- #backup /u01/svndata/
- #20110909 by dongnan
- #variables
- rsync=/usr/bin/rsync
- rsync_add=192.168.57.82
- svn_data_dir=/u01/svndata
- mysql_data_dir=/data/mysql
- l_time=`date +'%Y-%m-%d %H:%M:%S'`
- today_time=`date +'%Y-%m-%d'`
- log_dir=/root/sh/log
- #add for backup mysql
- /root/sh/mysql_bak.sh || echo "$l_time mysql backup error!" >> /root/sh/log/mysql_bak.err
- sleep 30
- #rsync
- for var in $svn_data_dir $mysql_data_dir ;do
- #echo $var && continue
- now_time=`date +"%F_%T"`
- if ! $rsync -avz --delete --password-file=/root/sh/pw "$var" dongnan@${rsync_add}::svn_data/ 2>>$log_dir/svn_err.txt;then
- echo $now_time >> $log_dir/svn_err.txt
- echo "#####" >> $log_dir/svn_err.txt
- fi
- done
cat /root/sh/mysql_bak.sh
- #!/bin/bash
- #backup mysql data
- #20111012 by dongnan
- #variables
- dump=/usr/bin/mysqldump
- mysql=/usr/local/bin/mysql
- option='-uroot -pdongnan --opt -e -x --master-data=2'
- databases=$(mysql -uroot -ppassword -e 'show databases;' | awk '$1 !~ /Database|information_schema|test/ {print $1}')
- bak_dir=/data/
- l_time=`date +'%Y-%m-%d %H:%M:%S'`
- now_time=`date +'%Y-%m-%d'`
- expires_time=`date +"%Y%m%d" --date='4 days ago'`
- log=/root/sh/log/mysql_bak.err
- date_dir=$(date +'%Y%m%d')
- #delete old file (4days)
- find "$bak_dir" -name "${expires_time}" -type d | xargs rm -rf
- #date dir
-
test -e ${bak_dir}${date_dir} || mkdir -p ${bak_dir}${date_dir}
- #backup and gzip database.sql
- for database in $databases;do
-
$dump $option $database | gzip > ${bak_dir}${date_dir}/${now_time}-${database}.sql.gz || echo "$l_time $database backup error!!" >> $log
- done
结束
更多欢迎到此讨论:
37275208
#update 20120605
gzip -dc 2012-06-01-uspace_uchome.sql.gz | mysql -uroot -pdongnan uspace_uchome
#update 20130210
- cat mysql_bak.sh
- #!/bin/bash
- #backup mysql data
- #20111012 by dongnan
- #variables
- dump=/usr/local/mysql/bin/mysqldump
- mysql=/usr/local/mysql/bin/mysql
- uuencode=/usr/bin/uuencode
- email=dongnan@mail.com
- option='-udongnan -pdongnan --opt -e -x --master-data=2'
- databases=$($mysql -udongnan -pdongnan -e 'show databases;' | awk '$1 !~ /Database|information_schema|test|mysql/ {print $1}')
- bak_dir=/backup/
- l_time=`date +'%Y-%m-%d %H:%M:%S'`
- now_time=`date +'%Y-%m-%d'`
- expires_time=`date +"%Y%m%d" --date='7 days ago'`
- log=/root/sh/log/mysql_bak.err
- date_dir=$(date +'%Y%m%d')
- #test command
- for command in $dump $mysql $uuencode;do
- if [[ ! -e $command ]];then
- echo "Not Found $command"
- exit 1
- fi
- done
- #delete old file (7days)
- find "$bak_dir" -name "${expires_time}" -type d | xargs rm -rf
- #date dir
- test -e ${bak_dir}${date_dir} || mkdir -p ${bak_dir}${date_dir}
- #backup and gzip database.sql
- for database in $databases;do
- ! test -e ${bak_dir}${date_dir}/${now_time}-${database}.sql.gz || break
- $dump $option $database | gzip > ${bak_dir}${date_dir}/${now_time}-${database}.sql.gz || echo "$l_time $database backup error!!" >> $log
- $uuencode ${bak_dir}${date_dir}/${now_time}-${database}.sql.gz ${now_time}-${database}.sql.gz | mail -s "${now_time}-${database}.sql.gz" $email
- done
#
本文转自 dongnan 51CTO博客,原文链接:http://blog.51cto.com/dngood/883480