mirror of
https://github.com/edgebox-iot/edgeboxctl.git
synced 2026-09-24 05:41:43 +02:00
Added system.CopyDir and EdgeappsBackupPath conf
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
"log"
|
||||
"os"
|
||||
"io"
|
||||
"os/exec"
|
||||
"bufio"
|
||||
"path/filepath"
|
||||
@@ -302,3 +303,78 @@ func RemoveTunnelService() {
|
||||
utils.Exec(wsPath, "rm", cmdargs)
|
||||
}
|
||||
|
||||
func CopyDir(src string, dest string) error {
|
||||
srcInfo, err := os.Stat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !srcInfo.IsDir() {
|
||||
return fmt.Errorf("%s is not a directory", src)
|
||||
}
|
||||
|
||||
err = os.MkdirAll(dest, srcInfo.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
items, err := ioutil.ReadDir(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, item := range items {
|
||||
srcPath := filepath.Join(src, item.Name())
|
||||
destPath := filepath.Join(dest, item.Name())
|
||||
|
||||
if item.IsDir() {
|
||||
err = CopyDir(srcPath, destPath)
|
||||
if err != nil {
|
||||
fmt.Printf("error copying directory %s to %s: %s\n", srcPath, destPath, err.Error())
|
||||
}
|
||||
} else {
|
||||
err = CopyFile(srcPath, destPath)
|
||||
if err != nil {
|
||||
fmt.Printf("error copying file %s to %s: %s\n", srcPath, destPath, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CopyFile(src string, dest string) error {
|
||||
srcFile, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer srcFile.Close()
|
||||
|
||||
destFile, err := os.Create(dest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer destFile.Close()
|
||||
|
||||
_, err = io.Copy(destFile, srcFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = destFile.Sync()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
srcInfo, err := os.Stat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.Chmod(dest, srcInfo.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -626,7 +626,7 @@ func taskRestoreBackup() string {
|
||||
// Copy all files in /home/system/components/apps/ to a backup folder
|
||||
fmt.Println("Copying all files in /home/system/components/apps/ to a backup folder")
|
||||
os.MkdirAll(utils.GetPath(utils.EdgeAppsBackupPath + "temp/"), 0777)
|
||||
utils.CopyDir(utils.GetPath(utils.EdgeAppsPath), utils.GetPath(utils.EdgeAppsBackupPath + "temp/"))
|
||||
system.CopyDir(utils.GetPath(utils.EdgeAppsPath), utils.GetPath(utils.EdgeAppsBackupPath + "temp/"))
|
||||
|
||||
fmt.Println("Removing all files in /home/system/components/apps/")
|
||||
os.RemoveAll(utils.GetPath(utils.EdgeAppsPath))
|
||||
@@ -649,7 +649,7 @@ func taskRestoreBackup() string {
|
||||
if strings.Contains(result, "Fatal:") {
|
||||
// Copy all files from backup folder to /home/system/components/apps/
|
||||
os.MkdirAll(utils.GetPath(utils.EdgeAppsPath), 0777)
|
||||
utils.CopyDir(utils.GetPath(utils.EdgeAppsBackupPath + "temp/"), utils.GetPath(utils.EdgeAppsPath))
|
||||
system.CopyDir(utils.GetPath(utils.EdgeAppsBackupPath + "temp/"), utils.GetPath(utils.EdgeAppsPath))
|
||||
|
||||
fmt.Println("Error restoring backup: ")
|
||||
utils.WriteOption("BACKUP_STATUS", "error")
|
||||
|
||||
@@ -109,6 +109,7 @@ const CloudEnvFileLocation string = "cloudEnvFileLocation"
|
||||
const ApiEnvFileLocation string = "apiEnvFileLocation"
|
||||
const ApiPath string = "apiPath"
|
||||
const EdgeAppsPath string = "edgeAppsPath"
|
||||
const EdgeAppsBackupPath string = "edgeAppsBackupPath"
|
||||
const WsPath string = "wsPath"
|
||||
|
||||
// GetPath : Returns either the hardcoded path, or a overwritten value via .env file at project root. Register paths here for seamless working code between dev and prod environments ;)
|
||||
@@ -156,6 +157,13 @@ func GetPath(pathKey string) string {
|
||||
targetPath = "/home/system/components/apps/"
|
||||
}
|
||||
|
||||
case EdgeAppsBackupPath:
|
||||
if env["EDGEAPPS_BACKUP_PATH"] != "" {
|
||||
targetPath = env["EDGEAPPS_BACKUP_PATH"]
|
||||
} else {
|
||||
targetPath = "/home/system/components/backups/"
|
||||
}
|
||||
|
||||
case WsPath:
|
||||
|
||||
if env["WS_PATH"] != "" {
|
||||
|
||||
Reference in New Issue
Block a user