oxirs 0.2.4

Command-line interface for OxiRS - import, export, migration, and benchmarking tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! TDB Backup and Restore Tool
//!
//! Comprehensive backup and restore functionality for OxiRS databases
//! with support for compression, incremental backups, and verification.

use super::{ToolResult, ToolStats};
use std::fs::{self, File};
use std::io::{self, BufReader, BufWriter, Read, Write};
use std::path::{Path, PathBuf};
use std::time::SystemTime;

/// Configuration for backup operation
pub struct BackupConfig {
    pub source: PathBuf,
    pub target: PathBuf,
    pub compress: bool,
    pub incremental: bool,
    pub verify: bool,
    pub include_metadata: bool,
}

/// Configuration for restore operation
pub struct RestoreConfig {
    pub source: PathBuf,
    pub target: PathBuf,
    pub verify: bool,
    pub overwrite: bool,
}

/// Backup metadata
#[derive(Debug)]
struct BackupMetadata {
    timestamp: SystemTime,
    source_path: PathBuf,
    file_count: usize,
    total_size: u64,
    compressed: bool,
    incremental: bool,
}

/// Run backup operation
pub async fn run(
    source: PathBuf,
    target: PathBuf,
    compress: bool,
    incremental: bool,
) -> ToolResult {
    let config = BackupConfig {
        source,
        target,
        compress,
        incremental,
        verify: true,
        include_metadata: true,
    };

    backup(config).await
}

/// Perform database backup
pub async fn backup(config: BackupConfig) -> ToolResult {
    let mut stats = ToolStats::new();

    println!("OxiRS Database Backup");
    println!("=====================\n");

    // Validate source exists
    if !config.source.exists() {
        return Err(format!(
            "Source directory does not exist: {}",
            config.source.display()
        )
        .into());
    }

    if !config.source.is_dir() {
        return Err(format!("Source must be a directory: {}", config.source.display()).into());
    }

    println!("Source: {}", config.source.display());
    println!("Target: {}", config.target.display());
    println!(
        "Mode: {}",
        if config.incremental {
            "Incremental"
        } else {
            "Full"
        }
    );
    println!(
        "Compression: {}\n",
        if config.compress { "Yes" } else { "No" }
    );

    // Create target directory
    if let Some(parent) = config.target.parent() {
        fs::create_dir_all(parent)?;
    }

    // Collect files to backup
    let files = collect_files(&config.source)?;
    println!("Found {} file(s) to backup", files.len());

    // Calculate total size
    let total_size: u64 = files
        .iter()
        .map(|f| f.metadata().ok().map(|m| m.len()).unwrap_or(0))
        .sum();
    println!("Total size: {:.2} MB\n", total_size as f64 / 1_048_576.0);

    // Perform backup
    let backup_start = std::time::Instant::now();

    if config.compress {
        backup_compressed(&files, &config.source, &config.target)?;
    } else {
        backup_uncompressed(&files, &config.source, &config.target)?;
    }

    let backup_time = backup_start.elapsed();

    // Write metadata
    if config.include_metadata {
        write_backup_metadata(&config, &files, total_size)?;
    }

    // Verify if requested
    if config.verify {
        println!("\nVerifying backup...");
        verify_backup(&config.source, &config.target, config.compress)?;
        println!("✓ Backup verified successfully");
    }

    println!("\nBackup completed in {:.2}s", backup_time.as_secs_f64());
    println!("Backup saved to: {}", config.target.display());

    stats.items_processed = files.len();
    stats.finish();
    stats.print_summary("Backup");

    Ok(())
}

