new: optional 'run' backups configuration for an optional post backup command

This commit is contained in:
evilsocket
2017-12-24 16:38:11 +01:00
parent fa10a24102
commit 8cf4d3dd1b
5 changed files with 51 additions and 6 deletions

View File

@@ -109,7 +109,8 @@ This is the example configuration file you need to customize the first time.
"backups": {
"enabled": false,
"period": 1800,
"folder": "/some/backup/path/"
"folder": "/some/backup/path/",
"run": "scp arc-backup.tar user@backup-server:/media/arc_backup/"
}
}
```
@@ -142,6 +143,7 @@ It is necessary to change only the `username` and `password` access parameters o
| backups.enabled | Enable automatic backups. |
| backups.period | Number of seconds between one backup and the next one. |
| backups.folder | Destination folder for the backup file. |
| backups.run | If filled, this command will be executed after the backup archive is created. |
## Realtime Notifications

View File

@@ -8,14 +8,17 @@
package backup
import (
"fmt"
"github.com/evilsocket/arc/arcd/db"
"github.com/evilsocket/arc/arcd/log"
"github.com/evilsocket/arc/arcd/utils"
"os/exec"
"path"
"runtime"
"time"
)
func worker(secs int, folder string) {
func worker(secs int, folder string, cmd string) {
period := time.Duration(secs) * time.Second
filename := path.Join(folder, "arc-backup.tar")
@@ -28,6 +31,44 @@ func worker(secs int, folder string) {
log.Errorf("Error while creating the backup file: %s.", err)
} else {
log.Infof("Backupped %s of data to %s in %s.", utils.FormatBytes(db.Size), log.Bold(filename), time.Since(started))
if cmd != "" {
log.Infof("Running %s ...", log.Bold(cmd))
var timer *time.Timer
var c *exec.Cmd
// make sure commands don't get stucked for more
// than we are configured to wait.
timer = time.AfterFunc(period, func() {
timer.Stop()
if c != nil {
log.Warningf("Command timed out, killing.")
c.Process.Kill()
}
})
cmd = fmt.Sprintf("cd '%s' && %s", folder, cmd)
started := time.Now()
if runtime.GOOS == "windows" {
c = exec.Command("cmd", "/C", cmd)
} else {
c = exec.Command("sh", "-c", cmd)
}
output, err := c.CombinedOutput()
if err != nil {
log.Errorf("Error: %s", err)
}
if output != nil && len(output) > 0 {
log.Infof("Output: %s", log.Bold(string(output)))
}
log.Infof("Command ran in %s.", time.Since(started))
}
}
time.Sleep(period)
@@ -35,6 +76,6 @@ func worker(secs int, folder string) {
}
func Start(period int, folder string) {
go worker(period, folder)
func Start(period int, folder string, cmd string) {
go worker(period, folder, cmd)
}

View File

@@ -73,6 +73,7 @@ type bkConfig struct {
Enabled bool `json:"enabled"`
Period int `json:"period"`
Folder string `json:"folder"`
Run string `json:"run"`
}
// Arc server configuration.

View File

@@ -136,7 +136,7 @@ func setupScheduler() {
func setupBackups() {
if config.Conf.Backups.Enabled {
log.Debugf("Starting backup task with a period of %ds ...", config.Conf.Backups.Period)
backup.Start(config.Conf.Backups.Period, config.Conf.Backups.Folder)
backup.Start(config.Conf.Backups.Period, config.Conf.Backups.Folder, config.Conf.Backups.Run)
} else {
log.Importantf("Backups are disabled.")
}

View File

@@ -35,6 +35,7 @@
"backups": {
"enabled": false,
"period": 1800,
"folder": "/some/backup/path/"
"folder": "/some/backup/path/",
"run": "scp arc-backup.tar user@backup-server:/media/arc_backup/"
}
}