@riligar/elysia-backup 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1316 +0,0 @@
1
- // https://bun.com/docs/runtime/s3
2
- // https://elysiajs.com/plugins/html
3
- import { Elysia, t } from 'elysia'
4
- import { S3Client } from 'bun'
5
- import { CronJob } from 'cron'
6
- import { readdir, stat, readFile, writeFile, mkdir } from 'node:fs/promises'
7
- import { readFileSync } from 'node:fs'
8
- import { existsSync } from 'node:fs'
9
- import { join, relative, dirname } from 'node:path'
10
- import { html } from '@elysiajs/html'
11
-
12
- // Session Management
13
- const sessions = new Map()
14
-
15
- /**
16
- * Generate a secure random session token
17
- */
18
- const generateSessionToken = () => {
19
- const array = new Uint8Array(32)
20
- crypto.getRandomValues(array)
21
- return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('')
22
- }
23
-
24
- /**
25
- * Create a new session for a user
26
- */
27
- const createSession = (username, sessionDuration = 24 * 60 * 60 * 1000) => {
28
- const token = generateSessionToken()
29
- const expiresAt = Date.now() + sessionDuration
30
- sessions.set(token, { username, expiresAt })
31
- return { token, expiresAt }
32
- }
33
-
34
- /**
35
- * Validate and return session data
36
- */
37
- const getSession = token => {
38
- if (!token) return null
39
- const session = sessions.get(token)
40
- if (!session) return null
41
- if (Date.now() > session.expiresAt) {
42
- sessions.delete(token)
43
- return null
44
- }
45
- return session
46
- }
47
-
48
- /**
49
- * Delete a session
50
- */
51
- const deleteSession = token => {
52
- if (token) {
53
- sessions.delete(token)
54
- }
55
- }
56
-
57
- /**
58
- * Elysia Plugin for R2/S3 Backup with UI (using native Bun.s3)
59
- *
60
- * @param {Object} config
61
- * @param {string} config.bucket - The R2/S3 bucket name
62
- * @param {string} config.accessKeyId - R2/S3 Access Key ID
63
- * @param {string} config.secretAccessKey - R2/S3 Secret Access Key
64
- * @param {string} config.endpoint - R2/S3 Endpoint URL
65
- * @param {string} config.sourceDir - Local directory to backup
66
- * @param {string} [config.prefix] - Optional prefix for S3 keys (e.g. 'backups/')
67
- * @param {string} [config.cronSchedule] - Cron schedule expression
68
- * @param {boolean} [config.cronEnabled] - Whether the cron schedule is enabled
69
- * @param {string} [config.configPath] - Path to save runtime configuration (default: './backup-config.json')
70
- */
71
- export const r2Backup = initialConfig => app => {
72
- // State to hold runtime configuration (allows UI updates)
73
- const configPath = initialConfig.configPath || './backup-config.json'
74
-
75
- // Load saved config if exists
76
- let savedConfig = {}
77
- if (existsSync(configPath)) {
78
- try {
79
- // We use synchronous read here or just init with promise (but top level await is tricky in plugins depending on usage)
80
- // For simplicity in this context, we'll rely on the fact that this runs once on startup.
81
- // However, since we are in a function, we can't easily do async await at top level unless the plugin is async.
82
- // Elysia plugins can be async.
83
- // Using readFileSync for startup config loading to ensure it's ready.
84
- const fileContent = readFileSync(configPath, 'utf-8')
85
- savedConfig = JSON.parse(fileContent)
86
- console.log('Loaded backup config from', configPath)
87
- } catch (e) {
88
- console.error('Failed to load backup config:', e)
89
- }
90
- }
91
-
92
- let config = { ...initialConfig, ...savedConfig }
93
- let backupJob = null
94
-
95
- const getS3Client = () => {
96
- // Debug config (masked)
97
- console.log('S3 Config:', {
98
- bucket: config.bucket,
99
- endpoint: config.endpoint,
100
- accessKeyId: config.accessKeyId ? '***' + config.accessKeyId.slice(-4) : 'missing',
101
- hasSecret: !!config.secretAccessKey,
102
- })
103
-
104
- return new S3Client({
105
- accessKeyId: config.accessKeyId,
106
- secretAccessKey: config.secretAccessKey,
107
- endpoint: config.endpoint,
108
- bucket: config.bucket,
109
- region: 'auto',
110
- // R2 requires specific region handling or defaults.
111
- // Bun S3 defaults to us-east-1 which is usually fine for R2 if endpoint is correct.
112
- })
113
- }
114
-
115
- const uploadFile = async (filePath, rootDir, timestampPrefix) => {
116
- const s3 = getS3Client()
117
- const fileContent = await readFile(filePath)
118
-
119
- const relativePath = relative(rootDir, filePath)
120
- const dir = dirname(relativePath)
121
- const filename = relativePath.split('/').pop() // or basename
122
-
123
- // Format: YYYY-MM-DD_HH-mm-ss_filename.ext (or ISO if not provided)
124
- const timestamp = timestampPrefix || new Date().toISOString()
125
- const newFilename = `${timestamp}_${filename}`
126
-
127
- // Reconstruct path with new filename
128
- const finalPath = dir === '.' ? newFilename : join(dir, newFilename)
129
-
130
- const key = config.prefix ? join(config.prefix, finalPath) : finalPath
131
-
132
- console.log(`Uploading ${key}...`)
133
-
134
- // Bun.s3 API: s3.write(key, data)
135
- await s3.write(key, fileContent)
136
- }
137
-
138
- const processDirectory = async (dir, timestampPrefix) => {
139
- const files = await readdir(dir)
140
- for (const file of files) {
141
- const fullPath = join(dir, file)
142
- const stats = await stat(fullPath)
143
- if (stats.isDirectory()) {
144
- await processDirectory(fullPath, timestampPrefix)
145
- } else {
146
- // Filter by extension
147
- const allowedExtensions = config.extensions || []
148
- const hasExtension = allowedExtensions.some(ext => file.endsWith(ext))
149
-
150
- // Skip files that look like backups to prevent recursion/duplication
151
- // Matches: YYYY-MM-DD_HH-mm-ss_ OR ISOString_
152
- const timestampRegex = /^(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)_/
153
- if (timestampRegex.test(file)) {
154
- console.log(`Skipping backup-like file: ${file}`)
155
- continue
156
- }
157
-
158
- if (allowedExtensions.length === 0 || hasExtension) {
159
- await uploadFile(fullPath, config.sourceDir, timestampPrefix)
160
- }
161
- }
162
- }
163
- }
164
-
165
- const setupCron = () => {
166
- if (backupJob) {
167
- backupJob.stop()
168
- backupJob = null
169
- }
170
-
171
- if (config.cronSchedule && config.cronEnabled !== false) {
172
- console.log(`Setting up backup cron: ${config.cronSchedule}`)
173
- try {
174
- backupJob = new CronJob(
175
- config.cronSchedule,
176
- async () => {
177
- console.log('Running scheduled backup...')
178
- try {
179
- // Generate timestamp similar to manual run
180
- const now = new Date()
181
- const timestamp =
182
- now.getFullYear() +
183
- '-' +
184
- String(now.getMonth() + 1).padStart(2, '0') +
185
- '-' +
186
- String(now.getDate()).padStart(2, '0') +
187
- '_' +
188
- String(now.getHours()).padStart(2, '0') +
189
- '-' +
190
- String(now.getMinutes()).padStart(2, '0') +
191
- '-' +
192
- String(now.getSeconds()).padStart(2, '0')
193
-
194
- await processDirectory(config.sourceDir, timestamp)
195
- console.log('Scheduled backup completed')
196
- } catch (e) {
197
- console.error('Scheduled backup failed:', e)
198
- }
199
- },
200
- null,
201
- true // start
202
- )
203
- } catch (e) {
204
- console.error('Invalid cron schedule:', e.message)
205
- }
206
- }
207
- }
208
-
209
- // Initialize cron
210
- setupCron()
211
-
212
- const listRemoteFiles = async () => {
213
- const s3 = getS3Client()
214
- // Bun.s3 list API
215
- // Returns a Promise that resolves to the list of files (or object with contents)
216
- // Based on Bun docs/behavior, list() returns an array of S3File-like objects or similar structure
217
- try {
218
- const response = await s3.list({ prefix: config.prefix || '' })
219
- // If response is array
220
- if (Array.isArray(response)) {
221
- return response.map(f => ({
222
- Key: f.key || f.name, // Handle potential property names
223
- Size: f.size,
224
- LastModified: f.lastModified,
225
- }))
226
- }
227
- // If response has contents (AWS-like)
228
- if (response.contents) {
229
- return response.contents.map(f => ({
230
- Key: f.key,
231
- Size: f.size,
232
- LastModified: f.lastModified,
233
- }))
234
- }
235
- console.log('Unknown list response structure:', response)
236
- return []
237
- } catch (e) {
238
- console.error('Error listing files with Bun.s3:', e)
239
- return []
240
- }
241
- }
242
-
243
- const restoreFile = async key => {
244
- const s3 = getS3Client()
245
- const file = s3.file(key)
246
-
247
- if (!(await file.exists())) {
248
- throw new Error(`File ${key} not found in bucket`)
249
- }
250
-
251
- const arrayBuffer = await file.arrayBuffer()
252
- const byteArray = new Uint8Array(arrayBuffer)
253
-
254
- // Determine local path
255
- // Remove prefix from key to get relative path
256
- const relativePath = config.prefix ? key.replace(config.prefix, '') : key
257
- // Clean leading slashes if any
258
- const cleanRelative = relativePath.replace(/^[\/\\]/, '')
259
-
260
- // Extract directory and filename
261
- const dir = dirname(cleanRelative)
262
- const filename = cleanRelative.split('/').pop()
263
-
264
- // Strip timestamp prefix if present to restore original filename
265
- // Matches: YYYY-MM-DD_HH-mm-ss_ OR ISOString_
266
- const timestampRegex = /^(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)_/
267
- const originalFilename = filename.replace(timestampRegex, '')
268
-
269
- const finalLocalRelativePath = dir === '.' ? originalFilename : join(dir, originalFilename)
270
- const localPath = join(config.sourceDir, finalLocalRelativePath)
271
-
272
- // Ensure directory exists
273
- await mkdir(dirname(localPath), { recursive: true })
274
- await writeFile(localPath, byteArray)
275
- return localPath
276
- }
277
-
278
- const deleteFile = async key => {
279
- const s3 = getS3Client()
280
- // Bun S3 client delete
281
- await s3.delete(key)
282
- }
283
-
284
- const getJobStatus = () => {
285
- const isRunning = !!backupJob && config.cronEnabled !== false
286
- let nextRun = null
287
- if (isRunning && backupJob) {
288
- try {
289
- const nextDate = backupJob.nextDate()
290
- if (nextDate) {
291
- // cron returns Luxon DateTime, convert to JS Date for consistent ISO string
292
- nextRun = nextDate.toJSDate().toISOString()
293
- }
294
- } catch (e) {
295
- console.error('Error getting next date', e)
296
- }
297
- }
298
- return { isRunning, nextRun }
299
- }
300
-
301
- return app.use(html()).group('/backup', app => {
302
- // Authentication Middleware
303
- const authMiddleware = context => {
304
- // Only bypass auth if no credentials are configured at all
305
- if (!config.auth || !config.auth.username || !config.auth.password) {
306
- return
307
- }
308
-
309
- const path = context.path
310
-
311
- // Allow access to login page and auth endpoints without authentication
312
- if (path === '/backup/login' || path === '/backup/auth/login' || path === '/backup/auth/logout') {
313
- return
314
- }
315
-
316
- // Check session cookie
317
- const cookies = context.headers.cookie || ''
318
- const sessionMatch = cookies.match(/backup-session=([^;]+)/)
319
- const sessionToken = sessionMatch ? sessionMatch[1] : null
320
-
321
- const session = getSession(sessionToken)
322
-
323
- if (!session) {
324
- // Redirect to login page
325
- context.set.status = 302
326
- context.set.headers['Location'] = '/backup/login'
327
- return new Response('Redirecting to login', {
328
- status: 302,
329
- headers: { Location: '/backup/login' },
330
- })
331
- }
332
- }
333
-
334
- return (
335
- app
336
- .onBeforeHandle(authMiddleware)
337
-
338
- // AUTH: Login Page
339
- .get('/login', ({ set }) => {
340
- // If no auth credentials configured, redirect to dashboard
341
- if (!config.auth || !config.auth.username || !config.auth.password) {
342
- set.status = 302
343
- set.headers['Location'] = '/backup'
344
- return
345
- }
346
-
347
- set.headers['Content-Type'] = 'text/html; charset=utf8'
348
- return `
349
- <!DOCTYPE html>
350
- <html lang="en">
351
- <head>
352
- <meta charset="UTF-8">
353
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
354
- <title>Login - Backup Manager</title>
355
- <script src="https://cdn.tailwindcss.com"></script>
356
- <script>
357
- tailwind.config = {
358
- theme: {
359
- extend: {
360
- fontFamily: {
361
- sans: ['Montserrat', 'sans-serif'],
362
- }
363
- }
364
- }
365
- }
366
- </script>
367
- <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
368
- <script src="https://unpkg.com/lucide@latest"></script>
369
- <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700&display=swap" rel="stylesheet">
370
- <style>
371
- [x-cloak] { display: none !important; }
372
- </style>
373
- </head>
374
- <body class="bg-gray-50 min-h-screen flex items-center justify-center p-6 antialiased">
375
- <div class="w-full max-w-md" x-data="loginApp()">
376
- <!-- Login Card -->
377
- <div class="bg-white rounded-2xl border border-gray-100 shadow-[0_8px_30px_rgba(0,0,0,0.08)] overflow-hidden">
378
- <!-- Header -->
379
- <div class="p-10 text-center border-b border-gray-100">
380
- <div class="w-16 h-16 bg-gray-900 rounded-full flex items-center justify-center mx-auto mb-4">
381
- <i data-lucide="shield-check" class="w-8 h-8 text-white"></i>
382
- </div>
383
- <h1 class="text-2xl font-bold text-gray-900 mb-2">Backup Manager</h1>
384
- <p class="text-sm text-gray-500">Access Control Panel</p>
385
- </div>
386
-
387
- <!-- Form -->
388
- <div class="p-10">
389
- <form @submit.prevent="login" class="space-y-6">
390
- <!-- Error Message -->
391
- <div x-show="error" x-cloak class="bg-red-50 border border-red-200 rounded-lg p-4">
392
- <div class="flex items-center gap-3">
393
- <i data-lucide="alert-circle" class="w-5 h-5 text-red-600"></i>
394
- <span class="text-sm text-red-800 font-medium" x-text="error"></span>
395
- </div>
396
- </div>
397
-
398
- <!-- Username -->
399
- <div>
400
- <label class="block text-sm font-semibold text-gray-700 mb-2">Username</label>
401
- <div class="relative">
402
- <div class="absolute inset-y-0 left-0 pl-4 flex items-center pointer-events-none">
403
- <i data-lucide="user" class="w-5 h-5 text-gray-400"></i>
404
- </div>
405
- <input
406
- type="text"
407
- x-model="username"
408
- required
409
- class="w-full bg-gray-50 border border-gray-200 rounded-lg pl-12 pr-4 py-3 text-gray-900 focus:ring-2 focus:ring-gray-900 focus:border-transparent outline-none transition-all font-medium"
410
- placeholder="Enter your username"
411
- autofocus
412
- >
413
- </div>
414
- </div>
415
-
416
- <!-- Password -->
417
- <div>
418
- <label class="block text-sm font-semibold text-gray-700 mb-2">Password</label>
419
- <div class="relative">
420
- <div class="absolute inset-y-0 left-0 pl-4 flex items-center pointer-events-none">
421
- <i data-lucide="lock" class="w-5 h-5 text-gray-400"></i>
422
- </div>
423
- <input
424
- type="password"
425
- x-model="password"
426
- required
427
- class="w-full bg-gray-50 border border-gray-200 rounded-lg pl-12 pr-4 py-3 text-gray-900 focus:ring-2 focus:ring-gray-900 focus:border-transparent outline-none transition-all font-medium"
428
- placeholder="Enter your password"
429
- >
430
- </div>
431
- </div>
432
-
433
- <!-- Submit Button -->
434
- <button
435
- type="submit"
436
- :disabled="loading"
437
- class="w-full bg-gray-900 hover:bg-gray-800 text-white font-bold py-3.5 px-6 rounded-xl transition-all shadow-lg hover:shadow-xl transform hover:-translate-y-0.5 flex items-center justify-center gap-2 disabled:opacity-70 disabled:cursor-not-allowed disabled:transform-none"
438
- >
439
- <span x-show="!loading" class="flex items-center gap-2">
440
- <span>Sign In</span>
441
- <i data-lucide="arrow-right" class="w-5 h-5"></i>
442
- </span>
443
- <span x-show="loading" class="flex items-center gap-2">
444
- <i data-lucide="loader-2" class="w-5 h-5 animate-spin"></i>
445
- <span>Authenticating...</span>
446
- </span>
447
- </button>
448
- </form>
449
- </div>
450
- </div>
451
-
452
- <!-- Footer -->
453
- <div class="text-center mt-6 text-sm text-gray-500">
454
- <i data-lucide="info" class="w-4 h-4 inline-block mr-1"></i>
455
- Secure connection required for production use
456
- </div>
457
- </div>
458
-
459
- <script>
460
- document.addEventListener('alpine:init', () => {
461
- Alpine.data('loginApp', () => ({
462
- username: '',
463
- password: '',
464
- loading: false,
465
- error: '',
466
-
467
- init() {
468
- this.$nextTick(() => lucide.createIcons());
469
- },
470
-
471
- async login() {
472
- this.loading = true;
473
- this.error = '';
474
-
475
- try {
476
- const response = await fetch('/backup/auth/login', {
477
- method: 'POST',
478
- headers: { 'Content-Type': 'application/json' },
479
- body: JSON.stringify({
480
- username: this.username,
481
- password: this.password
482
- })
483
- });
484
-
485
- const data = await response.json();
486
-
487
- if (response.ok && data.status === 'success') {
488
- // Redirect to dashboard
489
- window.location.href = '/backup';
490
- } else {
491
- this.error = data.message || 'Invalid credentials';
492
- this.$nextTick(() => lucide.createIcons());
493
- }
494
- } catch (err) {
495
- this.error = 'Connection failed. Please try again.';
496
- this.$nextTick(() => lucide.createIcons());
497
- } finally {
498
- this.loading = false;
499
- this.$nextTick(() => lucide.createIcons());
500
- }
501
- }
502
- }));
503
- });
504
- </script>
505
- </body>
506
- </html>
507
- `
508
- })
509
-
510
- // AUTH: Login Endpoint
511
- .post(
512
- '/auth/login',
513
- async ({ body, set }) => {
514
- // Check if auth credentials are configured
515
- if (!config.auth || !config.auth.username || !config.auth.password) {
516
- set.status = 403
517
- return {
518
- status: 'error',
519
- message: 'Authentication is not configured',
520
- }
521
- }
522
-
523
- const { username, password } = body
524
-
525
- // Validate credentials
526
- if (username === config.auth.username && password === config.auth.password) {
527
- // Create session
528
- const sessionDuration = config.auth.sessionDuration || 24 * 60 * 60 * 1000 // 24h default
529
- const { token, expiresAt } = createSession(username, sessionDuration)
530
-
531
- // Set cookie
532
- const expiresDate = new Date(expiresAt)
533
- set.headers['Set-Cookie'] = `backup-session=${token}; Path=/backup; HttpOnly; SameSite=Lax; Expires=${expiresDate.toUTCString()}`
534
-
535
- return { status: 'success', message: 'Login successful' }
536
- } else {
537
- set.status = 401
538
- return { status: 'error', message: 'Invalid username or password' }
539
- }
540
- },
541
- {
542
- body: t.Object({
543
- username: t.String(),
544
- password: t.String(),
545
- }),
546
- }
547
- )
548
-
549
- // AUTH: Logout Endpoint
550
- .post('/auth/logout', ({ headers, set }) => {
551
- const cookies = headers.cookie || ''
552
- const sessionMatch = cookies.match(/backup-session=([^;]+)/)
553
- const sessionToken = sessionMatch ? sessionMatch[1] : null
554
-
555
- if (sessionToken) {
556
- deleteSession(sessionToken)
557
- }
558
-
559
- // Clear cookie
560
- set.headers['Set-Cookie'] = `backup-session=; Path=/backup; HttpOnly; SameSite=Lax; Max-Age=0`
561
-
562
- return { status: 'success', message: 'Logged out successfully' }
563
- })
564
-
565
- // API: Run Backup
566
- .post(
567
- '/api/run',
568
- async ({ body, set }) => {
569
- try {
570
- const { timestamp } = body || {}
571
- console.log(`Starting backup of ${config.sourceDir} to ${config.bucket} with timestamp ${timestamp}`)
572
- await processDirectory(config.sourceDir, timestamp)
573
- return {
574
- status: 'success',
575
- message: 'Backup completed successfully',
576
- timestamp: new Date().toISOString(),
577
- }
578
- } catch (error) {
579
- console.error('Backup failed:', error)
580
- set.status = 500
581
- return { status: 'error', message: error.message }
582
- }
583
- },
584
- {
585
- body: t.Optional(
586
- t.Object({
587
- timestamp: t.Optional(t.String()),
588
- })
589
- ),
590
- }
591
- )
592
-
593
- // API: List Files
594
- .get('/api/files', async ({ set }) => {
595
- try {
596
- const files = await listRemoteFiles()
597
- return {
598
- files: files.map(f => ({
599
- key: f.Key,
600
- size: f.Size,
601
- lastModified: f.LastModified,
602
- })),
603
- }
604
- } catch (error) {
605
- set.status = 500
606
- return { status: 'error', message: error.message }
607
- }
608
- })
609
-
610
- // API: Restore File
611
- .post(
612
- '/api/restore',
613
- async ({ body, set }) => {
614
- try {
615
- const { key } = body
616
- if (!key) throw new Error('Key is required')
617
- const localPath = await restoreFile(key)
618
- return { status: 'success', message: `Restored to ${localPath}` }
619
- } catch (error) {
620
- set.status = 500
621
- return { status: 'error', message: error.message }
622
- }
623
- },
624
- {
625
- body: t.Object({
626
- key: t.String(),
627
- }),
628
- }
629
- )
630
-
631
- // API: Delete File
632
- .post(
633
- '/api/delete',
634
- async ({ body, set }) => {
635
- try {
636
- const { key } = body
637
- if (!key) throw new Error('Key is required')
638
- await deleteFile(key)
639
- return { status: 'success', message: `Deleted ${key}` }
640
- } catch (error) {
641
- set.status = 500
642
- return { status: 'error', message: error.message }
643
- }
644
- },
645
- {
646
- body: t.Object({
647
- key: t.String(),
648
- }),
649
- }
650
- )
651
-
652
- // API: Update Config
653
- .post(
654
- '/api/config',
655
- async ({ body }) => {
656
- // Handle extensions if passed as string
657
- let newConfig = { ...body }
658
- if (typeof newConfig.extensions === 'string') {
659
- newConfig.extensions = newConfig.extensions
660
- .split(',')
661
- .map(e => e.trim())
662
- .filter(Boolean)
663
- }
664
- config = { ...config, ...newConfig }
665
-
666
- // Persist config
667
- try {
668
- // Don't save secrets in plain text if possible, but for this simple tool we might have to
669
- // or just save the non-env parts.
670
- // For now, we save everything that overrides the defaults.
671
- await writeFile(configPath, JSON.stringify(config, null, 2))
672
- } catch (e) {
673
- console.error('Failed to save config:', e)
674
- }
675
-
676
- setupCron() // Restart cron with new config
677
- return {
678
- status: 'success',
679
- config: { ...config, secretAccessKey: '***' },
680
- jobStatus: getJobStatus(),
681
- }
682
- },
683
- {
684
- body: t.Object({
685
- bucket: t.String(),
686
- endpoint: t.String(),
687
- sourceDir: t.String(),
688
- prefix: t.Optional(t.String()),
689
- extensions: t.Optional(t.Union([t.Array(t.String()), t.String()])), // Allow array or comma-separated string
690
- accessKeyId: t.String(),
691
- secretAccessKey: t.String(),
692
- cronSchedule: t.Optional(t.String()),
693
- cronEnabled: t.Optional(t.Boolean()),
694
- }),
695
- }
696
- )
697
-
698
- // UI: Dashboard
699
- .get('/', ({ set }) => {
700
- set.headers['Content-Type'] = 'text/html; charset=utf8'
701
- const jobStatus = getJobStatus()
702
- return `
703
- <!DOCTYPE html>
704
- <html lang="en">
705
- <head>
706
- <meta charset="UTF-8">
707
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
708
- <title>R2 Backup Manager</title>
709
- <script src="https://cdn.tailwindcss.com"></script>
710
- <script>
711
- tailwind.config = {
712
- theme: {
713
- extend: {
714
- fontFamily: {
715
- sans: ['Montserrat', 'sans-serif'],
716
- },
717
- colors: {
718
- gray: {
719
- 50: '#F9FAFB',
720
- 100: '#F3F4F6',
721
- 200: '#E5E7EB',
722
- 300: '#D1D5DB',
723
- 400: '#9CA3AF',
724
- 500: '#6B7280',
725
- 600: '#4B5563',
726
- 700: '#374151',
727
- 800: '#1F2937',
728
- 900: '#111827',
729
- }
730
- }
731
- }
732
- }
733
- }
734
- </script>
735
- <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
736
- <script src="https://unpkg.com/lucide@latest"></script>
737
- <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700&display=swap" rel="stylesheet">
738
- <style>
739
- [x-cloak] { display: none !important; }
740
- </style>
741
- </head>
742
- <body class="bg-gray-50 text-gray-900 min-h-screen p-12 antialiased selection:bg-gray-900 selection:text-white">
743
- <div class="max-w-5xl mx-auto" x-data="backupApp()">
744
- <!-- Header -->
745
- <div class="flex flex-col md:flex-row md:items-end justify-between mb-16 gap-6">
746
- <div>
747
- <h6 class="text-xs font-bold tracking-widest text-gray-500 uppercase mb-2 flex items-center gap-2">
748
- <i data-lucide="shield-check" class="w-4 h-4"></i>
749
- System Administration
750
- </h6>
751
- <h1 class="text-4xl font-bold text-gray-900 tracking-tight flex items-center gap-3">
752
- Backup Manager
753
- </h1>
754
- </div>
755
-
756
- <div class="flex items-center gap-4">
757
- <!-- Tabs -->
758
- <div class="flex p-1 bg-gray-200/50 rounded-xl">
759
- <button @click="activeTab = 'dashboard'; $nextTick(() => lucide.createIcons())"
760
- :class="activeTab === 'dashboard' ? 'bg-white text-gray-900 shadow-sm' : 'text-gray-500 hover:text-gray-700'"
761
- class="px-6 py-2.5 rounded-lg text-sm font-semibold transition-all duration-200 flex items-center gap-2">
762
- <i data-lucide="layout-dashboard" class="w-4 h-4"></i>
763
- Overview
764
- </button>
765
- <button @click="activeTab = 'files'; fetchFiles()"
766
- :class="activeTab === 'files' ? 'bg-white text-gray-900 shadow-sm' : 'text-gray-500 hover:text-gray-700'"
767
- class="px-6 py-2.5 rounded-lg text-sm font-semibold transition-all duration-200 flex items-center gap-2">
768
- <i data-lucide="folder-open" class="w-4 h-4"></i>
769
- Files & Restore
770
- </button>
771
- <button @click="activeTab = 'settings'; $nextTick(() => lucide.createIcons())"
772
- :class="activeTab === 'settings' ? 'bg-white text-gray-900 shadow-sm' : 'text-gray-500 hover:text-gray-700'"
773
- class="px-6 py-2.5 rounded-lg text-sm font-semibold transition-all duration-200 flex items-center gap-2">
774
- <i data-lucide="settings" class="w-4 h-4"></i>
775
- Settings
776
- </button>
777
- </div>
778
-
779
- ${
780
- config.auth && config.auth.enabled
781
- ? `
782
- <!-- Logout Button -->
783
- <button @click="logout" class="inline-flex items-center gap-2 px-4 py-2.5 bg-gray-100 hover:bg-gray-200 text-gray-700 font-semibold text-sm rounded-lg transition-all">
784
- <i data-lucide="log-out" class="w-4 h-4"></i>
785
- <span>Logout</span>
786
- </button>
787
- `
788
- : ''
789
- }
790
- </div>
791
- </div>
792
-
793
- <!-- Dashboard Tab -->
794
- <div x-show="activeTab === 'dashboard'" class="space-y-12"
795
- x-transition:enter="transition ease-out duration-300"
796
- x-transition:enter-start="opacity-0 translate-y-2"
797
- x-transition:enter-end="opacity-100 translate-y-0">
798
-
799
- <!-- Status Cards -->
800
- <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
801
- <div class="bg-white p-8 rounded-2xl border border-gray-100 shadow-[0_2px_8px_rgba(0,0,0,0.04)]">
802
- <div class="text-gray-500 text-sm font-medium mb-2 flex items-center gap-2">
803
- <i data-lucide="folder" class="w-4 h-4"></i>
804
- Local Source
805
- </div>
806
- <div class="text-gray-900 font-semibold truncate" x-text="config.sourceDir"></div>
807
- </div>
808
- <div class="bg-white p-8 rounded-2xl border border-gray-100 shadow-[0_2px_8px_rgba(0,0,0,0.04)]">
809
- <div class="text-gray-500 text-sm font-medium mb-2 flex items-center gap-2">
810
- <i data-lucide="cloud" class="w-4 h-4"></i>
811
- Target Bucket
812
- </div>
813
- <div class="text-gray-900 font-semibold truncate" x-text="config.bucket"></div>
814
- </div>
815
- <div class="bg-white p-8 rounded-2xl border border-gray-100 shadow-[0_2px_8px_rgba(0,0,0,0.04)]">
816
- <div class="text-gray-500 text-sm font-medium mb-2 flex items-center gap-2">
817
- <i data-lucide="clock" class="w-4 h-4"></i>
818
- Last Backup
819
- </div>
820
- <div class="text-gray-900 font-semibold" x-text="lastBackup || 'No backup recorded'"></div>
821
- </div>
822
- <div class="bg-white p-8 rounded-2xl border border-gray-100 shadow-[0_2px_8px_rgba(0,0,0,0.04)] relative overflow-hidden group">
823
- <div class="text-gray-500 text-sm font-medium mb-3 flex items-center gap-2">
824
- <i data-lucide="calendar-clock" class="w-4 h-4"></i>
825
- Schedule Status
826
- </div>
827
-
828
- <template x-if="cronStatus.isRunning">
829
- <div class="flex items-center gap-3">
830
- <span class="relative flex h-2.5 w-2.5 shrink-0" title="Active">
831
- <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span>
832
- <span class="relative inline-flex rounded-full h-2.5 w-2.5 bg-green-500"></span>
833
- </span>
834
- <div class="flex items-baseline gap-1.5 min-w-0">
835
- <span class="text-gray-900 font-bold text-xs font-mono truncate" x-text="new Date(cronStatus.nextRun).toLocaleString(undefined, { dateStyle: 'short', timeStyle: 'medium' })"></span>
836
- </div>
837
- </div>
838
- </template>
839
-
840
- <template x-if="!cronStatus.isRunning">
841
- <div class="flex items-center gap-3">
842
- <span class="h-2.5 w-2.5 rounded-full bg-gray-300 shrink-0" title="Stopped"></span>
843
- <span class="text-gray-400 text-sm italic">Scheduler paused</span>
844
- </div>
845
- </template>
846
- </div>
847
- </div>
848
-
849
- <!-- Action Area -->
850
- <div class="bg-white rounded-2xl border border-gray-100 shadow-[0_2px_8px_rgba(0,0,0,0.04)] overflow-hidden">
851
- <div class="p-10 flex flex-col items-center justify-center text-center">
852
- <div class="w-16 h-16 bg-gray-50 rounded-full flex items-center justify-center mb-6 text-gray-400">
853
- <i data-lucide="save" class="w-8 h-8"></i>
854
- </div>
855
- <h3 class="text-xl font-bold text-gray-900 mb-2">Trigger Manual Backup</h3>
856
- <p class="text-gray-500 max-w-md mb-8 leading-relaxed">Initiate a backup of your local directory to the configured R2 bucket. This will upload all files recursively.</p>
857
-
858
- <button
859
- @click="runBackup()"
860
- :disabled="loading"
861
- class="group relative inline-flex items-center justify-center px-8 py-3.5 text-base font-semibold text-white transition-all duration-200 bg-gray-900 rounded-xl hover:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-900 disabled:opacity-70 disabled:cursor-not-allowed">
862
- <span x-show="!loading" class="flex items-center gap-2">
863
- <i data-lucide="play-circle" class="w-5 h-5"></i>
864
- Start Backup Process
865
- </span>
866
- <span x-show="loading" class="flex items-center gap-2">
867
- <i data-lucide="loader-2" class="w-5 h-5 animate-spin"></i>
868
- Processing...
869
- </span>
870
- </button>
871
- </div>
872
-
873
- <!-- Logs -->
874
- <div class="bg-gray-50 border-t border-gray-100 p-8">
875
- <h4 class="text-xs font-bold text-gray-400 uppercase tracking-wider mb-4">Activity Log</h4>
876
- <div class="space-y-3">
877
- <template x-for="log in logs" :key="log.id">
878
- <div class="flex items-start gap-4 text-sm">
879
- <span class="font-mono text-xs text-gray-400 mt-0.5" x-text="log.time"></span>
880
- <div class="flex items-center gap-2">
881
- <i :data-lucide="log.type === 'error' ? 'alert-circle' : 'info'"
882
- :class="log.type === 'error' ? 'text-red-600' : 'text-gray-400'"
883
- class="w-4 h-4"></i>
884
- <span :class="log.type === 'error' ? 'text-red-600 font-medium' : 'text-gray-600'" x-text="log.message"></span>
885
- </div>
886
- </div>
887
- </template>
888
- <div x-show="logs.length === 0" class="text-gray-400 text-sm italic">No recent activity recorded.</div>
889
- </div>
890
- </div>
891
- </div>
892
- </div>
893
-
894
- <!-- Files Tab -->
895
- <div x-show="activeTab === 'files'" class="bg-white rounded-2xl border border-gray-100 shadow-[0_2px_8px_rgba(0,0,0,0.04)] overflow-hidden"
896
- x-transition:enter="transition ease-out duration-300"
897
- x-transition:enter-start="opacity-0 translate-y-2"
898
- x-transition:enter-end="opacity-100 translate-y-0">
899
- <div class="p-8 border-b border-gray-100 flex justify-between items-center">
900
- <div>
901
- <h2 class="text-xl font-bold text-gray-900 flex items-center gap-2">
902
- <i data-lucide="server" class="w-5 h-5 text-gray-400"></i>
903
- Remote Files
904
- </h2>
905
- <p class="text-gray-500 text-sm mt-1">Files currently stored in your R2 bucket</p>
906
- </div>
907
- <button @click="fetchFiles()" class="text-gray-900 hover:text-gray-600 font-semibold text-sm transition-colors flex items-center gap-2">
908
- <i data-lucide="refresh-cw" class="w-4 h-4"></i>
909
- Refresh List
910
- </button>
911
- </div>
912
- <div class="overflow-x-auto">
913
- <table class="w-full text-left">
914
- <thead>
915
- <tr class="border-b border-gray-100">
916
- <th class="px-8 py-4 text-xs font-bold text-gray-400 uppercase tracking-wider">File Key</th>
917
- <th class="px-8 py-4 text-xs font-bold text-gray-400 uppercase tracking-wider">Size</th>
918
- <th class="px-8 py-4 text-xs font-bold text-gray-400 uppercase tracking-wider">Last Modified</th>
919
- <th class="px-8 py-4 text-xs font-bold text-gray-400 uppercase tracking-wider text-right">Action</th>
920
- </tr>
921
- </thead>
922
- <template x-for="group in groups" :key="group.name">
923
- <tbody class="divide-y divide-gray-50 border-b border-gray-100">
924
- <!-- Folder Header -->
925
- <tr class="bg-gray-50 hover:bg-gray-100 cursor-pointer transition-colors select-none"
926
- @click="group.expanded = !group.expanded; $nextTick(() => lucide.createIcons())">
927
- <td colspan="4" class="px-8 py-3">
928
- <div class="flex items-center gap-3">
929
- <i :data-lucide="group.expanded ? 'folder-open' : 'folder'" class="w-5 h-5 text-gray-400"></i>
930
- <span class="font-bold text-gray-700 text-sm" x-text="formatDateHeader(group.name)"></span>
931
- <span class="text-xs font-medium text-gray-500 bg-gray-200 px-2 py-0.5 rounded-full" x-text="group.files.length"></span>
932
- </div>
933
- </td>
934
- </tr>
935
-
936
- <!-- Files in Group -->
937
- <template x-for="file in group.files" :key="file.key">
938
- <tr x-show="group.expanded" class="hover:bg-gray-50 transition-colors duration-150 group bg-white">
939
- <td class="px-8 py-5 font-medium text-gray-900 text-sm pl-12" x-text="file.key"></td>
940
- <td class="px-8 py-5 text-gray-500 text-sm" x-text="formatBytes(file.size)"></td>
941
- <td class="px-8 py-5 text-gray-500 text-sm" x-text="new Date(file.lastModified).toLocaleString()"></td>
942
- <td class="px-8 py-5 text-right">
943
- <div class="flex justify-end gap-3 items-center">
944
- <!-- Restore Button -->
945
- <button
946
- x-data="holdButton(() => restoreFile(file.key))"
947
- @mousedown="start()" @touchstart.prevent="start()"
948
- @mouseup="stop()" @mouseleave="stop()" @touchend="stop()"
949
- class="relative overflow-hidden px-4 py-2 rounded-lg text-xs font-bold uppercase tracking-wider text-blue-600 bg-blue-50 hover:bg-blue-100 transition-colors select-none"
950
- title="Hold 3s to Restore"
951
- >
952
- <div class="absolute inset-0 bg-blue-200/50 origin-left transition-all duration-0 ease-linear" :style="'width: ' + progress + '%'"></div>
953
- <span class="relative z-10 flex items-center gap-2">
954
- <i data-lucide="rotate-ccw" class="w-3 h-3"></i>
955
- <span x-text="progress > 0 ? 'Hold...' : 'Restore'"></span>
956
- </span>
957
- </button>
958
-
959
- <!-- Delete Button -->
960
- <button
961
- x-data="holdButton(() => deleteFile(file.key))"
962
- @mousedown="start()" @touchstart.prevent="start()"
963
- @mouseup="stop()" @mouseleave="stop()" @touchend="stop()"
964
- class="relative overflow-hidden px-4 py-2 rounded-lg text-xs font-bold uppercase tracking-wider text-red-600 bg-red-50 hover:bg-red-100 transition-colors select-none"
965
- title="Hold 3s to Delete"
966
- >
967
- <div class="absolute inset-0 bg-red-200/50 origin-left transition-all duration-0 ease-linear" :style="'width: ' + progress + '%'"></div>
968
- <span class="relative z-10 flex items-center gap-2">
969
- <i data-lucide="trash-2" class="w-3 h-3"></i>
970
- <span x-text="progress > 0 ? 'Hold...' : 'Delete'"></span>
971
- </span>
972
- </button>
973
- </div>
974
- </td>
975
- </tr>
976
- </template>
977
- </tbody>
978
- </template>
979
-
980
- <!-- Empty State (outside the loop) -->
981
- <tbody x-show="groups.length === 0">
982
- <tr>
983
- <td colspan="4" class="px-8 py-16 text-center">
984
- <div class="flex flex-col items-center justify-center">
985
- <span x-show="!loadingFiles" class="text-gray-500 font-medium flex flex-col items-center gap-2">
986
- <i data-lucide="inbox" class="w-8 h-8 text-gray-300"></i>
987
- No files found in bucket
988
- </span>
989
- <span x-show="loadingFiles" class="text-gray-500 font-medium animate-pulse flex flex-col items-center gap-2">
990
- <i data-lucide="loader" class="w-8 h-8 animate-spin text-gray-300"></i>
991
- Loading remote files...
992
- </span>
993
- </div>
994
- </td>
995
- </tr>
996
- </tbody>
997
- </table>
998
- </div>
999
- </div>
1000
-
1001
- <!-- Settings Tab -->
1002
- <div x-show="activeTab === 'settings'" class="max-w-3xl mx-auto"
1003
- x-transition:enter="transition ease-out duration-300"
1004
- x-transition:enter-start="opacity-0 translate-y-2"
1005
- x-transition:enter-end="opacity-100 translate-y-0">
1006
- <div class="bg-white rounded-2xl border border-gray-100 shadow-[0_2px_8px_rgba(0,0,0,0.04)] p-10">
1007
- <h2 class="text-xl font-bold text-gray-900 mb-8 flex items-center gap-2">
1008
- <i data-lucide="sliders" class="w-5 h-5"></i>
1009
- Configuration
1010
- </h2>
1011
- <form @submit.prevent="saveConfig" class="space-y-8">
1012
- <div class="space-y-6">
1013
- <div>
1014
- <label class="block text-sm font-semibold text-gray-700 mb-2">Endpoint URL</label>
1015
- <input type="text" x-model="configForm.endpoint" class="w-full bg-gray-50 border border-gray-200 rounded-lg px-4 py-3 text-gray-900 focus:ring-2 focus:ring-gray-900 focus:border-transparent outline-none transition-all font-medium">
1016
- </div>
1017
- <div class="grid grid-cols-2 gap-6">
1018
- <div>
1019
- <label class="block text-sm font-semibold text-gray-700 mb-2">Bucket Name</label>
1020
- <input type="text" x-model="configForm.bucket" class="w-full bg-gray-50 border border-gray-200 rounded-lg px-4 py-3 text-gray-900 focus:ring-2 focus:ring-gray-900 focus:border-transparent outline-none transition-all font-medium">
1021
- </div>
1022
- <div>
1023
- <label class="block text-sm font-semibold text-gray-700 mb-2">Prefix (Folder)</label>
1024
- <input type="text" x-model="configForm.prefix" class="w-full bg-gray-50 border border-gray-200 rounded-lg px-4 py-3 text-gray-900 focus:ring-2 focus:ring-gray-900 focus:border-transparent outline-none transition-all font-medium">
1025
- </div>
1026
- </div>
1027
- <div>
1028
- <label class="block text-sm font-semibold text-gray-700 mb-2">Source Directory (Local)</label>
1029
- <input type="text" x-model="configForm.sourceDir" readonly class="w-full bg-gray-100 border border-gray-200 rounded-lg px-4 py-3 text-gray-500 cursor-not-allowed focus:outline-none font-medium">
1030
- <p class="text-xs text-gray-400 mt-1">Defined in server configuration</p>
1031
- </div>
1032
- <div>
1033
- <label class="block text-sm font-semibold text-gray-700 mb-2">Allowed Extensions (comma separated)</label>
1034
- <input type="text" x-model="configForm.extensions" placeholder=".db, .sqlite" class="w-full bg-gray-50 border border-gray-200 rounded-lg px-4 py-3 text-gray-900 focus:ring-2 focus:ring-gray-900 focus:border-transparent outline-none transition-all font-medium">
1035
- </div>
1036
- <div>
1037
- <label class="block text-sm font-semibold text-gray-700 mb-2">Cron Schedule</label>
1038
- <div class="flex gap-4 items-center">
1039
- <div class="relative flex-grow">
1040
- <input type="text" x-model="configForm.cronSchedule" placeholder="0 0 * * *" class="w-full bg-gray-50 border border-gray-200 rounded-lg px-4 py-3 text-gray-900 focus:ring-2 focus:ring-gray-900 focus:border-transparent outline-none transition-all font-medium">
1041
- <div class="absolute right-3 top-1/2 -translate-y-1/2 text-xs text-gray-500">
1042
- <a href="https://crontab.guru/" target="_blank" class="underline hover:text-gray-800">Help</a>
1043
- </div>
1044
- </div>
1045
- <div class="flex gap-2 shrink-0">
1046
- <button
1047
- type="button"
1048
- @click="configForm.cronEnabled = true; saveConfig()"
1049
- :class="configForm.cronEnabled !== false ? 'bg-green-600 text-white shadow-md' : 'bg-gray-100 text-gray-400 hover:bg-gray-200'"
1050
- class="px-4 py-3 rounded-lg font-bold text-sm transition-all flex items-center gap-2"
1051
- >
1052
- <i data-lucide="play" class="w-4 h-4"></i>
1053
- Start
1054
- </button>
1055
- <button
1056
- type="button"
1057
- @click="configForm.cronEnabled = false; saveConfig()"
1058
- :class="configForm.cronEnabled === false ? 'bg-red-600 text-white shadow-md' : 'bg-gray-100 text-gray-400 hover:bg-gray-200'"
1059
- class="px-4 py-3 rounded-lg font-bold text-sm transition-all flex items-center gap-2"
1060
- >
1061
- <i data-lucide="square" class="w-4 h-4"></i>
1062
- Stop
1063
- </button>
1064
- </div>
1065
- </div>
1066
- <p class="text-xs text-gray-400 mt-1">Format: Minute Hour Day Month DayOfWeek (e.g., "0 0 * * *" for daily at midnight)</p>
1067
- </div>
1068
- <div class="grid grid-cols-2 gap-6">
1069
- <div>
1070
- <label class="block text-sm font-semibold text-gray-700 mb-2">Access Key ID</label>
1071
- <input type="text" x-model="configForm.accessKeyId" class="w-full bg-gray-50 border border-gray-200 rounded-lg px-4 py-3 text-gray-900 focus:ring-2 focus:ring-gray-900 focus:border-transparent outline-none transition-all font-medium">
1072
- </div>
1073
- <div>
1074
- <label class="block text-sm font-semibold text-gray-700 mb-2">Secret Access Key</label>
1075
- <input type="password" x-model="configForm.secretAccessKey" class="w-full bg-gray-50 border border-gray-200 rounded-lg px-4 py-3 text-gray-900 focus:ring-2 focus:ring-gray-900 focus:border-transparent outline-none transition-all font-medium">
1076
- </div>
1077
- </div>
1078
- </div>
1079
-
1080
- <div class="pt-4 flex justify-end">
1081
- <button type="submit" class="bg-gray-900 hover:bg-gray-800 text-white font-bold py-3 px-8 rounded-xl transition-all shadow-lg hover:shadow-xl transform hover:-translate-y-0.5 flex items-center gap-2">
1082
- <i data-lucide="save" class="w-4 h-4"></i>
1083
- Save Changes
1084
- </button>
1085
- </div>
1086
- </form>
1087
- </div>
1088
- </div>
1089
- </div>
1090
- <script>
1091
- document.addEventListener('alpine:init', () => {
1092
- Alpine.data('holdButton', (action) => ({
1093
- progress: 0,
1094
- interval: null,
1095
- start() {
1096
- this.progress = 0
1097
- // 3 seconds = 3000ms. Update every 30ms. 100 steps.
1098
- // 100% / 100 steps = 1% per step.
1099
- this.interval = setInterval(() => {
1100
- this.progress += 1
1101
- if (this.progress >= 100) {
1102
- this.trigger()
1103
- }
1104
- }, 30)
1105
- },
1106
- stop() {
1107
- clearInterval(this.interval)
1108
- this.progress = 0
1109
- },
1110
- trigger() {
1111
- this.stop()
1112
- action()
1113
- }
1114
- }))
1115
- })
1116
-
1117
- function backupApp() {
1118
- return {
1119
- activeTab: 'dashboard',
1120
- loading: false,
1121
- loadingFiles: false,
1122
- lastBackup: null,
1123
- files: [],
1124
- groups: [],
1125
- logs: [],
1126
- config: ${JSON.stringify(config)},
1127
- cronStatus: ${JSON.stringify(jobStatus)},
1128
- configForm: { ...${JSON.stringify(config)} },
1129
-
1130
- init() {
1131
- // Initial load if needed
1132
- this.$nextTick(() => {
1133
- lucide.createIcons()
1134
- })
1135
- },
1136
-
1137
- addLog(message, type = 'info') {
1138
- this.logs.unshift({
1139
- id: Date.now(),
1140
- message,
1141
- type,
1142
- time: new Date().toLocaleTimeString()
1143
- })
1144
- this.$nextTick(() => lucide.createIcons())
1145
- },
1146
-
1147
- formatBytes(bytes, decimals = 2) {
1148
- if (!+bytes) return '0 Bytes'
1149
- const k = 1024
1150
- const dm = decimals < 0 ? 0 : decimals
1151
- const sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB']
1152
- const i = Math.floor(Math.log(bytes) / Math.log(k))
1153
- return \`\${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} \${sizes[i]}\`
1154
- },
1155
-
1156
- formatDateHeader(dateStr) {
1157
- if (dateStr === 'Others') return 'Others';
1158
- // dateStr is YYYY-MM-DD
1159
- // Create date object treating the string as local time components to avoid timezone shifts
1160
- const [y, m, d] = dateStr.split('-').map(Number);
1161
- const date = new Date(y, m - 1, d);
1162
- return date.toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' });
1163
- },
1164
-
1165
- async runBackup() {
1166
- this.loading = true
1167
-
1168
- // Generate local timestamp: YYYY-MM-DD_HH-mm-ss
1169
- const now = new Date()
1170
- const timestamp = now.getFullYear() + '-' +
1171
- String(now.getMonth() + 1).padStart(2, '0') + '-' +
1172
- String(now.getDate()).padStart(2, '0') + '_' +
1173
- String(now.getHours()).padStart(2, '0') + '-' +
1174
- String(now.getMinutes()).padStart(2, '0') + '-' +
1175
- String(now.getSeconds()).padStart(2, '0')
1176
-
1177
- try {
1178
- const res = await fetch('/backup/api/run', {
1179
- method: 'POST',
1180
- headers: { 'Content-Type': 'application/json' },
1181
- body: JSON.stringify({ timestamp })
1182
- })
1183
- const data = await res.json()
1184
- if (data.status === 'success') {
1185
- this.lastBackup = new Date().toLocaleString()
1186
- this.addLog('Backup completed successfully', 'success')
1187
- this.fetchFiles() // Refresh file list
1188
- } else {
1189
- throw new Error(data.message)
1190
- }
1191
- } catch (err) {
1192
- this.addLog('Backup failed: ' + err.message, 'error')
1193
- } finally {
1194
- this.loading = false
1195
- }
1196
- },
1197
-
1198
- async fetchFiles() {
1199
- this.loadingFiles = true
1200
- try {
1201
- const res = await fetch('/backup/api/files')
1202
- const data = await res.json()
1203
- if (data.files) {
1204
- // 1. Sort files by Key Descending (Newest first)
1205
- const sortedFiles = data.files.sort((a, b) => b.key.localeCompare(a.key));
1206
-
1207
- // 2. Group by Date
1208
- const groupsMap = {};
1209
- sortedFiles.forEach(file => {
1210
- // Extract YYYY-MM-DD from the filename (handles prefixes like 'backups/2025-...')
1211
- // Looks for YYYY-MM-DD followed by underscore or T (for ISO)
1212
- // Note: Backslashes must be double-escaped in this server-side template string
1213
- const match = file.key.match(/(?:^|\\/)(\\d{4}-\\d{2}-\\d{2})[_T]/);
1214
- const dateKey = match ? match[1] : 'Others';
1215
-
1216
- if (!groupsMap[dateKey]) {
1217
- groupsMap[dateKey] = [];
1218
- }
1219
- groupsMap[dateKey].push(file);
1220
- });
1221
-
1222
- // 3. Convert to array and sort groups descending
1223
- this.groups = Object.keys(groupsMap)
1224
- .sort()
1225
- .reverse()
1226
- .map((dateKey, index) => ({
1227
- name: dateKey,
1228
- files: groupsMap[dateKey],
1229
- expanded: false // Start with all folders collapsed
1230
- }));
1231
-
1232
- this.$nextTick(() => lucide.createIcons())
1233
- }
1234
- } catch (err) {
1235
- this.addLog('Failed to fetch files: ' + err.message, 'error')
1236
- } finally {
1237
- this.loadingFiles = false
1238
- }
1239
- },
1240
-
1241
- async restoreFile(key) {
1242
- // Removed confirm dialog in favor of hold button
1243
- try {
1244
- const res = await fetch('/backup/api/restore', {
1245
- method: 'POST',
1246
- headers: { 'Content-Type': 'application/json' },
1247
- body: JSON.stringify({ key })
1248
- })
1249
- const data = await res.json()
1250
- if (data.status === 'success') {
1251
- this.addLog(data.message, 'success')
1252
- } else {
1253
- throw new Error(data.message)
1254
- }
1255
- } catch (err) {
1256
- this.addLog('Restore failed: ' + err.message, 'error')
1257
- }
1258
- },
1259
-
1260
- async deleteFile(key) {
1261
- try {
1262
- const res = await fetch('/backup/api/delete', {
1263
- method: 'POST',
1264
- headers: { 'Content-Type': 'application/json' },
1265
- body: JSON.stringify({ key })
1266
- })
1267
- const data = await res.json()
1268
- if (data.status === 'success') {
1269
- this.addLog(data.message, 'success')
1270
- this.fetchFiles() // Refresh list
1271
- } else {
1272
- throw new Error(data.message)
1273
- }
1274
- } catch (err) {
1275
- this.addLog('Delete failed: ' + err.message, 'error')
1276
- }
1277
- },
1278
-
1279
- async saveConfig() {
1280
- try {
1281
- const res = await fetch('/backup/api/config', {
1282
- method: 'POST',
1283
- headers: { 'Content-Type': 'application/json' },
1284
- body: JSON.stringify(this.configForm)
1285
- })
1286
- const data = await res.json()
1287
- if (data.status === 'success') {
1288
- this.config = data.config
1289
- if (data.jobStatus) this.cronStatus = data.jobStatus
1290
- this.addLog('Configuration updated', 'success')
1291
- this.activeTab = 'dashboard'
1292
- }
1293
- } catch (err) {
1294
- this.addLog('Failed to save config: ' + err.message, 'error')
1295
- }
1296
- },
1297
-
1298
- async logout() {
1299
- try {
1300
- await fetch('/backup/auth/logout', { method: 'POST' });
1301
- window.location.href = '/backup/login';
1302
- } catch (err) {
1303
- console.error('Logout failed:', err);
1304
- window.location.href = '/backup/login';
1305
- }
1306
- }
1307
- }
1308
- }
1309
- </script>
1310
- </body>
1311
- </html>
1312
- `
1313
- })
1314
- )
1315
- })
1316
- }