@@ -30,19 +30,12 @@ func overrideCronFile(filePath string, jobs []string) error {
3030 return writer .Flush ()
3131}
3232
33- func isBorgInit (repo string ) (bool , error ) {
34- cmd := exec .Command ("borg" , "info" , repo )
35-
36- err := cmd .Run ()
37- if err != nil {
38- if exitError , ok := err .(* exec.ExitError ); ok {
39- if exitError .ExitCode () == 2 {
40- return false , nil
41- }
42- }
43- return false , err
33+ func isBorgInit (repo string ) bool {
34+ dir , err := os .ReadDir (repo )
35+ if err != nil || len (dir ) == 0 {
36+ return false
4437 }
45- return true , err
38+ return true
4639}
4740
4841func main () {
@@ -57,10 +50,8 @@ func main() {
5750 if _ , err := os .ReadDir (src ); err != nil {
5851 _log .Log ("error" , repo + " does not exist can't perform backup" )
5952 }
60- is , err := isBorgInit (repo )
61- if err != nil {
62- log .Fatal (err )
63- } else if is == true {
53+ is := isBorgInit (repo )
54+ if is == true {
6455 _log .Log ("note" , "Repo already initialize, skipping..." )
6556 } else {
6657 _log .Log ("note" , "Initializing repo..." )
@@ -70,7 +61,7 @@ func main() {
7061 _log .Log ("error" , "No passphrase specified, exiting..." )
7162 }
7263
73- err = cmd .ExecCmd ([]string {"borg" , "init" , "--encryption=repokey-blake2" , repo }, slices .Insert (os .Environ (), len (os .Environ ()), "BORG_PASSPHRASE=" + passphrase ))
64+ err : = cmd .ExecCmd ([]string {"borg" , "init" , "--encryption=repokey-blake2" , repo }, slices .Insert (os .Environ (), len (os .Environ ()), "BORG_PASSPHRASE=" + passphrase ))
7465 if err != nil {
7566 log .Fatal (err )
7667 }
@@ -80,9 +71,9 @@ func main() {
8071 cronFilePath := "/etc/crontabs/root"
8172 newJobs := []string {
8273 "# Borg Backup Cron Job" ,
83- interval + " root run-parts /docker-backup.d >> /var/log/cron.log 2>&1" ,
74+ interval + " run-parts /docker-backup.d >> /var/log/cron.log 2>&1" ,
8475 }
85- err = overrideCronFile (cronFilePath , newJobs )
76+ err : = overrideCronFile (cronFilePath , newJobs )
8677 if err != nil {
8778 fmt .Printf ("Error overriding cron file: %v\n " , err )
8879 } else {
0 commit comments