阿里云帮助文档链接
文档结构:
一、 数据库软件的搭建及工具的准备
二、数据库备份的下载
三、备份在本地数据库中的恢复
一、数据库软件的搭建及工具的准备
-
- 数据库软件的搭建 -- 见mysql的搭建
- 2.工具的准备下载参考地址
二、从阿里云rds处获取下载数据库备份的链接
#!/home/.venv/bin/python
# - * - coding: utf-8 - * -
"""RDS CLI.
Usage:[]
rds_utils.py describe_backups_request
rds_utils.py describe_backups_intranet_request
rds_utils.py describe_dbinstanceperformance_request
rds_utils.py -h | --help
rds_utils.py --version
Options:
-h, --help display this help and exit.
--version output version information and exit.
"""
import json
import sys
import datetime
from docopt import docopt
from aliyunsdkcore import client
from aliyunsdkrds.request.v20140815 import DescribeBackupsRequest, \
DescribeDBInstancePerformanceRequest
access_key_id = ""
access_key_secret = ""
access_region = "cn-shenzhen"
accept_fomat = "json"
db_instanceid = ""
def get_time_range():
u"""获取查询备份数据库文件的开始和结束时间."""
now_time = datetime.datetime.now()
end_at = datetime.datetime.strftime(now_time, '%Y-%m-%dT%H:%MZ')
yesterday = now_time + datetime.timedelta(days=-2)
start_at = datetime.datetime.strftime(yesterday, '%Y-%m-%dT16:00Z')
return (start_at, end_at,)
def get_session_time_range():
u"""获取查询数据库session会话链接数时间范围."""
now_time = datetime.datetime.now()
end_at = datetime.datetime.strftime(now_time, '%Y-%m-%dT%H:%MZ')
yesterday = now_time + datetime.timedelta(days=-2)
start_at = datetime.datetime.strftime(yesterday, '%Y-%m-%dT16:00Z')
return (start_at, end_at,)
def describe_dbinstanceperformance_request():
u"""获取指定RDS会话链接数."""
(start_at, end_at) = get_time_range()
clt = client.AcsClient(access_key_id, access_key_secret, access_region)
request = DescribeDBInstancePerformanceRequest.\
DescribeDBInstancePerformanceRequest()
request.set_accept_format(accept_fomat)
request.set_action_name('DescribeDBInstancePerformance')
request.set_DBInstanceId(db_instanceid)
request.set_StartTime(start_at)
request.set_EndTime(end_at)
result = clt.do_action(request)
result = json.loads(result)
backup_list = result.get('Items', {}).get('Backup', None)
print backup_list
def describe_backups_request():
u"""获取指定RDS中互联网备份下载地址."""
(start_at, end_at) = get_time_range()
clt = client.AcsClient(access_key_id, access_key_secret, access_region)
request = DescribeBackupsRequest.DescribeBackupsRequest()
request.set_accept_format(accept_fomat)
request.set_action_name('DescribeBackups')
request.set_DBInstanceId(db_instanceid)
request.set_StartTime(start_at)
request.set_EndTime(end_at)
result = clt.do_action(request)
result = json.loads(result)
backup_list = result.get('Items', {}).get('Backup', None)
if backup_list is None:
print "Retrive [elc_db] database backup file failed."
sys.exit(1)
elif not backup_list:
print "Retrive [elc_db] database backup file failed."
sys.exit(1)
else:
last_backup = backup_list[0]
if last_backup.get('BackupType', None) in ['FullBackup']:
# 线上新增数据库后,此处需要同步修改
if last_backup.get('BackupDBNames', None) in ['elc_db,premium,xxl-job']:
if last_backup.get('BackupMethod') in ['Physical']:
if last_backup.get('BackupStatus') in ['Success']:
print last_backup.get('BackupDownloadURL')
sys.exit(0)
else:
print "Invalid BackupStatus [{0}].".format(
last_backup.get('BackupStatus', None))
sys.exit(1)
else:
print "Invalid BackupMethod [{0}].".format(
last_backup.get('BackupMethod', None))
sys.exit(1)
else:
print "Invalid BackupDBNames [{0}].".format(
last_backup.get('BackupDBNames', None))
sys.exit(1)
else:
print "Invalid BackupType [{0}].".format(
last_backup.get('BackupType', None))
sys.exit(1)
def describe_backups_intranet_request():
u"""获取指定RDS中阿里云内网备份下载地址."""
(start_at, end_at) = get_time_range()
clt = client.AcsClient(access_key_id, access_key_secret, access_region)
request = DescribeBackupsRequest.DescribeBackupsRequest()
request.set_accept_format(accept_fomat)
request.set_action_name('DescribeBackups')
request.set_DBInstanceId(db_instanceid)
request.set_StartTime(start_at)
request.set_EndTime(end_at)
result = clt.do_action(request)
result = json.loads(result)
backup_list = result.get('Items', {}).get('Backup', None)
if backup_list is None:
print "Retrive [elc_db] database backup file failed."
sys.exit(1)
elif not backup_list:
print "Retrive [elc_db] database backup file failed."
sys.exit(1)
else:
last_backup = backup_list[0]
if last_backup.get('BackupType', None) in ['FullBackup']:
# 线上新增数据库后,此处需要同步修改
if last_backup.get('BackupDBNames', None) in ['elc_db,premium,xxl-job']:
if last_backup.get('BackupMethod') in ['Physical']:
if last_backup.get('BackupStatus') in ['Success']:
print last_backup.get('BackupIntranetDownloadURL')
sys.exit(0)
else:
print "Invalid BackupStatus [{0}].".format(
last_backup.get('BackupStatus', None))
sys.exit(1)
else:
print "Invalid BackupMethod [{0}].".format(
last_backup.get('BackupMethod', None))
sys.exit(1)
else:
print "Invalid BackupDBNames [{0}].".format(
last_backup.get('BackupDBNames', None))
sys.exit(1)
else:
print "Invalid BackupType [{0}].".format(
last_backup.get('BackupType', None))
sys.exit(1)
def action_route(args):
u"""命令行参数解析后路由."""
if args.get('describe_backups_request'):
describe_backups_request()
elif args.get('describe_backups_intranet_request'):
describe_backups_intranet_request()
elif args.get('describe_dbinstanceperformance_request'):
describe_dbinstanceperformance_request()
if __name__ == '__main__':
args = docopt(__doc__, version='RDS CLI 1.0')
action_route(args)
- 数据库在内网的恢复
#!/bin/bash
set -o errexit
set -o nounset
backup_file_path=""
mysql_folder="/data/backup"
replace_file_folder="/root/sqls/no_delete"
tmp_sql_file="/tmp/system_options_update.sql"
rand_bit="xxxxxx"
update_key="sms_mc_strPwd sms_mw_strPwd sms_jweq_pwd sms_emay_send_key sms_mw_strPwd_m sms_jweq_pwd_m dx_security_key \
service_email_password service_sms_password service_mail_password register_code service_mail_account sms_mw_str_pwd"
exec_sql_file() {
/usr/local/mysql/bin/mysql -uelc_user -pxxxxxx elc_db < ${tmp_sql_file}
}
generate_sql_file() {
for key_name in ${update_key};do
if [[ "${key_name}" == "service_mail_account" ]];then
echo "update t_system_options set _value='[email protected]' where _key='${key_name}';" >> ${tmp_sql_file}
elif [[ "${key_name}" == "service_email_password" ]];then
echo "update t_system_options set _value='jjjjjjj' where _key='${key_name}';" >> ${tmp_sql_file}
else
echo "update t_system_options set _value='${rand_bit}' where _key='${key_name}';" >> ${tmp_sql_file}
fi
done
# 同意批量修改为10
echo "update t_system_options set _value=10 where _key like '%password%';" >> ${tmp_sql_file}
echo "update t_system_options set _value=10 where _key like '%pwd%';" >> ${tmp_sql_file}
echo "commit;" >> ${tmp_sql_file}
}
clear_file() {
if [[ -f "${tmp_sql_file}" ]];then
/bin/rm ${tmp_sql_file} -rf
fi
}
keep_security() {
clear_file
generate_sql_file
exec_sql_file
}
drop_old_user() {
/usr/local/mysql/bin/mysqladmin -uroot password 'xxxxxx'
/usr/local/mysql/bin/mysql -uroot -pxxxxxx <'root' and char_length(user)>0;
delete from mysql.tables_priv where user<>'root' and char_length(user)>0;
flush privileges;
create user 'elc_user'@'localhost' IDENTIFIED BY 'xxxxxx';
create user 'elc_user'@'%' IDENTIFIED BY 'xxxxxx';
grant all privileges on elc_db.* to 'elc_user'@'localhost' identified by 'xxxxxx';
grant all privileges on elc_db.* to 'elc_user'@'%' identified by 'xxxxxx';
flush privileges;
create user 'elc_rw'@'localhost' IDENTIFIED BY 'xxxxxx';
create user 'elc_rw'@'%' IDENTIFIED BY 'xxxxxx';
grant all privileges on elc_db.* to 'elc_rw'@'localhost' identified by 'xxxxxx';
grant all privileges on elc_db.* to 'elc_rw'@'%' identified by 'xxxxxx';
flush privileges;
EOF
}
start_mysql() {
cd ${mysql_folder}
/usr/bin/nohup /bin/sh /usr/local/mysql-5.6.23/bin/mysqld_safe --defaults-file=${mysql_folder}/my.cnf --user=mysql --datadir=${mysql_folder} >> mysql.nohup 2>&1 &
if [[ $? -eq 0 ]];then
echo "Start mysql success."
else
echo "Start mysql failed."
exit 1
fi
}
update_conf_file() {
/bin/cp -rpf ${replace_file_folder}/backup-my.cnf ${mysql_folder}/
/bin/cp -rpf ${replace_file_folder}/my.cnf ${mysql_folder}/
/bin/chown -R mysql:mysql ${mysql_folder}
}
restore_data_file() {
/usr/bin/innobackupex --defaults-file=${mysql_folder}/backup-my.cnf --apply-log ${mysql_folder}
if [[ $? -eq 0 ]];then
echo "Restore data file success."
else
echo "Restore data file failed."
exit 1
fi
}
extra_backup_file() {
/bin/rm ${mysql_folder} -rf
/bin/mkdir -p ${mysql_folder}
# /bin/bash /usr/local/bin/rds_backup_extract.sh -f /data/rds_archives/hins1719821_data_20180312005816.tar.gz -C ${mysql_folder}
/bin/bash /usr/local/bin/rds_backup_extract.sh -f ${backup_file_path} -C ${mysql_folder}
if [[ $? -eq 0 ]];then
echo "Extra backup file success."
else
echo "Extra backup file failed."
exit 1
fi
}
download_file() {
local download_url=$(/usr/local/bin/rds_utils.py describe_backups_request)
local saved_file_name=$(/bin/basename "`/bin/echo ${download_url} | /bin/awk -F'?' '{print $1}' | /bin/awk -F'https:/' '{print $2}'`")
backup_file_path="/data/rds_archives/${saved_file_name}"
# 支持断点续传
/usr/bin/wget -c "${download_url}" -O "${backup_file_path}"
if [[ $? -eq 0 ]];then
echo "Download backup file success."
else
echo "Download backup file failed."
exit 1
fi
}
stop_mysql() {
set +o errexit
# 停止Tomcat服务, 如果Tomcat已经停止,则忽略
# is_actived=`ps -ef | grep mysql | grep -v grep`
is_actived=`ps -ef | grep "/data/backup/my.cnf" | grep -v grep`
if [[ -n "${is_actived}" ]];then
# ps -ef | grep mysql | grep -v grep | awk '{print $2}' | xargs kill -9
ps -ef | grep "/data/backup/my.cnf"| grep -v grep | awk '{print $2}' | xargs kill -9
if [[ $? -eq 0 ]];then
echo "Kill -9 mysql_pid success."
else
echo "Kill -9 mysql_pid failed."
exit 1
fi
else
echo "Service mysql haved stopped before killed."
fi
set -o errexit
}
main() {
download_file
stop_mysql
extra_backup_file
restore_data_file
update_conf_file
start_mysql
sleep 60s
drop_old_user
keep_security
}
main
四、配置定时任务
[root@test65-mysql-node1 sqls]# crontab -l
00 07 * * * /bin/bash /root/sqls/download_db_backup_file.sh >> /var/log/rds_download.log 2>&1