suse-linux定时备份数据库和文件shell脚本

#!/bin/bash

#2009-11-8第一次编写

#作者:duanyunxi

#版本:v1.1

#备份脚本

##设置备份##

echo"##开始设置备份!##"

if[-f/etc/crontab];then

RETVAL=`servicecronstatus|grep'running'`

if["$RETVAL"!=""];then

echo"请选择备份格式(s:数据库|t:tar.gz压缩包)?"

readENCODE

#################数据库sql脚本备份###########

if["$ENCODE"=="s"]||["$ENCODE"=="S"];then

##提示输入mysql管理员用户root的密码##

RETVAL="1"

until[$RETVAL-eq0]

do

echo"请输入mysql管理员用户root的密码"

readMYSQLPASSWD

mysqladmin-uroot-p$MYSQLPASSWDversion1>/dev/null2>&1

RETVAL=$?

if[$RETVAL-eq0];then

echo"mysql管理员root密码输入正确!"

else

echo"mysql管理员密码错误,请重新输入!"

fi

done

##提示输入要备份的数据库名##

echo"请输入要备份数据库名,例如:icfjkcms"

INPUT="n"

until["$INPUT"=="Y"]||["$INPUT"=="y"]

do

readDATABASE

echo"您输入的数据库名是:$DATABASE,请确认(y|n)?"

readINPUT

if["$INPUT"!="Y"]&&["$INPUT"!="y"];then

echo"请输入要备份数据库名,例如:icfjkcms"

else

echo"您输入的备份数据库名为:$DATABASE"

fi

done

##提示输入备份频率##

echo"***定时备份频率格式:分时天月一周第几天"

echo"***例如:*/206-12*12*表示在12月内,每天的早上6点到12点中,每隔20分钟执行一次"

echo"请输入备份频率,例如:*/2****"

INPUT="n"

until["$INPUT"=="Y"]||["$INPUT"=="y"]

do

readRATE

echo"您输入的备份频率是:$RATE,请确认(y|n)?"

readINPUT

if["$INPUT"!="Y"]&&["$INPUT"!="y"];then

echo"请输入备份频率,例如:*/2****"

else

echo"您输入的备份频率为:$RATE"

fi

done

DAY=`date+%Y%m%d`

TIME=`date+%H%M%S`

###备份文件备份脚本存放目录###

mkdir-p/var/backup/$DAY/sh

echo"生成备份的脚本文件"

echo"#!/bin/bash">>/var/backup/$DAY/sh/$DATABASE-SQL$TIME.sh

echo"BACKDAY=\`date+%Y%m%d%H%M\`">>/var/backup/$DAY/sh/$DATABASE-SQL$TIME.sh

echo"mysqldump--default-character-set=utf8-hlocalhost-uroot-p$MYSQLPASSWD-B-d--opt-q$DATABASE>/var/backup/$DAY/DDL$DATABASE-\$BACKDAY.sql;">>/var/backup/$DAY/sh/$DATABASE-SQL$TIME.sh

echo"mysqldump--default-character-set=utf8-hlocalhost-uroot-p$MYSQLPASSWD-B-t--opt-q$DATABASE>/var/backup/$DAY/DML$DATABASE-\$BACKDAY.sql;">>/var/backup/$DAY/sh/$DATABASE-SQL$TIME.sh

chmod775/var/backup/$DAY/sh/$DATABASE-SQL$TIME.sh

echo"备份脚本生成完成"

###写README文件

echo"写README文件"

echo"制作人:段云喜">>/var/backup/$DAY/sh/$DATABASE-SQL$TIME.readme

echo"制作时间:$DAY">>/var/backup/$DAY/sh/$DATABASE-SQL$TIME.readme

echo"$DATABASE-SQL$TIME.sh为备份$DATABASE数据库脚本">>/var/backup/$DAY/sh/$DATABASE-SQL$TIME.readme

echo"cron执行$DATABASE-SQL$TIME.sh频率是$RATE">>/var/backup/$DAY/sh/$DATABASE-SQL$TIME.readme

echo"备份生成的sql脚本在/var/backup/$DAY目录下">>/var/backup/$DAY/sh/$DATABASE-SQL$TIME.readme

echo"备份文件完成,去/var/backup/$DAY/sh/里查看$DATABASE-SQL$TIME.readme文件"

sleep3

echo"停cron服务"

servicecronstop

###定时备份---每隔二分钟备份一次###

echo"$RATEroottest-x/var/backup/$DAY/sh/$DATABASE-SQL$TIME.sh&&/var/backup/$DAY/sh/$DATABASE-SQL$TIME.sh">>/etc/crontab

sleep3

echo"开启cron服务"

servicecronstart

echo"备份配置完成!"

