Skip to content

Commit cbb22ad

Browse files
committed
Implement export/import functionality
1 parent d063db1 commit cbb22ad

File tree

7 files changed

+626
-3
lines changed

7 files changed

+626
-3
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Unlike traditional databases, KV is designed for simplicity and speed. No server
4747
- [Version Control & History](#version-control--history)
4848
- [Output Formats](#output-formats)
4949
- [Batch Operations & Multiple Keys](#batch-operations--multiple-keys)
50+
- [Backup & Restore](#backup--restore)
5051
- [Utility Commands](#utility-commands)
5152
- [Configuration](#configuration)
5253
- [Data Storage](#data-storage)
@@ -430,6 +431,66 @@ kv unlock --all --password "mypass"
430431
# none of the changes are applied (all-or-nothing behavior)
431432
```
432433
434+
### Backup & Restore
435+
436+
> **Note:** Export creates a complete snapshot of your database including all keys, values, encryption, hidden state, TTL settings, and full history. Import completely replaces your current database with the imported one, creating a backup of your current database first.
437+
438+
```bash
439+
# Export database to a file
440+
kv db export backup.db
441+
# Output: Database exported to: backup.db
442+
443+
# Export to absolute path
444+
kv db export /path/to/backups/kv-backup-2025-10-20.db
445+
446+
# Overwrite existing backup file
447+
kv db export backup.db --force
448+
449+
# Export to stdout (useful for piping)
450+
kv db export - > backup.db
451+
# Or simply:
452+
kv db export > backup.db
453+
454+
# Import database from a file (replaces current database)
455+
kv db import backup.db
456+
# Output:
457+
# Current database backed up to: /path/to/kv.db.backup
458+
# Database imported successfully
459+
460+
# Import from stdin
461+
cat backup.db | kv db import -
462+
# Or simply:
463+
kv db import < backup.db
464+
465+
# Practical examples:
466+
467+
# Create daily backups
468+
kv db export ~/backups/kv-$(date +%Y-%m-%d).db
469+
470+
# Transfer database between machines
471+
# On source machine:
472+
kv db export - | ssh user@remote 'kv db import -'
473+
474+
# Compress backup
475+
kv db export - | gzip > kv-backup.db.gz
476+
# Restore from compressed backup
477+
gunzip -c kv-backup.db.gz | kv db import -
478+
```
479+
480+
**What gets preserved in export/import:**
481+
- All keys and values (plain text, encrypted, and hidden)
482+
- Password-encrypted keys (with their encryption intact)
483+
- Hidden/visible state
484+
- TTL and expiration settings
485+
- Complete version history for all keys
486+
- All configuration and metadata
487+
488+
**Safety features:**
489+
- Import automatically creates a backup at `<db-path>.backup` before replacing
490+
- Import validates the file is a valid database before proceeding
491+
- Export fails if file exists (use `--force` to override)
492+
- Export validates destination directory exists
493+
433494
### Utility Commands
434495
435496
```bash

src/cmd/db.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cmd
2+
3+
import "github.com/spf13/cobra"
4+
5+
// dbCmd represents the db command
6+
var dbCmd = &cobra.Command{
7+
Use: "db",
8+
Short: "Database operations",
9+
}
10+
11+
func init() {
12+
rootCmd.AddCommand(dbCmd)
13+
}

src/cmd/export.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/AmrSaber/kv/src/common"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var exportFlags = struct{ force bool }{}
14+
15+
// exportCmd represents the export command
16+
var exportCmd = &cobra.Command{
17+
Use: "export <file-path>",
18+
Short: "Export database to a file",
19+
Long: `Export the entire database to a binary file.
20+
21+
The exported file captures the complete state of the database and can be
22+
imported later using the 'import' command to restore the exact state.
23+
24+
Use "-" as the file path to write to stdout (useful for piping).`,
25+
Example: ` # Export database to a file
26+
kv db export my-backup
27+
28+
# Export to stdout
29+
kv db export > backup.db # Or using "-" as file name
30+
31+
# Export to absolute path
32+
kv db export /path/to/backup
33+
34+
# Overwrite existing file
35+
kv db export backup.db --force`,
36+
Args: cobra.MaximumNArgs(1),
37+
Run: func(cmd *cobra.Command, args []string) {
38+
// Handle stdout
39+
if len(args) == 0 || args[0] == "-" {
40+
exportToStdout()
41+
return
42+
}
43+
44+
destPath := common.NormalizePath(args[0])
45+
46+
// Check if directory exists
47+
dir := filepath.Dir(destPath)
48+
if _, err := os.Stat(dir); os.IsNotExist(err) {
49+
common.Fail("Directory does not exist: %s", dir)
50+
}
51+
52+
// Check if file already exists
53+
if _, err := os.Stat(destPath); err == nil {
54+
if !exportFlags.force {
55+
common.Fail("File already exists: %s (use --force to overwrite)", destPath)
56+
}
57+
58+
// Remove existing file when --force is used
59+
err = os.Remove(destPath)
60+
common.FailOn(err)
61+
}
62+
63+
// Export using VACUUM INTO
64+
db := common.GetDB()
65+
_, err := db.Exec("VACUUM INTO ?", destPath)
66+
common.FailOn(err)
67+
68+
fmt.Printf("Database exported to: %s\n", destPath)
69+
},
70+
}
71+
72+
func exportToStdout() {
73+
// Create a temporary file for VACUUM INTO
74+
tmpFile, err := os.CreateTemp("", "kv-export-*")
75+
common.FailOn(err)
76+
tmpPath := tmpFile.Name()
77+
err = tmpFile.Close()
78+
common.FailOn(err)
79+
defer func() { _ = os.Remove(tmpPath) }()
80+
81+
// Export to temp file
82+
db := common.GetDB()
83+
_, err = db.Exec("VACUUM INTO ?", tmpPath)
84+
common.FailOn(err)
85+
86+
// Copy temp file to stdout
87+
file, err := os.Open(tmpPath)
88+
common.FailOn(err)
89+
defer func() { _ = file.Close() }()
90+
91+
_, err = io.Copy(os.Stdout, file)
92+
common.FailOn(err)
93+
}
94+
95+
func init() {
96+
dbCmd.AddCommand(exportCmd)
97+
98+
exportCmd.Flags().BoolVarP(&exportFlags.force, "force", "f", false, "Overwrite existing file if it exists")
99+
}

src/cmd/import.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
8+
"github.com/AmrSaber/kv/src/common"
9+
"github.com/spf13/cobra"
10+
_ "modernc.org/sqlite"
11+
)
12+
13+
// importCmd represents the import command
14+
var importCmd = &cobra.Command{
15+
Use: "import <file-path>",
16+
Short: "Import database from a file",
17+
Long: `Import a database from a binary file and replace the current database.
18+
19+
The imported file must be a valid database file created with the 'export' command.
20+
This will completely replace the current database with the imported one.
21+
22+
WARNING: This operation is destructive. The current database will be backed up
23+
to <db-path>.backup before importing.
24+
25+
Use "-" as the file path to read from stdin (useful for piping).`,
26+
Example: ` # Import database from a file
27+
kv db import backup.db
28+
29+
# Import from stdin - you also get UUOC award :)
30+
cat backup.db | kv db import # Or use "-" as file name
31+
32+
# Import from absolute path
33+
kv db import /path/to/backup`,
34+
Args: cobra.MaximumNArgs(1),
35+
Run: func(cmd *cobra.Command, args []string) {
36+
sourcePath := "-"
37+
if len(args) > 0 {
38+
sourcePath = args[0]
39+
}
40+
41+
// Handle stdin
42+
if sourcePath == "-" {
43+
// Create temporary file for stdin content
44+
tmpFile, err := os.CreateTemp("", "kv-import-*")
45+
common.FailOn(err)
46+
defer func() { _ = os.Remove(tmpFile.Name()) }()
47+
48+
// Copy stdin to temp file
49+
_, err = io.Copy(tmpFile, os.Stdin)
50+
common.FailOn(err)
51+
err = tmpFile.Close()
52+
common.FailOn(err)
53+
54+
sourcePath = tmpFile.Name()
55+
} else {
56+
sourcePath = common.NormalizePath(sourcePath)
57+
58+
// Check if source file exists
59+
if _, err := os.Stat(sourcePath); os.IsNotExist(err) {
60+
common.Fail("File does not exist: %s", sourcePath)
61+
}
62+
}
63+
64+
// Validate source is a valid SQLite database
65+
if err := common.ValidateSqliteFile(sourcePath); err != nil {
66+
common.Fail("Invalid database file: %v", err)
67+
}
68+
69+
// Get destination path
70+
destPath := common.GetDBPath()
71+
backupPath := destPath + ".backup"
72+
73+
// Vacuum current database to commit all WAL changes to main file
74+
db := common.GetDB()
75+
_, err := db.Exec("VACUUM")
76+
common.FailOn(err)
77+
78+
// Close database connection
79+
common.CloseDB()
80+
81+
// Remove old backup if exists
82+
_ = os.Remove(backupPath)
83+
84+
// Backup current database if it exists
85+
if _, err := os.Stat(destPath); err == nil {
86+
err = os.Rename(destPath, backupPath)
87+
if err != nil {
88+
common.Fail("Failed to backup current database: %v", err)
89+
}
90+
fmt.Printf("Current database backed up to: %s\n", backupPath)
91+
}
92+
93+
// Remove WAL files (should be empty after VACUUM, but remove just in case)
94+
_ = os.Remove(destPath + "-wal")
95+
_ = os.Remove(destPath + "-shm")
96+
97+
// Copy source to destination
98+
err = common.CopyFile(sourcePath, destPath)
99+
if err != nil {
100+
if _, err := os.Stat(backupPath); err == nil {
101+
_ = os.Remove(destPath)
102+
_ = os.Rename(backupPath, destPath)
103+
}
104+
105+
common.Fail("Failed to import database: %v", err)
106+
}
107+
108+
// Reopen database (migrations will run automatically)
109+
common.GetDB()
110+
111+
fmt.Println("Database imported successfully")
112+
},
113+
}
114+
115+
func init() {
116+
dbCmd.AddCommand(importCmd)
117+
}

src/common/db.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ var pragmas = []string{
1616
`PRAGMA busy_timeout = 5000`,
1717
}
1818

19+
func CloseDB() {
20+
if db != nil {
21+
_ = db.Close()
22+
db = nil
23+
}
24+
}
25+
1926
func ClearDB() {
20-
dbPath := path.Dir(getDBPath())
27+
dbPath := path.Dir(GetDBPath())
2128
err := os.RemoveAll(dbPath)
2229
FailOn(err)
2330
}
@@ -31,7 +38,7 @@ func GetDB() *sql.DB {
3138
}
3239

3340
func openDB() *sql.DB {
34-
dbPath := getDBPath()
41+
dbPath := GetDBPath()
3542
_ = os.MkdirAll(path.Dir(dbPath), os.ModeDir|os.ModePerm)
3643

3744
db, err := sql.Open("sqlite", dbPath+"?_txlock=immediate")
@@ -60,11 +67,23 @@ func openDB() *sql.DB {
6067
return db
6168
}
6269

63-
func getDBPath() string {
70+
func GetDBPath() string {
6471
scope := gap.NewScope(gap.User, "kv")
6572

6673
dbPath, err := scope.DataPath("kv.db")
6774
FailOn(err)
6875

6976
return dbPath
7077
}
78+
79+
func ValidateSqliteFile(path string) error {
80+
testDB, err := sql.Open("sqlite", path+"?mode=ro")
81+
if err != nil {
82+
return err
83+
}
84+
85+
defer func() { _ = testDB.Close() }()
86+
87+
// Try to query to ensure it's actually valid
88+
return testDB.Ping()
89+
}

src/common/files.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package common
2+
3+
import (
4+
"io"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
func CopyFile(src, dst string) error {
10+
sourceFile, err := os.Open(src)
11+
if err != nil {
12+
return err
13+
}
14+
defer func() { _ = sourceFile.Close() }()
15+
16+
destFile, err := os.Create(dst)
17+
if err != nil {
18+
return err
19+
}
20+
defer func() { _ = destFile.Close() }()
21+
22+
_, err = io.Copy(destFile, sourceFile)
23+
return err
24+
}
25+
26+
func NormalizePath(path string) string {
27+
if len(path) > 0 && path[0] == '~' {
28+
home, err := os.UserHomeDir()
29+
FailOn(err)
30+
path = filepath.Join(home, path[1:])
31+
}
32+
33+
// Make path absolute
34+
absPath, err := filepath.Abs(path)
35+
FailOn(err)
36+
37+
return absPath
38+
}

0 commit comments

Comments
 (0)