`
- 浏览:
95478 次
- 性别:
- 来自:
北京
-
backup_mysql.sh
#!/bin/sh
# set -x
## this script is for auto mysql backup
## log file: /opt/alu/logs/3rd_party/mysql/backup.log
declare db_names
LOG_FILE=/opt/alu/logs/3rd_party/mysql/backup.log
TIME=`date +"%Y%m%d%H%M%S"`
DUMP_FILE=".sql"
TGZ_FILE=".tgz"
SHELL_DIR=/opt/alu/shell/sysmgt
log_success_msg(){
echo " SUCCESS! $@"
}
log_failure_msg(){
echo " ERROR! $@"
}
p_echo(){
echo >> ${LOG_FILE}
echo "-------------Backup-------------" >> ${LOG_FILE}
echo `date +"%Y-%m-%d %H:%M:%S"` >> ${LOG_FILE}
echo "-------------Backup-------------" >> ${LOG_FILE}
echo >> ${LOG_FILE}
}
## check mysql pid, and kill it
checkProcess(){
PIDS=`ps -ef|grep mysqld|grep -v grep|grep 3306|awk '{print $2}'`
if [ -n ${PIDS} ]; then
for pid in ${PIDS}
do
kill -9 ${pid}
done
fi
}
## check mysql service, make sure it's alive
checkStatus(){
`mysqladmin ping > /dev/null 2>&1`
if [[ $? != 0 ]]; then
checkProcess
echo "mysql is not alive,will be start now!" >> ${LOG_FILE}
${SHELL_DIR}/mysql_supervise.sh start >> ${LOG_FILE} 2>&1
fi
`mysqladmin ping > /dev/null 2>&1`
if [[ $? != 0 ]];then
echo "Mysql server error"
exit 1
fi
}
## find all database name
find_db_names(){
DB_NAMES_ALL=$(mysql -e "show databases;")
index=0
for DB in ${DB_NAMES_ALL}
do
if [[ "$DB" != "Database" && "$DB" != "information_schema" && "$DB" != "performance_schema" ]];then
db_names[$index]=$DB
let index++
fi
done
}
## tgz file today exists,delete it
delete_old_file(){
if [ -f $1 ]; then
echo "[$1] Backup file is exists,will be delete" >> ${LOG_FILE}
rm -f $1 > /dev/null 2>&1
fi
}
## mysqldump function
func_mysqldump(){
db_dump_file=$1$DUMP_FILE
db_tgz_file=$1$TGZ_FILE
mysqldump -q --add-drop-table --single-transaction --lock-tables=false $1 > $db_dump_file
tar -czvf $db_tgz_file $db_dump_file > /dev/null
echo "[$1] Backup success!"
echo "[${db_tgz_file}] Backup success!" >> ${LOG_FILE}
rm -rf $db_dump_file > /dev/null 2>&1
}
## backup all db
backup_all(){
# find all db name
find_db_names
echo
echo -e "Please enter the directory for save the backup file:"
read dir
if [[ ! -d $dir ]];then
log_failure_msg "directory error!"
exit 1
fi
echo
cd $dir; mkdir $TIME; cd $TIME
echo "Backup............."
for arr in ${db_names[@]}
do
func_mysqldump $arr
done
echo "Finish"
}
## backup one db user specified
backup_one(){
# find all db name
find_db_names
echo
echo -e "Please enter the db name you want to backup(${db_names[@]}):"
read dbname
flag=1
for arr in ${db_names[@]}
do
if [[ $dbname = $arr ]];then
flag=0
break
else
flag=1
continue
fi
done
if [[ $flag = 1 ]];then
log_failure_msg "db not exist!"
exit 1
fi
echo
echo -e "Please enter the directory for save the backup file:"
read dir
if [[ ! -d $dir ]];then
log_failure_msg "directory error!"
exit 1
fi
echo
# others,all right
cd $dir; mkdir $TIME; cd $TIME
echo "Backup.................."
func_mysqldump $dbname
echo "Finish"
}
## for one parameter
has_param(){
dir=$1
if [[ ! -d $dir ]];then
log_failure_msg "directory error!"
exit 1
fi
# find all db name
find_db_names
cd $dir; mkdir $TIME; cd $TIME
for arr in ${db_names[@]}
do
func_mysqldump $arr
done
exit $?
}
menu(){
echo
PS3="Please select: "
select t in "backup all" "backup one db" "exit"
do
case $t in
'backup all')
backup_all
exit $?
;;
'backup one db')
backup_one
exit $?
;;
*)
exit 1
;;
esac
done
}
######### main
main(){
p_echo
checkStatus
if [[ $# != 0 ]];then
has_param $@
else
menu
fi
}
main $@
restore_mysql.sh
#!/bin/sh
#set -x
## it's for mysql restore
declare array
declare array_sql
declare time_select
LOG_FILE=/opt/alu/logs/3rd_party/mysql/backup.log
SHELL_DIR=/opt/alu/shell/sysmgt
log_failure_msg(){
echo " ERROR! $@"
}
p_echo(){
echo >> ${LOG_FILE}
echo "------------Restore------------" >> ${LOG_FILE}
echo `date +"%Y-%m-%d %H:%M:%S"` >> ${LOG_FILE}
echo "------------Restore------------" >> ${LOG_FILE}
echo >> ${LOG_FILE}
}
## number match
number_match(){
if [[ $# = 0 ]];then
echo "Need a parameter"
return 1
else
if [[ ! $1 =~ ^[0-9]+$ ]]
then
return 1
fi
return 0
fi
}
## validate user's enter
## $1,enter string; $2,a number
enter_validate(){
number_match $1
if [[ $? != 0 ]];then
return 1
fi
if [[ $1 -lt 1 || $1 -gt $2 ]];then
return 1
fi
return 0
}
## make sure if mysql's status is OK
check_status(){
`mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} ping >>/dev/null 2>&1`
if [[ $? != 0 ]]; then
PIDS=`ps -ef|grep mysqld|grep -v grep|grep 3306|awk '{print $2}'`
if [[ -n ${PIDS} ]]; then
for pid in ${PIDS}
do
kill -9 ${pid}
done
fi
echo "Mysql is not alive,will be start now!" >> ${LOG_FILE}
${SHELL_DIR}/mysql_supervise.sh start >> /dev/null 2>&1
fi
}
## list all file folder name,such as 20110928171259,20110927171259
time_folder_names(){
i=0
files=`ls -t $@`
for file in ${files}
do
array[$i]=$file
((i++))
done
}
## show folder list,and get the selected
func_folder_select(){
i=0
folders=`ls -t $@`
for folder in $folders
do
array[$i]=$folder
((i++))
done
# time_folder_names $@
len=${#array[@]}
# No backup file
if [[ $len = 0 ]];then
echo "No backuped scripts under $@,exit."
exit 1
fi
# have files
echo
echo "Please select the time"
for((index=0;index<$len;index++))
do
echo
echo "[`expr $index + 1`] "${array[$index]}
done
echo
echo "Please input the number before folder name.Otherwise,exit.Input:"
read select
# validate
enter_validate $select $len
if [[ $? = 1 ]];then
echo "Input error.Exit now"
exit 1
fi
# selected folder name
time_select=${array[`expr $select - 1`]}
}
func_param(){
read -p "[$1] Sure to restore now?[yes or no]:"
if [[ "$REPLY" = "y" || "$REPLY" = "Y" || "$REPLY" = "yes" || "$REPLY" = "YES" ]]
then
p_echo
func_folder_select $@
restore_all $@/$time_select
else
echo "Not restore,exit now"
exit 1
fi
}
## public restore function,$1 is dbname,$2 is sql file
restore(){
mysql -e "CREATE DATABASE IF NOT EXISTS $1;" >> ${LOG_FILE} 2>&1
mysql $1 < $2 >> ${LOG_FILE} 2>&1
if [[ $? = 0 ]];then
echo "[$1] Restore success!"
echo "[$1] Restore success!" >> ${LOG_FILE}
else
echo "[$1] Restore fail!"
echo "[$1] Restore fail!" >> ${LOG_FILE}
fi
}
restore_all(){
sql_dir=$@
files=`ls $sql_dir`
if [[ -z $files ]];then
echo "No backup file.exit." >> ${LOG_FILE}
exit 1
fi
cd $sql_dir >> /dev/null
for file in $files
do
tar -zxvf $file >> /dev/null
dbname=${file%.*}
sql=$dbname".sql"
restore $dbname $sql
done
# delete sql files
ls *.sql|xargs rm -rf
}
restore_one(){
files=`ls $@/$time_select`
if [[ -z $files ]];then
echo "No backup file.exit." >> ${LOG_FILE}
exit 1
fi
echo
echo "Database list below:"
echo
i=0
for file in $files
do
array_sql[$i]=$file
echo "[`expr $i + 1`] "${file%.*}
((i++))
done
len=${#array_sql[@]}
echo "Please select:"
read select
enter_validate $select $len
if [[ $? = 1 ]];then
echo "Input error.Exit now"
exit 1
fi
# restore
cd $@/$time_select
tgz_file=${array_sql[`expr $select - 1`]}
tar -zxvf $tgz_file >> /dev/null
db=${tgz_file%.*}
sql=$db".sql"
restore $db $sql
ls *.sql|xargs rm -rf
}
menu(){
echo
echo "Please enter the backup script file directory:"
read dir
if [[ ! -d $dir ]];then
log_failure_msg "directory error!"
exit 1
fi
echo
func_folder_select $dir
echo
echo "Please select the restore type:"
PS3="Please select: "
select t in "Restore all" "Restore one db" "exit"
do
case $t in
'Restore all')
restore_all $dir/$time_select
exit $?
;;
'Restore one db')
restore_one $dir
exit $?
;;
*)
exit 1
;;
esac
done
}
####### main
main(){
p_echo
check_status
if [ $# = 0 ];then
menu
else
func_param $@
fi
}
main $@
mysql_supervise.sh
#!/bin/sh
PID_MYSQL=`ps -ef|grep mysqld|grep -v grep|grep 3306|awk '{print $2}'`
PID_LOC=/opt/alu/logs/sysmgt/
mode=$1 # start or stop
basename=`basename "$0"`
if [ $# = 0 ]; then
echo "Usage:$basename {start|stop}"
exit 1
fi
[ $# -ge 1 ] && shift
case "$mode" in
'start')
if [ -n "${PID_MYSQL}" ]; then
echo "MySQL is running (${PID_MYSQL})"
exit 1
fi
# delete old pid file if exists
if
ls -lrt $PID_LOC|grep mysql >/dev/null 2>&1
then
rm -rf ${PID_LOC}mysql.*
fi
/etc/init.d/mysql start
PID_MYSQL=`ps -ef|grep mysqld|grep -v grep|grep 3306|awk '{print $2}'`
touch "${PID_LOC}mysql."${PID_MYSQL}
echo ${PID_MYSQL}
;;
'stop')
if [ -z "${PID_MYSQL}" ]; then
echo "MySQL is not running"
exit 1
fi
/etc/init.d/mysql stop
if
ls "${PID_LOC}mysql."${PID_MYSQL} >/dev/null 2>&1
then
rm -f "${PID_LOC}mysql."${PID_MYSQL}
fi
;;
*)
echo "Usage:$basename {start|stop}"
exit 1
;;
esac
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
了解这些基本概念后,你可以查看压缩包内的`基于xtrabackup的MySQL数据库备份及还原Shell脚本`,这个脚本将详细展示如何结合`xtrabackup`和Shell来自动化MySQL数据库的备份与还原流程。在实际使用时,你需要根据自己...
本篇将详细阐述如何使用Shell脚本来实现MySQL数据库的备份与恢复。 首先,让我们分析提供的两个脚本文件: 1. `datarestore.sh`:这个脚本通常用于执行数据库恢复操作。它可能包含了连接到MySQL服务器,选择要恢复...
今天这个备份分二个版本一个是linux上直接安装的mysql,另一种是docker上安装的mysql。基本操作都一样只是备份sql语句不同而已。可以选择设置需要备份的库,自动备份压缩,自动删除 7 天前的备份,需要使用 crontab ...
《Xtrabackup备份与恢复:Shell脚本详解》 ...总结,Xtrabackup结合Shell脚本提供了一套强大的MySQL备份恢复解决方案,能够应对各种复杂的业务场景。理解并熟练掌握这一工具,对于保障数据库的安全运行具有重要意义。
下面我们将深入探讨如何利用shell脚本来执行MySQL备份。 首先,我们需要理解MySQL备份的基本类型:全量备份和增量备份。全量备份是指备份整个数据库或所有选定的数据库,而增量备份则仅备份自上次备份以来发生更改...
Linux下自动备份Mysql文件shell脚本。
今天这个备份分二个版本一个是linux上直接安装的mysql,另一种是docker上安装的mysql。基本操作都一样只是备份sql语句不同而已。可以选择设置需要备份的库,自动备份压缩,自动删除 7 天前的备份,需要使用 crontab ...
以下是一些关于如何创建和使用MySQL自动备份shell脚本的详细说明: 首先,我们需要一个能够执行备份、压缩、清理旧备份并(可选地)将备份文件传输到远程位置或发送电子邮件通知的脚本。以下两个示例提供了不同选项...
"mysql自动实现备份脚本" 提供了一种高效的方法,通过编写Bash shell脚本来自动化这一过程。下面将详细介绍这个脚本的工作原理、重要性以及如何配置和使用。 1. **Bash脚本简介** Bash是Unix和Linux系统中的默认...
mysql数据库备份shell脚本,每天备份,只保留30天的备份数据
本文档介绍了一个用于在Linux环境下自动备份MySQL数据库的Shell脚本。通过利用Cron定时任务功能,该脚本能够在特定时间自动运行,完成数据库的备份操作。以下是该脚本的主要组成部分及功能说明: ##### 2.1 变量...
在本场景中,我们有一个名为`auto_mysql_back.sh`的shell脚本,它用于自动化MySQL数据库的备份过程。下面将详细解释这个脚本可能涉及的关键知识点。 1. **MySQL数据库备份**: - **全量备份**:通常,全量备份是指...
通过上述的步骤,我们可以利用Shell脚本实现MySQL的自动备份和恢复。在实际操作中,需要确保备份用户有足够的权限执行`mysqldump`操作,同时也需要调整备份策略,比如备份间隔、保留时间等,以满足实际的业务需求。 ...
shell脚本可以用于mysql备份、传输、删除,例如使用shell脚本编写一个mysql备份、传输、删除的程序,实现自动化数据库管理。 自动备份mysql库文件 shell脚本可以用于自动备份mysql库文件,例如使用shell脚本编写一...
Shell脚本可以自动备份数据库,以便在需要时恢复数据。 4. MySQL数据库备份多循环 这个脚本可以备份多个MySQL数据库,这在实际应用中非常有用。我们使用mysqldump命令来备份数据库,并将备份文件保存到指定的目录...
MySQL数据迁移是一个重要的...总结来说,通过编写Shell脚本自动化MySQL数据迁移,我们可以提高效率,减少错误,并实现定制化的迁移策略。理解这些基本原理和操作后,你可以根据自己的需求创建适应各种情况的迁移脚本。
本文将详细介绍MySQL数据库的自动备份与恢复策略,特别是在数据库遭到破坏后进行数据恢复的方法,以及如何通过Shell脚本实现整个备份恢复流程的自动化。 ### MySQL自动备份与恢复的重要性 在数据库服务器搭建完成后...
backup mysql databases and keep newest 5 days backup
在本文中,我们将深入探讨MySQL自动备份脚本的原理、编写方法以及如何利用shell脚本来实现这一功能。 首先,理解MySQL备份的必要性至关重要。数据库是任何应用程序的核心,其中存储着关键的业务数据。因此,定期...
mysql数据库定期自动备份的shell脚本。如定期每三天自动备份数据库,并只保存20份备份文件,多余的删除。