#################文件夹/文件tar.gz打包备份###########

elif["$ENCODE"=="t"]||["$ENCODE"=="T"];then

echo"请输入备份文件名前缀,例如:www"

INPUT="n"

until["$INPUT"=="Y"]||["$INPUT"=="y"]

do

readPREFIX

echo"您输入的备份文件名前缀是:$PREFIX,请确认(y|n)?"

readINPUT

if["$INPUT"!="Y"]&&["$INPUT"!="y"];then

echo"请输入备份文件名前缀,例如:www:"

else

echo"您输入的备份文件名前缀为:$PREFIX"

fi

done

echo"请输入要备份的路径,例如:/usr/local/site"

INPUT="n"

until["$INPUT"=="Y"]||["$INPUT"=="y"]

do

readDATAPATH

echo"您输入要备份的路径是:$DATAPATH,请确认(y|n)?"

readINPUT

if["$INPUT"!="Y"]&&["$INPUT"!="y"];then

echo"请输入要备份的路径,例如:/usr/local/site:"

else

echo"你要备份的路径为:$DATAPATH"

fi

done

echo"请输入备份路径中不备份的文件或文件夹:(多个用空格隔开)如果没有直接确认!"

INPUT="n"

until["$INPUT"=="Y"]||["$INPUT"=="y"]

do

readFILES

echo"您输入不备份的文件或文件夹是:$FILES,请确认(y|n)?"

readINPUT

if["$INPUT"!="Y"]&&["$INPUT"!="y"];then

echo"请输入备份路径中不备份的文件或文件夹:(多个用空格隔开)"

else

echo"您输入不备份的文件或文件夹是:$FILES"

fi

done

##提示输入备份频率##

echo"***定时备份频率格式:分时天月一周第几天"

echo"***例如:*/206-12*12*表示在12月内,每天的早上6点到12点中,每隔20分钟执行一次"

echo"请输入备份频率,例如:*/2****"

INPUT="n"

until["$INPUT"=="Y"]||["$INPUT"=="y"]

do

readRATE

echo"您输入的备份频率是:$RATE,请确认(y|n)?"

readINPUT

if["$INPUT"!="Y"]&&["$INPUT"!="y"];then

echo"请输入备份频率,例如:*/2****"

else

echo"您输入的备份频率为:$RATE"

fi

done

DAY=`date+%Y%m%d`

TIME=`date+%H%M%S`

###备份文件备份脚本存放目录###

mkdir-p/var/backup/$DAY/sh

forFILEin$FILES

do

EXCLUDE="$EXCLUDE--exclude$FILE"

done

echo"生成备份的脚本文件"

echo"#!/bin/bash">>/var/backup/$DAY/sh/$PREFIX-TAR$TIME.sh

echo"BACKDAY=\`date+%Y%m%d%H%M\`">>/var/backup/$DAY/sh/$PREFIX-TAR$TIME.sh

echo"cd$DATAPATH">>/var/backup/$DAY/sh/$PREFIX-TAR$TIME.sh

echo"tar$EXCLUDE-zcpf/var/backup/$DAY/$PREFIX-\$BACKDAY.tar.gz.">>/var/backup/$DAY/sh/$PREFIX-TAR$TIME.sh

chmod775/var/backup/$DAY/sh/$PREFIX-TAR$TIME.sh

echo"备份脚本生成完成"

###写README文件

echo"写README文件"

echo"制作人:段云喜">>/var/backup/$DAY/sh/$PREFIX-TAR$TIME.readme

echo"制作时间:$DAY">>/var/backup/$DAY/sh/$PREFIX-TAR$TIME.readme

echo"$PREFIX-TAR$TIME.sh为备份$DATAPATH的脚本不包括$FILES">>/var/backup/$DAY/sh/$PREFIX-TAR$TIME.readme

echo"cron执行$PREFIX-TAR$TIME.sh频率是$RATE">>/var/backup/$DAY/sh/$PREFIX-TAR$TIME.readme

echo"备份生成的tar.gz压缩包在/var/backup/$DAY目录下">>/var/backup/$DAY/sh/$PREFIX-TAR$TIME.readme

echo"备份文件完成,去/var/backup/$DAY/sh/里查看$PREFIX-TAR$TIME.readme文件"

sleep3

echo"停此cron服务"

servicecronstop

###定时备份---每隔二分钟备份一次###

echo"$RATEroottest-x/var/backup/$DAY/sh/$PREFIX-TAR$TIME.sh&&/var/backup/$DAY/sh/$PREFIX-TAR$TIME.sh">>/etc/crontab

sleep3

echo"开启cron服务"

servicecronstart

echo"备份配置完成!"

fi

fi

fi

相关推荐