宝塔或aapanel备份的时候,有时候不想备份runtime或public目录中一些缓存垃圾,或指定扩展名的日志文件等
官方原本没有这个功能,我们DIY做一个,改几行dy代码。
环境:当前修改时 aapanel 版本 为 7.0.21
共改造2个文件
修改第一处文件位置 ToBackup 就是站点列表那里的备份功能
/www/server/panel/class_v2/panel_site_v2.py
找到def ToBackup所在内容,替换为如下:
# 打包
def ToBackup(self, get):
# 校验参数
try:
get.validate([
Param('id').Integer(),
], [
public.validate.trim_filter(),
])
except Exception as ex:
public.print_log("error info: {}".format(ex))
return public.return_message(-1, 0, str(ex))
id = get.id
find = public.M('sites').where("id=?", (id,)).field('name,path,id').find()
import time
fileName = find['name'] + '_' + time.strftime('%Y%m%d_%H%M%S', time.localtime()) + '.zip'
backupPath = session['config']['backup_path'] + '/site'
zipName = backupPath + '/' + fileName
if not (os.path.exists(backupPath)): os.makedirs(backupPath)
tmps = '/tmp/panelExec.log'
ruleFilepath=find['path']+'/backuprule.txt'
rules="'.user.ini' '*/.user.ini'"
if os.path.exists(ruleFilepath):
rules = public.readFile(ruleFilepath)
rules=rules.replace("\r\n", "\n")
words=rules.split('\n')
# 使用split方法将文本按行分割,然后使用列表推导式添加单引号
lines_with_quotes = [f"'{lineword.strip()}'" for lineword in words if lineword.strip()]
# 重新组合成字符串
rules = '\n'.join(lines_with_quotes)
rules = re.sub(r'\s+', ' ', rules)
rules = rules.replace('\n', " ")
execStr = "cd '" + find['path'] + "' && zip '" + zipName + "' -x " + rules + " -r ./ > " + tmps + " 2>&1"
outStr=execStr
public.ExecShell(execStr)
sql = public.M('backup').add('type,name,pid,filename,size,addtime',
(0, fileName, find['id'], zipName, 0, public.getDate()))
public.write_log_gettext('Site manager', 'Backup site [{}] succeed!', (find['name'],))
return_message = public.return_msg_gettext(True, "Backup Succeeded!!" + outStr)
del return_message['status']
return public.return_message(0, 0, return_message['msg'])
修改第二个文件位置 backup_site,自动备份任务那边
/www/server/panel/class_v2/panel_task_v2.py
找到def backup_site所在内容,替换为如下:
# 备份网站
def backup_site(self, id, log_file):
find = public.M('sites').where(
"id=?", (id,)).field('name,path,id').find()
fileName = find['name']+'_' + \
time.strftime('%Y%m%d_%H%M%S', time.localtime())+'.zip'
backupPath = public.M('config').where(
'id=?', (1,)).getField('backup_path') + '/site'
zipName = backupPath + '/'+fileName
if not (os.path.exists(backupPath)):
os.makedirs(backupPath)
ruleFilepath=find['path']+'/backuprule.txt'
rules="'.user.ini' '*/.user.ini'"
if os.path.exists(ruleFilepath):
rules = public.readFile(ruleFilepath)
rules=rules.replace("\r\n", "\n")
words=rules.split('\n')
# 使用split方法将文本按行分割,然后使用列表推导式添加单引号
lines_with_quotes = [f"'{lineword.strip()}'" for lineword in words if lineword.strip()]
# 重新组合成字符串
rules = '\n'.join(lines_with_quotes)
rules = re.sub(r'\s+', ' ', rules)
rules = rules.replace('\n', " ")
execStr = "cd '" + find['path'] + "' && zip '" + \
zipName + "' -x " + rules + " -r ./ &> " + log_file
public.ExecShell(execStr)
sql = public.M('backup').add('type,name,pid,filename,size,addtime',
(0, fileName, find['id'], zipName, 0, public.getDate()))
public.WriteLog('TYPE_SITE', 'SITE_BACKUP_SUCCESS', (find['name'],),not_web = self.not_web)
return public.return_msg_gettext(True, public.lang("Backup Succeeded!backup_site:"+execStr))
这样修改完了以后,
在待备份站点根目录下创建排除规则文件,允许\r\n或\n换行,每行一条
backuprule.txt
规则内容如下:
.user.ini
*/.user.ini
./runtime/*
./public/upload
./public/uploads
*/*.log
备份试试看吧!