/// Perform database restore
pub async fn restore(config: RestoreConfig) -> ToolResult {
    let mut stats = ToolStats::new();

    println!("OxiRS Database Restore");
    println!("======================\n");

    // Validate source exists
    if !config.source.exists() {
        return Err(format!("Backup file does not exist: {}", config.source.display()).into());
    }

    println!("Source: {}", config.source.display());
    println!("Target: {}", config.target.display());

    // Check if target exists
    if config.target.exists() && !config.overwrite {
        return Err(format!(
            "Target directory already exists: {}. Use --overwrite to replace",
            config.target.display()
        )
        .into());
    }

    // Create target directory
    fs::create_dir_all(&config.target)?;

    // Detect backup format
    let is_compressed = config.source.extension().and_then(|s| s.to_str()) == Some("tar")
        || config.source.extension().and_then(|s| s.to_str()) == Some("gz");

    println!(
        "Format: {}\n",
        if is_compressed {
            "Compressed"
        } else {
            "Uncompressed"
        }
    );

    // Perform restore
    let restore_start = std::time::Instant::now();

    if is_compressed {
        restore_compressed(&config.source, &config.target)?;
    } else {
        restore_uncompressed(&config.source, &config.target)?;
    }

    let restore_time = restore_start.elapsed();

    // Verify if requested
    if config.verify {
        println!("\nVerifying restore...");
        verify_restore(&config.target)?;
        println!("✓ Restore verified successfully");
    }

    println!("\nRestore completed in {:.2}s", restore_time.as_secs_f64());
    println!("Database restored to: {}", config.target.display());

    stats.finish();
    stats.print_summary("Restore");

    Ok(())
}

/// Collect all files in directory recursively
fn collect_files(dir: &Path) -> ToolResult<Vec<PathBuf>> {
    let mut files = Vec::new();

    fn collect_recursive(dir: &Path, files: &mut Vec<PathBuf>) -> ToolResult<()> {
        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.is_dir() {
                collect_recursive(&path, files)?;
            } else if path.is_file() {
                files.push(path);
            }
        }
        Ok(())
    }

    collect_recursive(dir, &mut files)?;
    Ok(files)
}

/// Backup without compression (copy files)
fn backup_uncompressed(files: &[PathBuf], source_root: &Path, target: &Path) -> ToolResult<()> {
    fs::create_dir_all(target)?;

    for file in files {
        // Calculate relative path
        let rel_path = file
            .strip_prefix(source_root)
            .map_err(|e| format!("Failed to get relative path: {}", e))?;

        let target_file = target.join(rel_path);

        // Create parent directories
        if let Some(parent) = target_file.parent() {
            fs::create_dir_all(parent)?;
        }

        // Copy file
        fs::copy(file, &target_file)?;
        print!(".");
        io::stdout().flush()?;
    }

    println!();
    Ok(())
}

/// Backup with compression (simple tar-like format)
fn backup_compressed(files: &[PathBuf], source_root: &Path, target: &Path) -> ToolResult<()> {
    let target_file = if target.extension().is_some() {
        target.to_path_buf()
    } else {
        target.with_extension("tar")
    };

    let file = File::create(&target_file)?;
    let mut writer = BufWriter::new(file);

    for file_path in files {
        // Calculate relative path
        let rel_path = file_path
            .strip_prefix(source_root)
            .map_err(|e| format!("Failed to get relative path: {}", e))?;

        // Read file content
        let mut file = File::open(file_path)?;
        let metadata = file.metadata()?;
        let size = metadata.len();

        // Write header: path length (4 bytes) + path + size (8 bytes)
        let path_str = rel_path.to_string_lossy();
        let path_bytes = path_str.as_bytes();
        writer.write_all(&(path_bytes.len() as u32).to_le_bytes())?;
        writer.write_all(path_bytes)?;
        writer.write_all(&size.to_le_bytes())?;

        // Write content
        let mut buffer = vec![0u8; 8192];
        loop {
            let n = file.read(&mut buffer)?;
            if n == 0 {
                break;
            }
            writer.write_all(&buffer[..n])?;
        }

        print!(".");
        io::stdout().flush()?;
    }

    writer.flush()?;
    println!();

    Ok(())
}

/// Restore from uncompressed backup
fn restore_uncompressed(source: &Path, target: &Path) -> ToolResult<()> {
    // Simply copy all files from source to target
    copy_dir_recursive(source, target)?;
    Ok(())
}

