From 41306ab505dce6a18faec57bc7ecd31028b5ad34 Mon Sep 17 00:00:00 2001 From: midoks Date: Thu, 7 Jul 2022 10:38:56 +0800 Subject: [PATCH] rememory --- plugins/mysql-ya/index.py | 4 +- plugins/mysql-ya/scripts/backup.py | 119 +++++++++++++++++++++++++++++ plugins/varnish/install.sh | 11 +++ route/static/app/crontab.js | 2 +- scripts/rememory.sh | 83 ++++++++++---------- 5 files changed, 172 insertions(+), 47 deletions(-) create mode 100755 plugins/mysql-ya/scripts/backup.py diff --git a/plugins/mysql-ya/index.py b/plugins/mysql-ya/index.py index bac8ba801..4a261d86c 100755 --- a/plugins/mysql-ya/index.py +++ b/plugins/mysql-ya/index.py @@ -624,9 +624,9 @@ def setDbBackup(): if not data[0]: return data[1] - scDir = mw.getRunDir() + '/scripts/backup.py' + scDir = getServerDir() + '/scripts/backup.py' - cmd = 'python ' + scDir + ' database ' + args['name'] + ' 3' + cmd = 'python3 ' + scDir + ' database ' + args['name'] + ' 3' os.system(cmd) return mw.returnJson(True, 'ok') diff --git a/plugins/mysql-ya/scripts/backup.py b/plugins/mysql-ya/scripts/backup.py new file mode 100755 index 000000000..b7207d9ab --- /dev/null +++ b/plugins/mysql-ya/scripts/backup.py @@ -0,0 +1,119 @@ +# coding: utf-8 +#----------------------------- +# 网站备份工具 +#----------------------------- + +import sys +import os + +if sys.platform != 'darwin': + os.chdir('/www/server/mdserver-web') + + +chdir = os.getcwd() +sys.path.append(chdir + '/class/core') + +# reload(sys) +# sys.setdefaultencoding('utf-8') + + +import mw +import db +import time + + +class backupTools: + + def backupDatabase(self, name, count): + db_path = mw.getServerDir() + '/mysql' + db_name = 'mysql' + name = mw.M('databases').dbPos(db_path, 'mysql').where( + 'name=?', (name,)).getField('name') + startTime = time.time() + if not name: + endDate = time.strftime('%Y/%m/%d %X', time.localtime()) + log = "数据库[" + name + "]不存在!" + print("★[" + endDate + "] " + log) + print( + "----------------------------------------------------------------------------") + return + + backup_path = mw.getRootDir() + '/backup/database' + if not os.path.exists(backup_path): + mw.execShell("mkdir -p " + backup_path) + + filename = backup_path + "/db_" + name + "_" + \ + time.strftime('%Y%m%d_%H%M%S', time.localtime()) + ".sql.gz" + + import re + mysql_root = mw.M('config').dbPos(db_path, db_name).where( + "id=?", (1,)).getField('mysql_root') + + mycnf = mw.readFile(db_path + '/etc/my.cnf') + rep = "\[mysqldump\]\nuser=root" + sea = "[mysqldump]\n" + subStr = sea + "user=root\npassword=" + mysql_root + "\n" + mycnf = mycnf.replace(sea, subStr) + if len(mycnf) > 100: + mw.writeFile(db_path + '/etc/my.cnf', mycnf) + + mw.execShell( + db_path + "/bin/mysqldump --opt --default-character-set=utf8 " + name + " | gzip > " + filename) + + if not os.path.exists(filename): + endDate = time.strftime('%Y/%m/%d %X', time.localtime()) + log = "数据库[" + name + "]备份失败!" + print("★[" + endDate + "] " + log) + print( + "----------------------------------------------------------------------------") + return + + mycnf = mw.readFile(db_path + '/etc/my.cnf') + mycnf = mycnf.replace(subStr, sea) + if len(mycnf) > 100: + mw.writeFile(db_path + '/etc/my.cnf', mycnf) + + endDate = time.strftime('%Y/%m/%d %X', time.localtime()) + outTime = time.time() - startTime + pid = mw.M('databases').dbPos(db_path, db_name).where( + 'name=?', (name,)).getField('id') + + mw.M('backup').add('type,name,pid,filename,addtime,size', (1, os.path.basename( + filename), pid, filename, endDate, os.path.getsize(filename))) + log = "数据库[" + name + "]备份成功,用时[" + str(round(outTime, 2)) + u"]秒" + mw.writeLog('计划任务', log) + print("★[" + endDate + "] " + log) + print("|---保留最新的[" + count + "]份备份") + print("|---文件名:" + filename) + + # 清理多余备份 + backups = mw.M('backup').where( + 'type=? and pid=?', ('1', pid)).field('id,filename').select() + + num = len(backups) - int(count) + if num > 0: + for backup in backups: + mw.execShell("rm -f " + backup['filename']) + mw.M('backup').where('id=?', (backup['id'],)).delete() + num -= 1 + print("|---已清理过期备份文件:" + backup['filename']) + if num < 1: + break + + def backupDatabaseAll(self, save): + db_path = mw.getServerDir() + '/mysql' + db_name = 'mysql' + databases = mw.M('databases').dbPos( + db_path, db_name).field('name').select() + for database in databases: + self.backupDatabase(database['name'], save) + + +if __name__ == "__main__": + backup = backupTools() + type = sys.argv[1] + elif type == 'database': + if sys.argv[2] == 'ALL': + backup.backupDatabaseAll(sys.argv[3]) + else: + backup.backupDatabase(sys.argv[2], sys.argv[3]) diff --git a/plugins/varnish/install.sh b/plugins/varnish/install.sh index 5a19fc95a..63689d188 100755 --- a/plugins/varnish/install.sh +++ b/plugins/varnish/install.sh @@ -60,6 +60,17 @@ Uninstall_varnish() cd ${rootPath} && python3 ${rootPath}/plugins/varnish/index.py stop cd ${rootPath} && python3 ${rootPath}/plugins/varnish/index.py initd_uninstall + if [ ${OSNAME} == "macos" ]; then + brew uninstall varnish + elif [ ${OSNAME} == "centos" ]; then + yum remove varnish -y + elif [ ${OSNAME} == "debian" ] || [ ${OSNAME} == "ubuntu" ]; then + apt remove varnish -y + elif [ ${OSNAME} == "opensuse" ];then + zypper remove -y varnish + else + echo "I won't support it" + fi rm -rf $serverPath/varnish echo "uninstall varnish" > $install_tmp } diff --git a/route/static/app/crontab.js b/route/static/app/crontab.js index 01f6473c3..459e65d50 100755 --- a/route/static/app/crontab.js +++ b/route/static/app/crontab.js @@ -657,7 +657,7 @@ function editTaskInfo(id){ \
\ 提示\ -
释放PHP、MYSQL、PURE-FTPD、APACHE、NGINX的内存占用,建议在每天半夜执行!
\ +
释放PHP、MYSQL、PURE-FTPD、OpenResty的内存占用,建议在每天半夜执行!
\
\
\ URL地址\ diff --git a/scripts/rememory.sh b/scripts/rememory.sh index 1e09ee1d6..a971110ce 100755 --- a/scripts/rememory.sh +++ b/scripts/rememory.sh @@ -20,58 +20,53 @@ else echo 'do start!' fi -if [ -f $rootPath"/php/init.d/php52" ];then - $rootPath"/php/init.d/php52" reload -fi - - -if [ -f $rootPath"/php/init.d/php53" ];then - $rootPath"/php/init.d/php53" reload -fi - -if [ -f $rootPath"/php/init.d/php54" ];then - $rootPath"/php/init.d/php54" reload -fi - -if [ -f $rootPath"/php/init.d/php55" ];then - $rootPath"/php/init.d/php55" reload -fi - -if [ -f $rootPath"/php/init.d/php56" ];then - $rootPath"/php/init.d/php56" reload -fi - -if [ -f $rootPath"/php/init.d/php70" ];then - $rootPath"/php/init.d/php70" reload -fi - -if [ -f $rootPath"/php/init.d/php71" ];then - $rootPath"/php/init.d/php71" reload -fi -if [ -f $rootPath"/php/init.d/php72" ];then - $rootPath"/php/init.d/php72" reload +echo "OpenResty -- START" +if [ -f /usr/lib/systemd/system/openresty.service ];then + systemctl reload openresty +elif [ -f $rootPath/openresty/nginx/sbin/nginx ];then + $rootPath/openresty/nginx/sbin/nginx -s reload +else + echo "..." +fi +echo "OpenResty -- END" + + +PHP_VER_LIST=(53 54 55 56 70 71 72 73 74 80 81) +for PHP_VER in ${PHP_VER_LIST[@]}; do +echo "PHP${PHP_VER} -- START" +if [ -f /usr/lib/systemd/system/php${PHP_VER}.service ];then + systemctl reload php${PHP_VER} +elif [ -f ${rootPath}/php/init.d/php${PHP_VER} ];then + ${rootPath}/php/init.d/php${PHP_VER} reload +else + echo "..." fi - -if [ -f $rootPath"/php/init.d/php73" ];then - $rootPath"/php/init.d/php73" reload +echo "PHP${PHP_VER} -- END" +done + +echo "MySQL -- START" +if [ -f /usr/lib/systemd/system/mysql.service ];then + systemctl reload mysql +elif [ -f ${rootPath}/php/init.d/mysql ];then + ${rootPath}/mysql/init.d/mysql reload +else + echo "..." fi +echo "MySQL -- END" -if [ -f $rootPath"/php/init.d/php74" ];then - $rootPath"/php/init.d/php74" reload -fi -if [ -f $rootPath"/php/init.d/php80" ];then - $rootPath"/php/init.d/php74" reload -fi -if [ -f $rootPath"/php/init.d/php81" ];then - $rootPath"/php/init.d/php74" reload +echo "PureFTPD -- START" +if [ -f /usr/lib/systemd/system/pureftp.service ];then + systemctl reload pureftp +elif [ -f ${rootPath}/pureftp/init.d/pureftp ];then + ${rootPath}/pureftp/init.d/pureftp reload +else + echo "..." fi +echo "PureFTPD -- END" -if [ -f $rootPath"/openresty/nginx/sbin/nginx" ];then - $rootPath"/openresty/nginx/sbin/nginx" -s reload -fi sync sleep 2