linux备份数据库和配置文件shell脚本

linux备份数据库和配置文件shell脚本

#!/bin/bash
# backup root path
backup_root_path=/data/backup
# the time of this backup
backup_time=`date +%Y%m%d%H%M%S`
# year
path_year=${backup_time:0:4}
# month
path_month=${backup_time:4:2}
# day
path_day=${backup_time:6:2}

# the path of year,if not exists,create this dir.
path_year_full=${backup_root_path}"/"${path_year}
if [ ! -d "$path_year_full" ];then
mkdir "$path_year_full"
fi

# the path of month,if not exists,create this dir.
path_month_full=${path_year_full}"/"${path_month}
if [ ! -d "$path_month_full" ];then
mkdir "$path_month_full"
fi

# the path of day,if not exists,create this dir.
path_day_full=${path_month_full}"/"${path_day}
if [ ! -d "$path_day_full" ];then
mkdir "$path_day_full"
fi

# --------------------------
# backup mysql databases
# --------------------------
path_backup_mysql=${path_day_full}"/mysql"
if [ ! -d "$path_backup_mysql" ];then
mkdir "$path_backup_mysql"
fi

db_names=(db1 db2)
db_user=root
db_pass=123456

for db_name in ${db_names[@]}
do
db_file_name=${path_backup_mysql}"/db"${backup_time}${db_name}
#echo $db_file_name
mysqldump -u${db_user} -p${db_pass} --opt -q -R $db_name | gzip >"$db_file_name".sql.gz
done

# ------------------------
# backup config file
# ------------------------
bk_config_path=config
path_backup_config=${path_day_full}"/"${bk_config_path}
if [ ! -d "$path_backup_config" ];then
mkdir "$path_backup_config"
fi
# src file
config_files=(
 "/usr/local/nginx-1.6.0/conf/nginx.conf"
 "/usr/local/tomcat/apache-tomcat-7.0.54/conf/server.xml"
 "/etc/mysql/my.cnf"
)
# dst file
config_dst_files=(
 "nginx.conf"
 "tomcat_conf_server.xml"
 "mysql_my.cnf"
)

config_count=${#config_files[@]}
#echo $config_count

for ((i=0;i<config_count;i++))
do
#echo $i
#echo ${config_files[i]} to ${path_backup_config}"/"${config_dst_files[i]}
cp -f ${config_files[i]} ${path_backup_config}"/"${config_dst_files[i]}
done

相关推荐