Skip to content

Commit

Permalink
data backup
Browse files Browse the repository at this point in the history
  • Loading branch information
qishenonly committed Jan 22, 2024
1 parent 38d7ec4 commit d9c6d82
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions lib/backup/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package backup

import (
"io"
"os"
"path/filepath"
)

// CopyFile copies a file from src to dest.
func copyFile(src, dest string, mode os.FileMode) error {
// Open the source file
srcFile, err := os.OpenFile(src, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
if err != nil {
return err
}
defer func(srcFile *os.File) {
err = srcFile.Close()
if err != nil {
panic(err)
}
}(srcFile)

// Create the destination file
destFile, err := os.Create(dest)
if err != nil {
return err
}
defer func(destFile *os.File) {
err = destFile.Close()
if err != nil {
panic(err)
}
}(destFile)

// Copy the contents of the source file to the destination file
if _, err = io.Copy(destFile, srcFile); err != nil {
return err
}

// Set permissions on destination files
return os.Chmod(dest, mode)
}

// CopyDir copies a directory from src to dest.
func CopyDir(src, dest string) error {
// Get the source directory information
srcInfo, err := os.Stat(src)
if err != nil {
return err
}

// Create the destination directory
if err = os.MkdirAll(dest, srcInfo.Mode()); err != nil {
return err
}

// Get the contents of the source directory
entries, err := os.ReadDir(src)
if err != nil {
return err
}

// Copy the contents of the source directory to the destination directory
for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
destPath := filepath.Join(dest, entry.Name())

if entry.IsDir() {
// If it is a subdirectory, call CopyDir recursively
if err = CopyDir(srcPath, destPath); err != nil {
return err
}
} else {
// If it is a file, call copyFile to copy the file content
if err = copyFile(srcPath, destPath, entry.Type()); err != nil {
return err
}
}
}

return nil
}

0 comments on commit d9c6d82

Please sign in to comment.