Skip to content

Commit

Permalink
feat: 计划任务备份应用/网站/数据库支持多选 (1Panel-dev#5849)
Browse files Browse the repository at this point in the history
  • Loading branch information
john1298308460 authored Jul 17, 2024
1 parent cbb1191 commit f6c334b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 31 deletions.
61 changes: 36 additions & 25 deletions backend/app/service/cronjob_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ import (

func (u *CronjobService) handleApp(cronjob model.Cronjob, startTime time.Time) error {
var apps []model.AppInstall
if cronjob.AppID == "all" {
if strings.Contains(cronjob.AppID, "all") {
apps, _ = appInstallRepo.ListBy()
} else {
itemID, _ := strconv.Atoi(cronjob.AppID)
app, err := appInstallRepo.GetFirst(commonRepo.WithByID(uint(itemID)))
appIds := strings.Split(cronjob.AppID, ",")
var idItems []uint
for i := 0; i < len(appIds); i++ {
itemID, _ := strconv.Atoi(appIds[i])
idItems = append(idItems, uint(itemID))
}
appItems, err := appInstallRepo.ListBy(commonRepo.WithIdsIn(idItems))
if err != nil {
return err
}
apps = append(apps, app)
apps = appItems
}
accountMap, err := loadClientMap(cronjob.BackupAccounts)
if err != nil {
Expand Down Expand Up @@ -232,7 +237,7 @@ type databaseHelper struct {

func loadDbsForJob(cronjob model.Cronjob) []databaseHelper {
var dbs []databaseHelper
if cronjob.DBName == "all" {
if strings.Contains(cronjob.DBName, "all") {
if cronjob.DBType == "mysql" || cronjob.DBType == "mariadb" {
mysqlItems, _ := mysqlRepo.List()
for _, mysql := range mysqlItems {
Expand All @@ -254,36 +259,42 @@ func loadDbsForJob(cronjob model.Cronjob) []databaseHelper {
}
return dbs
}
itemID, _ := strconv.Atoi(cronjob.DBName)
if cronjob.DBType == "mysql" || cronjob.DBType == "mariadb" {
mysqlItem, _ := mysqlRepo.Get(commonRepo.WithByID(uint(itemID)))
dbs = append(dbs, databaseHelper{
DBType: cronjob.DBType,
Database: mysqlItem.MysqlName,
Name: mysqlItem.Name,
})
} else {
pgItem, _ := postgresqlRepo.Get(commonRepo.WithByID(uint(itemID)))
dbs = append(dbs, databaseHelper{
DBType: cronjob.DBType,
Database: pgItem.PostgresqlName,
Name: pgItem.Name,
})

dbNames := strings.Split(cronjob.DBName, ",")
for _, name := range dbNames {
itemID, _ := strconv.Atoi(name)
if cronjob.DBType == "mysql" || cronjob.DBType == "mariadb" {
mysqlItem, _ := mysqlRepo.Get(commonRepo.WithByID(uint(itemID)))
dbs = append(dbs, databaseHelper{
DBType: cronjob.DBType,
Database: mysqlItem.MysqlName,
Name: mysqlItem.Name,
})
} else {
pgItem, _ := postgresqlRepo.Get(commonRepo.WithByID(uint(itemID)))
dbs = append(dbs, databaseHelper{
DBType: cronjob.DBType,
Database: pgItem.PostgresqlName,
Name: pgItem.Name,
})
}
}
return dbs
}

func loadWebsForJob(cronjob model.Cronjob) []model.Website {
var weblist []model.Website
if cronjob.Website == "all" {
if strings.Contains(cronjob.Website, "all") {
weblist, _ = websiteRepo.List()
return weblist
}
itemID, _ := strconv.Atoi(cronjob.Website)
webItem, _ := websiteRepo.GetFirst(commonRepo.WithByID(uint(itemID)))
if webItem.ID != 0 {
weblist = append(weblist, webItem)
websites := strings.Split(cronjob.Website, ",")
var idItems []uint
for i := 0; i < len(websites); i++ {
itemID, _ := strconv.Atoi(websites[i])
idItems = append(idItems, uint(itemID))
}
weblist, _ = websiteRepo.List(commonRepo.WithIdsIn(idItems))
return weblist
}

Expand Down
3 changes: 3 additions & 0 deletions frontend/src/api/interface/cronjob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export namespace Cronjob {
backupAccounts: string;
defaultDownload: string;
backupAccountList: Array<string>;
appIdList: Array<string>;
websiteList: Array<string>;
dbNameList: Array<string>;
retainCopies: number;
status: string;
secret: string;
Expand Down
43 changes: 37 additions & 6 deletions frontend/src/views/cronjob/operate/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@
<el-form-item
v-if="dialogData.rowData!.type === 'website' || dialogData.rowData!.type === 'cutWebsiteLog'"
:label="dialogData.rowData!.type === 'website' ? $t('cronjob.website'):$t('website.website')"
prop="website"
prop="websiteList"
>
<el-select class="selectClass" v-model="dialogData.rowData!.website">
<el-select class="selectClass" multiple v-model="dialogData.rowData!.websiteList">
<el-option
:disabled="websiteOptions.length === 0"
:label="$t('commons.table.all')"
Expand All @@ -212,8 +212,8 @@
</el-form-item>

<div v-if="dialogData.rowData!.type === 'app'">
<el-form-item :label="$t('cronjob.app')" prop="appID">
<el-select class="selectClass" clearable v-model="dialogData.rowData!.appID">
<el-form-item :label="$t('cronjob.app')" prop="appIdList">
<el-select class="selectClass" multiple clearable v-model="dialogData.rowData!.appIdList">
<el-option
:disabled="appOptions.length === 0"
:label="$t('commons.table.all')"
Expand All @@ -239,8 +239,8 @@
<el-radio value="postgresql">PostgreSQL</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item :label="$t('cronjob.database')" prop="dbName">
<el-select class="selectClass" clearable v-model="dialogData.rowData!.dbName">
<el-form-item :label="$t('cronjob.database')" prop="dbNameList">
<el-select class="selectClass" multiple clearable v-model="dialogData.rowData!.dbNameList">
<el-option
:disabled="dbInfo.dbs.length === 0"
:label="$t('commons.table.all')"
Expand Down Expand Up @@ -378,13 +378,15 @@ import { listContainer } from '@/api/modules/container';
import { Database } from '@/api/interface/database';
import { ListAppInstalled } from '@/api/modules/app';
import { loadDefaultSpec, specOptions, transObjToSpec, transSpecToObj, weekOptions } from './../helper';
const router = useRouter();
interface DialogProps {
title: string;
rowData?: Cronjob.CronjobInfo;
getTableList?: () => Promise<any>;
}
const title = ref<string>('');
const drawerVisible = ref(false);
const dialogData = ref<DialogProps>({
Expand All @@ -408,6 +410,15 @@ const acceptParams = (params: DialogProps): void => {
if (dialogData.value.rowData.backupAccounts) {
dialogData.value.rowData.backupAccountList = dialogData.value.rowData.backupAccounts.split(',');
}
if (dialogData.value.rowData.appID) {
dialogData.value.rowData.appIdList = dialogData.value.rowData.appID.split(',');
}
if (dialogData.value.rowData.website) {
dialogData.value.rowData.websiteList = dialogData.value.rowData.website.split(',');
}
if (dialogData.value.rowData.dbName) {
dialogData.value.rowData.dbNameList = dialogData.value.rowData.dbName.split(',');
}
dialogData.value.rowData!.command = dialogData.value.rowData!.command || 'sh';
dialogData.value.rowData!.isCustom =
dialogData.value.rowData!.command !== 'sh' &&
Expand Down Expand Up @@ -718,6 +729,17 @@ const onSubmit = async (formEl: FormInstance | undefined) => {
specs.push(itemSpec);
}
dialogData.value.rowData.backupAccounts = dialogData.value.rowData.backupAccountList.join(',');
if (dialogData.value.rowData.appIdList) {
dialogData.value.rowData.appID = dialogData.value.rowData.appIdList.join(',');
}
if (dialogData.value.rowData.websiteList) {
dialogData.value.rowData.website = dialogData.value.rowData.websiteList.join(',');
}
if (dialogData.value.rowData.dbNameList) {
dialogData.value.rowData.dbName = dialogData.value.rowData.dbNameList.join(',');
}
dialogData.value.rowData.spec = specs.join(',');
if (!formEl) return;
formEl.validate(async (valid) => {
Expand Down Expand Up @@ -750,40 +772,49 @@ defineExpose({
.specClass {
width: 20% !important;
margin-left: 20px;
.append {
width: 20px;
}
}
@media only screen and (max-width: 1000px) {
.specClass {
width: 100% !important;
margin-top: 20px;
margin-left: 0;
.append {
width: 43px;
}
}
}
.specTypeClass {
width: 22% !important;
}
@media only screen and (max-width: 1000px) {
.specTypeClass {
width: 100% !important;
}
}
.selectClass {
width: 100%;
}
.tagClass {
float: right;
margin-right: 10px;
font-size: 12px;
margin-top: 5px;
}
.logText {
line-height: 22px;
font-size: 12px;
.link {
font-size: 12px;
margin-top: -3px;
Expand Down

0 comments on commit f6c334b

Please sign in to comment.