数据库备份任务和查看接口
This commit is contained in:
parent
2badb9c45c
commit
b999ef60b3
|
@ -5,6 +5,8 @@ from apps.monitor.models import DrfRequestLog
|
||||||
from apps.utils.tasks import CustomTask
|
from apps.utils.tasks import CustomTask
|
||||||
from celery import shared_task
|
from celery import shared_task
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from django.conf import settings
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
@shared_task(base=CustomTask)
|
@shared_task(base=CustomTask)
|
||||||
|
@ -16,3 +18,18 @@ def clear_drf_log(days: int = 7):
|
||||||
now = timezone.now()
|
now = timezone.now()
|
||||||
days7_ago = now - timedelta(days=days)
|
days7_ago = now - timedelta(days=days)
|
||||||
DrfRequestLog.objects.filter(create_time__lte=days7_ago).delete()
|
DrfRequestLog.objects.filter(create_time__lte=days7_ago).delete()
|
||||||
|
|
||||||
|
@shared_task(base=CustomTask)
|
||||||
|
def clear_dbbackup(num: int=7):
|
||||||
|
"""
|
||||||
|
清除N条前的数据库备份记录,默认七条
|
||||||
|
|
||||||
|
清除N条前的数据库备份记录
|
||||||
|
"""
|
||||||
|
from apps.monitor.views import get_file_list
|
||||||
|
backpath = settings.BACKUP_PATH + '/database'
|
||||||
|
files = get_file_list(backpath)
|
||||||
|
files_remove_list = files[6:]
|
||||||
|
for f in files_remove_list:
|
||||||
|
filepath = os.path.join(backpath, f)
|
||||||
|
os.remove(filepath)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
from .views import DrfRequestLogViewSet, ServerInfoView, LogView, LogDetailView, index, room, video
|
from .views import DrfRequestLogViewSet, ServerInfoView, LogView, LogDetailView, index, room, video, DbBackupView
|
||||||
|
|
||||||
API_BASE_URL = 'api/monitor/'
|
API_BASE_URL = 'api/monitor/'
|
||||||
HTML_BASE_URL = 'monitor/'
|
HTML_BASE_URL = 'monitor/'
|
||||||
|
@ -11,6 +11,7 @@ urlpatterns = [
|
||||||
|
|
||||||
path(API_BASE_URL + 'log/', LogView.as_view()),
|
path(API_BASE_URL + 'log/', LogView.as_view()),
|
||||||
path(API_BASE_URL + 'log/<str:name>/', LogDetailView.as_view()),
|
path(API_BASE_URL + 'log/<str:name>/', LogDetailView.as_view()),
|
||||||
|
path(API_BASE_URL + 'dbbackup', DbBackupView.as_view()),
|
||||||
path(API_BASE_URL + 'server/', ServerInfoView.as_view()),
|
path(API_BASE_URL + 'server/', ServerInfoView.as_view()),
|
||||||
path(API_BASE_URL + 'request_log/', DrfRequestLogViewSet.as_view({'get': 'list'}), name='requestlog_view')
|
path(API_BASE_URL + 'request_log/', DrfRequestLogViewSet.as_view({'get': 'list'}), name='requestlog_view')
|
||||||
]
|
]
|
||||||
|
|
|
@ -127,6 +127,60 @@ class LogDetailView(APIView):
|
||||||
raise NotFound(**LOG_NOT_FONED)
|
raise NotFound(**LOG_NOT_FONED)
|
||||||
|
|
||||||
|
|
||||||
|
class DbBackupView(APIView):
|
||||||
|
perms_map = {'get': '*', 'delete': 'dbback.delete'}
|
||||||
|
|
||||||
|
def delete(self, request):
|
||||||
|
"""
|
||||||
|
删除备份
|
||||||
|
|
||||||
|
删除备份
|
||||||
|
"""
|
||||||
|
names = request.data.get('names', [])
|
||||||
|
for i in names:
|
||||||
|
fullpath = settings.BACKUP_PATH + '/database/' + i
|
||||||
|
os.remove(fullpath)
|
||||||
|
return Response()
|
||||||
|
|
||||||
|
@swagger_auto_schema(manual_parameters=[
|
||||||
|
openapi.Parameter('name', openapi.IN_QUERY,
|
||||||
|
description='文件名', type=openapi.TYPE_STRING)
|
||||||
|
])
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
查看最近的备份列表
|
||||||
|
|
||||||
|
查看最近的备份列表
|
||||||
|
"""
|
||||||
|
items = []
|
||||||
|
name = request.GET.get('name', None)
|
||||||
|
# for root, dirs, files in os.walk(settings.LOG_PATH):
|
||||||
|
# files.reverse()
|
||||||
|
backpath = settings.BACKUP_PATH + '/database'
|
||||||
|
for file in get_file_list(backpath):
|
||||||
|
if len(items) > 50:
|
||||||
|
break
|
||||||
|
filepath = os.path.join(backpath, file)
|
||||||
|
if name:
|
||||||
|
if name in filepath:
|
||||||
|
fsize = os.path.getsize(filepath)
|
||||||
|
if fsize:
|
||||||
|
items.append({
|
||||||
|
"name": file,
|
||||||
|
"filepath": filepath,
|
||||||
|
"size": round(fsize/1024/1024, 1)
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
fsize = os.path.getsize(filepath)
|
||||||
|
if fsize:
|
||||||
|
items.append({
|
||||||
|
"name": file,
|
||||||
|
"filepath": filepath,
|
||||||
|
"size": round(fsize/1024/1024, 1)
|
||||||
|
})
|
||||||
|
return Response(items)
|
||||||
|
|
||||||
|
|
||||||
class DrfRequestLogSerializer(serializers.ModelSerializer):
|
class DrfRequestLogSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = DrfRequestLog
|
model = DrfRequestLog
|
||||||
|
|
Loading…
Reference in New Issue