리팩토링
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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user