[FIX] fix restore app data bug, if have same volume parent dir

This commit is contained in:
barnettZQG 2018-06-29 17:40:19 +08:00
parent 08ebb30f64
commit c9380f2740
3 changed files with 25 additions and 0 deletions

View File

@ -229,6 +229,11 @@ func (b *BackupAPPRestore) restoreVersionAndData(backup *dbmodel.AppBackup, appS
}
err := util.Rename(tmpDir, util.GetParentDirectory(app.Service.HostPath))
if err != nil {
if strings.Contains(err.Error(), "file exists") {
if err := util.MergeDir(tmpDir, util.GetParentDirectory(app.Service.HostPath)); err != nil {
return err
}
}
return err
}
if err := os.Chmod(app.Service.HostPath, 0777); err != nil {

View File

@ -513,6 +513,20 @@ func Rename(old, new string) error {
return os.Rename(old, new)
}
//MergeDir MergeDir
func MergeDir(fromdir, todir string) error {
files, err := ioutil.ReadDir(fromdir)
if err != nil {
return err
}
for _, f := range files {
if err := os.Rename(path.Join(fromdir, f.Name()), path.Join(todir, f.Name())); err != nil {
return err
}
}
return nil
}
//CreateVersionByTime create version number
func CreateVersionByTime() string {
now := time.Now()

View File

@ -74,3 +74,9 @@ func TestGetDirList(t *testing.T) {
}
t.Log(list)
}
func TestMergeDir(t *testing.T) {
if err := MergeDir("/tmp/ctr-944254844/", "/tmp/cache"); err != nil {
t.Fatal(err)
}
}