/// Restore from compressed backup
fn restore_compressed(source: &Path, target: &Path) -> ToolResult<()> {
    let file = File::open(source)?;
    let mut reader = BufReader::new(file);

    loop {
        // Read path length
        let mut len_bytes = [0u8; 4];
        match reader.read_exact(&mut len_bytes) {
            Ok(_) => {}
            Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof => break,
            Err(e) => return Err(e.into()),
        }

        let path_len = u32::from_le_bytes(len_bytes) as usize;

        // Read path
        let mut path_bytes = vec![0u8; path_len];
        reader.read_exact(&mut path_bytes)?;
        let rel_path =
            String::from_utf8(path_bytes).map_err(|e| format!("Invalid UTF-8 in path: {}", e))?;

        // Read size
        let mut size_bytes = [0u8; 8];
        reader.read_exact(&mut size_bytes)?;
        let size = u64::from_le_bytes(size_bytes);

        // Create target file
        let target_file = target.join(&rel_path);
        if let Some(parent) = target_file.parent() {
            fs::create_dir_all(parent)?;
        }

        // Write content
        let mut file = File::create(&target_file)?;
        let mut remaining = size;
        let mut buffer = vec![0u8; 8192];

        while remaining > 0 {
            let to_read = remaining.min(buffer.len() as u64) as usize;
            reader.read_exact(&mut buffer[..to_read])?;
            file.write_all(&buffer[..to_read])?;
            remaining -= to_read as u64;
        }

        print!(".");
        io::stdout().flush()?;
    }

    println!();
    Ok(())
}

/// Copy directory recursively
fn copy_dir_recursive(source: &Path, target: &Path) -> ToolResult<()> {
    fs::create_dir_all(target)?;

    for entry in fs::read_dir(source)? {
        let entry = entry?;
        let file_type = entry.file_type()?;
        let source_path = entry.path();
        let target_path = target.join(entry.file_name());

        if file_type.is_dir() {
            copy_dir_recursive(&source_path, &target_path)?;
        } else if file_type.is_file() {
            fs::copy(&source_path, &target_path)?;
        }
    }

    Ok(())
}

/// Verify backup integrity
fn verify_backup(source: &Path, target: &Path, _compressed: bool) -> ToolResult<()> {
    // Basic verification: check file count and sizes match
    let source_files = collect_files(source)?;
    let source_size: u64 = source_files
        .iter()
        .map(|f| f.metadata().ok().map(|m| m.len()).unwrap_or(0))
        .sum();

    // For compressed backups, just check the archive exists
    if target.is_file() {
        let backup_size = target.metadata()?.len();
        if backup_size == 0 {
            return Err("Backup file is empty".into());
        }
        println!("  Source: {:.2} MB", source_size as f64 / 1_048_576.0);
        println!("  Backup: {:.2} MB", backup_size as f64 / 1_048_576.0);
    } else {
        // For uncompressed, check all files exist
        for source_file in &source_files {
            let rel_path = source_file
                .strip_prefix(source)
                .map_err(|e| format!("Failed to get relative path: {}", e))?;
            let target_file = target.join(rel_path);
            if !target_file.exists() {
                return Err(format!("File missing in backup: {}", rel_path.display()).into());
            }
        }
        println!("  {} file(s) verified", source_files.len());
    }

    Ok(())
}

/// Verify restore integrity
fn verify_restore(target: &Path) -> ToolResult<()> {
    // Basic verification: check directory is readable and contains files
    if !target.exists() {
        return Err("Restore target does not exist".into());
    }

    if !target.is_dir() {
        return Err("Restore target is not a directory".into());
    }

    let files = collect_files(target)?;
    if files.is_empty() {
        return Err("No files found in restore target".into());
    }

    println!("  {} file(s) restored", files.len());
    Ok(())
}

/// Write backup metadata
fn write_backup_metadata(
    config: &BackupConfig,
    files: &[PathBuf],
    total_size: u64,
) -> ToolResult<()> {
    let metadata = BackupMetadata {
        timestamp: SystemTime::now(),
        source_path: config.source.clone(),
        file_count: files.len(),
        total_size,
        compressed: config.compress,
        incremental: config.incremental,
    };

    let metadata_path = if config.target.is_dir() {
        config.target.join("backup_metadata.txt")
    } else {
        config.target.with_extension("metadata.txt")
    };

    let mut file = File::create(metadata_path)?;
    writeln!(file, "OxiRS Backup Metadata")?;
    writeln!(file, "====================")?;
    writeln!(file, "Timestamp: {:?}", metadata.timestamp)?;
    writeln!(file, "Source: {}", metadata.source_path.display())?;
    writeln!(file, "Files: {}", metadata.file_count)?;
    writeln!(file, "Size: {} bytes", metadata.total_size)?;
    writeln!(file, "Compressed: {}", metadata.compressed)?;
    writeln!(file, "Incremental: {}", metadata.incremental)?;

    Ok(())
}