Skip to content
Permalink
Browse files

Add database backup

  • Loading branch information...
Raggaer committed Feb 22, 2019
1 parent b14f946 commit db727cb0812f1f2b49b40e5afbe4982dc82061df
Showing with 28 additions and 0 deletions.
  1. +2 −0 README.md
  2. +26 −0 save.go
@@ -32,6 +32,8 @@ You can use the `help` command while a database is opened to view the list of co
When an entry or a group is deleted we move it to the `Recycle Bin` group (this will be created if its missing).
You can delete delete the entry forever or just leave it there as some sort of backup folder

After deleting a backup of the database file is created (before the delete change) with the format `y-m-d_h:i:s_name.kdbx`

## Clipboard

Commands like `xu` and `xp` copy the content to the system clipboard, making use of [https://github.com/atotto/clipboard](https://github.com/atotto/clipboard).
26 save.go
@@ -2,8 +2,11 @@ package main

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"

"github.com/tobischo/gokeepasslib/v2"
)
@@ -16,6 +19,12 @@ func databaseChangedSaveAlert(f *form, answer string) {
defer func() {
activeForm = nil
}()
// First we backup the database
if err := backupDatabase(); err != nil {
fmt.Println("Unable to backup database:")
fmt.Println(err.Error())
return
}
if strings.TrimSpace(answer) != "y" {
return
}
@@ -40,3 +49,20 @@ func saveDatabase() error {
}
return openDatabaseFile(databaseLocation)
}

func backupDatabase() error {
src, err := os.Open(databaseLocation)
if err != nil {
return err
}
defer src.Close()
dir, file := filepath.Split(databaseLocation)
backupName := filepath.Join(dir, time.Now().Format("2006-01-02_15:04:05")+"_"+file)
dst, err := os.Create(backupName)
if err != nil {
return err
}
defer dst.Close()
_, err = io.Copy(dst, src)
return err
}

0 comments on commit db727cb

Please sign in to comment.
You can’t perform that action at this time.