opt.: backup

This commit is contained in:
lollipopkit
2023-09-13 13:05:19 +08:00
parent 9ce7138d9b
commit 269c2a0a10
37 changed files with 535 additions and 632 deletions

View File

@@ -1,6 +1,21 @@
import 'dart:convert';
import 'dart:io';
import 'package:logging/logging.dart';
import 'package:toolbox/data/model/server/private_key_info.dart';
import 'package:toolbox/data/model/server/server_private_info.dart';
import 'package:toolbox/data/model/server/snippet.dart';
import 'package:toolbox/data/res/path.dart';
import 'package:toolbox/data/store/docker.dart';
import 'package:toolbox/data/store/private_key.dart';
import 'package:toolbox/data/store/server.dart';
import 'package:toolbox/data/store/setting.dart';
import 'package:toolbox/data/store/snippet.dart';
import 'package:toolbox/locator.dart';
const backupFormatVersion = 1;
final _logger = Logger('Backup');
class Backup {
// backup format version
@@ -45,4 +60,63 @@ class Backup {
'dockerHosts': dockerHosts,
'settings': settings,
};
Backup.loadFromStore()
: version = backupFormatVersion,
date = DateTime.now().toString().split('.').first,
spis = _server.fetch(),
snippets = _snippet.fetch(),
keys = _privateKey.fetch(),
dockerHosts = _dockerHosts.fetchAll(),
settings = _setting.toJson();
static Future<void> backup() async {
final result = _diyEncrtpt(json.encode(Backup.loadFromStore()));
await File(await backupPath).writeAsString(result);
}
Future<void> restore() async {
for (final s in snippets) {
_snippet.put(s);
}
for (final s in spis) {
_server.put(s);
}
for (final s in keys) {
_privateKey.put(s);
}
for (final k in dockerHosts.keys) {
final val = dockerHosts[k];
if (val != null && val is String && val.isNotEmpty) {
_dockerHosts.put(k, val);
}
}
}
Backup.fromJsonString(String raw)
: this.fromJson(json.decode(_diyDecrypt(raw)));
}
final _server = locator<ServerStore>();
final _snippet = locator<SnippetStore>();
final _privateKey = locator<PrivateKeyStore>();
final _dockerHosts = locator<DockerStore>();
final _setting = locator<SettingStore>();
String _diyEncrtpt(String raw) => json.encode(
raw.codeUnits.map((e) => e * 2 + 1).toList(growable: false),
);
String _diyDecrypt(String raw) {
try {
final list = json.decode(raw);
final sb = StringBuffer();
for (final e in list) {
sb.writeCharCode((e - 1) ~/ 2);
}
return sb.toString();
} catch (e, trace) {
_logger.warning('Decrypt failed', e, trace);
rethrow;
}
}

View File

@@ -144,7 +144,7 @@ class PsResult {
procs.add(Proc.parse(line, map));
} catch (e, trace) {
errs.add('$line: $e');
_logger.warning(trace);
_logger.warning('Parse process failed', e, trace);
}
}

View File

@@ -2,9 +2,9 @@
class BuildData {
static const String name = "ServerBox";
static const int build = 545;
static const int build = 547;
static const String engine = "3.13.2";
static const String buildAt = "2023-09-11 23:23:58.257948";
static const int modifications = 6;
static const String buildAt = "2023-09-12 14:04:07.018274";
static const int modifications = 4;
static const int script = 14;
}

View File

@@ -43,3 +43,5 @@ Future<String> get fontDir async {
await dir.create(recursive: true);
return _fontDir!;
}
Future<String> get backupPath async => '${await docDir}/srvbox_bak.json';

View File

@@ -10,6 +10,7 @@ class SettingStore extends PersistentStore {
Map<String, dynamic> toJson() => {for (var e in box.keys) e: box.get(e)};
// ------BEGIN------
//
// These settings are not displayed in the settings page
// You can edit them in the settings json editor (by long press the settings
// item in the drawer of the home page)
@@ -46,6 +47,25 @@ class SettingStore extends PersistentStore {
'textFactor',
1.0,
);
/// Lanch page idx
late final launchPage = StoreProperty(
box,
'launchPage',
defaultLaunchPageIdx,
);
/// Server detail disk ignore path
late final diskIgnorePath =
StoreListProperty(box, 'diskIgnorePath', defaultDiskIgnorePath);
/// Use double column servers page on Desktop
late final doubleColumnServersPage = StoreProperty(
box,
'doubleColumnServersPage',
isDesktop,
);
// ------END------
late final primaryColor = StoreProperty(
@@ -60,15 +80,6 @@ class SettingStore extends PersistentStore {
defaultUpdateInterval,
);
// Lanch page idx
late final launchPage = StoreProperty(
box,
'launchPage',
defaultLaunchPageIdx,
);
late final termColorIdx = StoreProperty(box, 'termColorIdx', 0);
// Max retry count when connect to server
late final maxRetryCount = StoreProperty(box, 'maxRetryCount', 2);
@@ -93,10 +104,6 @@ class SettingStore extends PersistentStore {
// SSH term font size
late final termFontSize = StoreProperty(box, 'termFontSize', 13.0);
// Server detail disk ignore path
late final diskIgnorePath =
StoreListProperty(box, 'diskIgnorePath', defaultDiskIgnorePath);
// Locale
late final locale = StoreProperty<String>(box, 'locale', '');
@@ -184,13 +191,6 @@ class SettingStore extends PersistentStore {
false,
);
/// Use double column servers page on Desktop
late final doubleColumnServersPage = StoreProperty(
box,
'doubleColumnServersPage',
isDesktop,
);
/// Whether use system's primary color as the app's primary color
late final useSystemPrimaryColor = StoreProperty(
box,