Skip to content

feat: add option for shutting down pc after backup #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Config struct {
BackupSourceDir string `json:"backupdir"`
PxarOut string `json:"pxarout"`
SMTP *SMTPConfig `json:"smtp"`
Shutdown bool `json:"shutdown"`
}

func (c *Config) valid() bool {
Expand Down Expand Up @@ -76,6 +77,8 @@ func loadConfig() *Config {

configFile := flag.String("config", "", "Path to JSON config file. If this flag is provided all the others are ignored")

shutdownFlag := flag.Bool("shutdown", false, "Shutdown the system after backup")

// Parse command line flags
flag.Parse()

Expand Down Expand Up @@ -104,6 +107,7 @@ func loadConfig() *Config {
config.BackupID = *backupIDFlag
config.BackupSourceDir = *backupSourceDirFlag
config.PxarOut = *pxarOutFlag
config.Shutdown = *shutdownFlag

initSmtpConfigIfNeeded := func() {
if config.SMTP == nil {
Expand Down
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"hash"
"os"
"os/exec"
"runtime"
"sync/atomic"

Expand Down Expand Up @@ -130,6 +131,9 @@ func main() {
}
}

if cfg.Shutdown {
shutDownPc()
}
}

func backup(client *PBSClient, newchunk, reusechunk *atomic.Uint64, pxarOut string, backupdir string) error {
Expand Down Expand Up @@ -371,3 +375,15 @@ func backup(client *PBSClient, newchunk, reusechunk *atomic.Uint64, pxarOut stri

return client.Finish()
}

func shutDownPc() {
if runtime.GOOS == "windows" {
if err := exec.Command("cmd", "/C", "shutdown", "/s", "/t", "0").Run(); err != nil {
fmt.Println("Failed to initiate shutdown:", err)
}
} else {
if err := exec.Command("shutdown", "now").Run(); err != nil {
fmt.Println("Failed to initiate shutdown:", err)
}
}
}