리팩토링
This commit is contained in:
61
internal/backup/backup.go
Normal file
61
internal/backup/backup.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package backup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.lhk.o-r.kr/freerer2/simple_backup/internal/copy"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// RunBackup: 백업 실행 함수
|
||||
func RunBackup(src, dst string, dryRun, verbose, force bool) error {
|
||||
if !dryRun {
|
||||
// 백업 폴더 생성
|
||||
if err := os.MkdirAll(dst, 0755); err != nil {
|
||||
return fmt.Errorf("백업 폴더 생성 실패: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
relPath, err := filepath.Rel(src, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
targetPath := filepath.Join(dst, relPath)
|
||||
|
||||
if info.IsDir() {
|
||||
if verbose {
|
||||
fmt.Println("디렉터리 생성:", targetPath)
|
||||
}
|
||||
if !dryRun {
|
||||
return os.MkdirAll(targetPath, info.Mode())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if dryRun {
|
||||
fmt.Println("복사 예정 파일:", targetPath)
|
||||
return nil
|
||||
}
|
||||
|
||||
// force 옵션이 없으면 수정 시간 비교
|
||||
if !force {
|
||||
dstInfo, err := os.Stat(targetPath)
|
||||
if err == nil && !info.ModTime().After(dstInfo.ModTime()) {
|
||||
if verbose {
|
||||
fmt.Println("복사 스킵 (업데이트 필요 없음):", path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if verbose {
|
||||
fmt.Println("파일 복사:", path, "→", targetPath)
|
||||
}
|
||||
return copy.RunCopy(path, targetPath)
|
||||
})
|
||||
}
|
||||
49
internal/copy/copy.go
Normal file
49
internal/copy/copy.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package copy
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// CopyFile 복사 함수: srcFile → dstFile
|
||||
func RunCopy(srcFile, dstFile string) error {
|
||||
src, err := os.Open(srcFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer func(src *os.File) {
|
||||
err := src.Close()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}(src)
|
||||
|
||||
dst, err := os.Create(dstFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer func(dst *os.File) {
|
||||
err := dst.Close()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}(dst)
|
||||
|
||||
_, err = io.Copy(dst, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 원본 파일의 권한을 복사
|
||||
info, err := os.Stat(srcFile)
|
||||
if err == nil {
|
||||
err := os.Chmod(dstFile, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
29
internal/path/path.go
Normal file
29
internal/path/path.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package path
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GetGroupByFolder: group-by 옵션에 따라 백업 경로 생성
|
||||
func GetGroupByFolder(basePath, groupBy string, t time.Time) string {
|
||||
switch groupBy {
|
||||
case "year":
|
||||
return fmt.Sprintf("%s/%d", basePath, t.Year())
|
||||
case "mon":
|
||||
return fmt.Sprintf("%s/%d%02d", basePath, t.Year(), t.Month())
|
||||
case "day":
|
||||
return fmt.Sprintf("%s/%d%02d%02d", basePath, t.Year(), t.Month(), t.Day())
|
||||
case "hour":
|
||||
return fmt.Sprintf("%s/%d%02d%02d_%02d", basePath, t.Year(), t.Month(), t.Day(), t.Hour())
|
||||
case "min":
|
||||
return fmt.Sprintf("%s/%d%02d%02d_%02d%02d", basePath, t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute())
|
||||
case "sec":
|
||||
return fmt.Sprintf("%s/%d%02d%02d_%02d%02d%02d", basePath, t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
|
||||
case "":
|
||||
return basePath
|
||||
default:
|
||||
fmt.Printf("지원하지 않는 group-by 옵션입니다: %s\n", groupBy)
|
||||
return basePath
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user