Basic app backup import is working
This commit is contained in:
@@ -1,30 +1,216 @@
|
||||
<script setup>
|
||||
|
||||
import { ref, useTemplateRef } from 'vue';
|
||||
import { Dialog } from 'pankow';
|
||||
import BackupProviderForm from './BackupProviderForm.vue';
|
||||
import { Dialog, FormGroup, TextInput } from 'pankow';
|
||||
import { s3like } from '../utils.js';
|
||||
import BackupProviderForm from './BackupProviderForm.vue';
|
||||
import AppsModel from '../models/AppsModel.js';
|
||||
|
||||
const appsModel = AppsModel.create();
|
||||
|
||||
const dialog = useTemplateRef('dialog');
|
||||
const id = ref('');
|
||||
const backupConfigInput = useTemplateRef('backupConfigInput');
|
||||
const appId = ref('');
|
||||
const busy = ref(false);
|
||||
const formError = ref({});
|
||||
const providerConfig = ref({});
|
||||
const provider = ref('');
|
||||
const remotePath = ref('');
|
||||
|
||||
async function onSubmit() {
|
||||
formError.value = {};
|
||||
busy.value = true;
|
||||
|
||||
const backupConfig = {
|
||||
provider: provider.value,
|
||||
};
|
||||
|
||||
if (providerConfig.value.encryptionPassword) {
|
||||
backupConfig.password = providerConfig.value.encryptionPassword;
|
||||
backupConfig.encryptedFilenames = providerConfig.value.encryptedFilenames;
|
||||
}
|
||||
|
||||
const format = providerConfig.value.format;
|
||||
let backupId = remotePath.value;
|
||||
|
||||
// only set provider specific fields, this will clear them in the db
|
||||
if (s3like(provider.value)) {
|
||||
backupConfig.bucket = providerConfig.value.bucket;
|
||||
backupConfig.prefix = providerConfig.value.prefix;
|
||||
backupConfig.accessKeyId = providerConfig.value.accessKeyId;
|
||||
backupConfig.secretAccessKey = providerConfig.value.secretAccessKey;
|
||||
|
||||
if (providerConfig.value.endpoint) backupConfig.endpoint = providerConfig.value.endpoint;
|
||||
|
||||
if (provider.value === 's3') {
|
||||
if (providerConfig.value.region) backupConfig.region = providerConfig.value.region;
|
||||
delete backupConfig.endpoint;
|
||||
} else if (provider.value === 'minio' || provider.value === 's3-v4-compat') {
|
||||
backupConfig.region = providerConfig.value.region || 'us-east-1';
|
||||
backupConfig.acceptSelfSignedCerts = !!providerConfig.value.acceptSelfSignedCerts;
|
||||
backupConfig.s3ForcePathStyle = true; // might want to expose this in the UI
|
||||
} else if (provider.value === 'exoscale-sos') {
|
||||
backupConfig.region = 'us-east-1';
|
||||
backupConfig.signatureVersion = 'v4';
|
||||
} else if (provider.value === 'wasabi') {
|
||||
backupConfig.region = providerConfig.value.endpoint;
|
||||
backupConfig.signatureVersion = 'v4';
|
||||
} else if (provider.value === 'scaleway-objectstorage') {
|
||||
backupConfig.region = providerConfig.value.endpoint;
|
||||
backupConfig.signatureVersion = 'v4';
|
||||
} else if (provider.value === 'linode-objectstorage') {
|
||||
backupConfig.region = providerConfig.value.endpoint;
|
||||
backupConfig.signatureVersion = 'v4';
|
||||
} else if (provider.value === 'ovh-objectstorage') {
|
||||
backupConfig.region = providerConfig.value.endpoint;
|
||||
backupConfig.signatureVersion = 'v4';
|
||||
} else if (provider.value === 'ionos-objectstorage') {
|
||||
backupConfig.region = providerConfig.value.endpoint;
|
||||
backupConfig.signatureVersion = 'v4';
|
||||
} else if (provider.value === 'vultr-objectstorage') {
|
||||
backupConfig.region = providerConfig.value.endpoint;
|
||||
backupConfig.signatureVersion = 'v4';
|
||||
} else if (provider.value === 'contabo-objectstorage') {
|
||||
backupConfig.region = providerConfig.value.endpoint;
|
||||
backupConfig.signatureVersion = 'v4';
|
||||
backupConfig.s3ForcePathStyle = true; // https://docs.contabo.com/docs/products/Object-Storage/technical-description (no virtual buckets)
|
||||
} else if (provider.value === 'upcloud-objectstorage') {
|
||||
const m = /^.*\.(.*)\.upcloudobjects.com$/.exec(providerConfig.value.endpoint);
|
||||
backupConfig.region = m ? m[1] : 'us-east-1'; // let it fail in validation phase if m is not valid
|
||||
backupConfig.signatureVersion = 'v4';
|
||||
} else if (provider.value === 'digitalocean-spaces') {
|
||||
backupConfig.region = 'us-east-1';
|
||||
} else if (provider.value === 'hetzner-objectstorage') {
|
||||
backupConfig.region = 'us-east-1';
|
||||
backupConfig.signatureVersion = 'v4';
|
||||
}
|
||||
} else if (provider.value === 'gcs') {
|
||||
// TODO test gcs import
|
||||
backupConfig.bucket = providerConfig.value.bucket;
|
||||
backupConfig.prefix = providerConfig.value.prefix;
|
||||
try {
|
||||
var serviceAccountKey = JSON.parse(providerConfig.value.gcsKey.content);
|
||||
backupConfig.projectId = serviceAccountKey.project_id;
|
||||
backupConfig.credentials = {
|
||||
client_email: serviceAccountKey.client_email,
|
||||
private_key: serviceAccountKey.private_key
|
||||
};
|
||||
|
||||
if (!backupConfig.projectId || !backupConfig.credentials || !backupConfig.credentials.client_email || !backupConfig.credentials.private_key) {
|
||||
throw 'fields_missing';
|
||||
}
|
||||
} catch (e) {
|
||||
formError.value.generic = 'Cannot parse Google Service Account Key: ' + e.message;
|
||||
formError.value.gcsKeyInput = true;
|
||||
busy.value = false;
|
||||
return;
|
||||
}
|
||||
} else if (provider.value === 'sshfs' || provider.value === 'cifs' || provider.value === 'nfs' || provider.value === 'ext4' || provider.value === 'xfs') {
|
||||
backupConfig.mountOptions = providerConfig.value.mountOptions;
|
||||
backupConfig.prefix = providerConfig.value.prefix;
|
||||
} else if (provider.value === 'mountpoint') {
|
||||
backupConfig.prefix = providerConfig.value.prefix;
|
||||
backupConfig.mountPoint = providerConfig.value.mountPoint;
|
||||
} else if (provider.value === 'filesystem') {
|
||||
const parts = remotePath.value.split('/');
|
||||
backupId = parts.pop() || parts.pop(); // removes any trailing slash. this is basename()
|
||||
backupConfig.backupFolder = parts.join('/'); // this is dirname()
|
||||
}
|
||||
|
||||
if (format === 'tgz') {
|
||||
if (backupId.substring(backupId.length - '.tar.gz'.length, backupId.length) === '.tar.gz') { // endsWith
|
||||
backupId = backupId.replace(/.tar.gz$/, '');
|
||||
} else if (backupId.substring(backupId.length - '.tar.gz.enc'.length, backupId.length) === '.tar.gz.enc') { // endsWith
|
||||
backupId = backupId.replace(/.tar.gz.enc$/, '');
|
||||
}
|
||||
}
|
||||
|
||||
const [error] = await appsModel.import(appId.value, backupId, format, backupConfig);
|
||||
if (error) {
|
||||
busy.value = false;
|
||||
formError.value = {};
|
||||
|
||||
if (error.status === 424) {
|
||||
formError.value.generic = error.body.message;
|
||||
|
||||
if (error.body.message.indexOf('AWS Access Key Id') !== -1) {
|
||||
formError.value.accessKeyId = true;
|
||||
} else if (error.body.message.indexOf('not match the signature') !== -1 || error.body.message.indexOf('Signature') !== -1) {
|
||||
formError.value.secretAccessKey = true;
|
||||
} else if (error.body.message.toLowerCase() === 'access denied') {
|
||||
formError.value.accessKeyId = true;
|
||||
} else if (error.body.message.indexOf('ECONNREFUSED') !== -1) {
|
||||
formError.value.generic = 'Unknown region';
|
||||
formError.value.region = true;
|
||||
} else if (error.body.message.toLowerCase() === 'wrong region') {
|
||||
formError.value.generic = 'Wrong S3 Region';
|
||||
formError.value.region = true;
|
||||
} else {
|
||||
formError.value.bucket = true;
|
||||
}
|
||||
} else if (error.status === 400) {
|
||||
formError.value.generic = error.body.message;
|
||||
|
||||
if (provider.value === 'filesystem') {
|
||||
formError.value.backupFolder = true;
|
||||
}
|
||||
} else {
|
||||
formError.value.generic = 'Internal error';
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
dialog.value.close();
|
||||
busy.value = false;
|
||||
formError.value = {};
|
||||
|
||||
// clear potential post-install flag
|
||||
delete localStorage['confirmPostInstall_' + appId.value];
|
||||
}
|
||||
|
||||
function onBackupConfigChanged(event) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function (result) {
|
||||
if (!result.target || !result.target.result) return console.error('Unable to read backup config');
|
||||
|
||||
let config;
|
||||
try {
|
||||
config = JSON.parse(result.target.result);
|
||||
if (config.provider === 'filesystem') { // this allows a user to upload a backup to server and import easily with an absolute path
|
||||
config.remotePath = config.backupFolder + '/' + config.remotePath;
|
||||
delete config.backupFolder;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Unable to parse backup config', e);
|
||||
return;
|
||||
}
|
||||
|
||||
provider.value = config.provider;
|
||||
remotePath.value = config.remotePath;
|
||||
|
||||
// we assume property names match here, this does not yet work for gcs keys
|
||||
Object.keys(config).forEach(function (k) {
|
||||
providerConfig.value[k] = config[k];
|
||||
});
|
||||
};
|
||||
|
||||
reader.readAsText(event.target.files[0]);
|
||||
}
|
||||
|
||||
function onUploadBackupConfig() {
|
||||
|
||||
backupConfigInput.value.click();
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
async open(appId) {
|
||||
id.value = appId;
|
||||
async open(id) {
|
||||
appId.value = id;
|
||||
busy.value = false;
|
||||
formError.value = {};
|
||||
provider.value = '';
|
||||
providerConfig.value = {};
|
||||
remotePath.value = '';
|
||||
|
||||
dialog.value.open();
|
||||
}
|
||||
@@ -34,6 +220,8 @@ defineExpose({
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<input ref="backupConfigInput" type="file" style="display: none" accept="application/json, text/json" @change="onBackupConfigChanged"/>
|
||||
|
||||
<Dialog ref="dialog" :title="$t('app.importBackupDialog.title')"
|
||||
:confirm-label="$t('app.importBackupDialog.importAction')"
|
||||
:confirm-active="!busy"
|
||||
@@ -47,7 +235,13 @@ defineExpose({
|
||||
<div>
|
||||
<div>{{ $t('app.importBackupDialog.description') }}</div>
|
||||
|
||||
<BackupProviderForm ref="form" v-model:provider="provider" v-model:provider-config="providerConfig" :form-error="formError" />
|
||||
<!-- remotePath contains the prefix as well -->
|
||||
<FormGroup>
|
||||
<label for="inputRemotePath">{{ $t('app.importBackupDialog.remotePath') }} <sup><a href="https://docs.cloudron.io/backups/#import-app-backup" class="help" target="_blank"><i class="fa fa-question-circle"></i></a></sup></label>
|
||||
<TextInput id="inputRemotePath" v-model="remotePath" required />
|
||||
</FormGroup>
|
||||
|
||||
<BackupProviderForm ref="form" v-model:provider="provider" v-model:provider-config="providerConfig" :form-error="formError" :import-only="true" />
|
||||
</div>
|
||||
</Dialog>
|
||||
</div>
|
||||
|
||||
@@ -8,7 +8,13 @@ import { mountlike, s3like } from '../utils.js';
|
||||
|
||||
const provider = defineModel('provider');
|
||||
const providerConfig = defineModel('providerConfig');
|
||||
const formError = defineProps(['formError']);
|
||||
const formError = defineProps({
|
||||
formError: {},
|
||||
importOnly: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
}
|
||||
});
|
||||
|
||||
const systemModel = SystemModel.create();
|
||||
|
||||
@@ -156,19 +162,19 @@ onMounted(async () => {
|
||||
</FormGroup>
|
||||
|
||||
<!-- Filesystem -->
|
||||
<FormGroup v-if="provider === 'filesystem'">
|
||||
<FormGroup v-if="provider === 'filesystem' && !importOnly">
|
||||
<label for="backupFolderInput">{{ $t('backups.configureBackupStorage.localDirectory') }}</label>
|
||||
<TextInput id="backupFolderInput" v-model="providerConfig.backupFolder" placeholder="Directory for backups" required />
|
||||
</FormGroup>
|
||||
|
||||
<!-- Filesystem/SSHFS/CIFS/NFS/EXT4/mountpoint -->
|
||||
<Checkbox v-if="provider === 'filesystem' || mountlike(provider)" v-model="providerConfig.useHardlinks" :label="$t('backups.configureBackupStorage.hardlinksLabel')"/>
|
||||
<Checkbox v-if="(provider === 'filesystem' || mountlike(provider)) && !importOnly" v-model="providerConfig.useHardlinks" :label="$t('backups.configureBackupStorage.hardlinksLabel')"/>
|
||||
|
||||
<!-- CIFS/mountpoint -->
|
||||
<Checkbox v-if="provider === 'mountpoint' || provider === 'cifs'" v-model="providerConfig.preserveAttributes" :label="$t('backups.configureBackupStorage.preserveAttributesLabel')"/>
|
||||
<Checkbox v-if="(provider === 'mountpoint' || provider === 'cifs') && !importOnly" v-model="providerConfig.preserveAttributes" :label="$t('backups.configureBackupStorage.preserveAttributesLabel')"/>
|
||||
|
||||
<!-- mountpoint -->
|
||||
<Checkbox v-if="provider === 'mountpoint'" v-model="providerConfig.chown" :label="$t('backups.configureBackupStorage.chown')"/>
|
||||
<Checkbox v-if="provider === 'mountpoint' && !importOnly" v-model="providerConfig.chown" :label="$t('backups.configureBackupStorage.chown')"/>
|
||||
|
||||
<!-- S3/Minio/SOS/GCS/UpCloud/B2/R2 -->
|
||||
<FormGroup v-if="provider === 'minio' || provider === 'upcloud-objectstorage' || provider === 'backblaze-b2' || provider === 'cloudflare-r2' || provider === 's3-v4-compat' || provider === 'idrive-e2'">
|
||||
@@ -183,7 +189,7 @@ onMounted(async () => {
|
||||
<TextInput id="bucketInput" v-model="providerConfig.bucket" required />
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup v-if="provider !== 'filesystem' && provider !== 'noop'">
|
||||
<FormGroup v-if="(provider !== 'filesystem' && provider !== 'noop') && !importOnly">
|
||||
<label for="prefixInput">{{ $t('backups.configureBackupStorage.prefix') }}</label>
|
||||
<TextInput id="prefixInput" v-model="providerConfig.prefix" placeholder="Prefix for backup file names" />
|
||||
</FormGroup>
|
||||
|
||||
Reference in New Issue
Block a user