backup: mv tmp snap file into tmp/

This commit is contained in:
Hongchao Deng 2016-10-30 20:48:02 -07:00
Родитель d446c8b0ee
Коммит c5f1745128
1 изменённых файлов: 17 добавлений и 7 удалений

Просмотреть файл

@ -17,10 +17,9 @@ package backup
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"time"
"golang.org/x/net/context"
@ -35,6 +34,11 @@ import (
"k8s.io/kubernetes/pkg/client/unversioned"
)
const (
backupTmpDir = "tmp"
backupFilePerm = 0600
)
type Backup struct {
kclient *unversioned.Client
@ -59,8 +63,10 @@ func New(kclient *unversioned.Client, clusterName, ns string, policy spec.Backup
}
func (b *Backup) Run() {
// It will be no-op if backup dir existed.
if err := os.MkdirAll(constants.BackupDir, 0700); err != nil {
// We created not only backup dir and but also tmp dir under it.
// tmp dir is used to store intermediate snapshot files.
// It will be no-op if target dir existed.
if err := os.MkdirAll(filepath.Join(constants.BackupDir, backupTmpDir), 0700); err != nil {
panic(err)
}
@ -141,8 +147,11 @@ func writeSnap(m *etcdutil.Member, backupDir string, rev int64) error {
}
defer rc.Close()
// TODO: custom backup dir
tmpfile, err := ioutil.TempFile(backupDir, "snapshot")
filename := makeFilename(rev)
tmpfile, err := os.OpenFile(filepath.Join(backupDir, backupTmpDir, filename), os.O_WRONLY|O_TRUNC|O_CREATE, backupFilePerm)
if err != nil {
return fmt.Errorf("failed to create snapshot tempfile: %v", err)
}
n, err := io.Copy(tmpfile, rc)
if err != nil {
tmpfile.Close()
@ -151,7 +160,8 @@ func writeSnap(m *etcdutil.Member, backupDir string, rev int64) error {
}
cancel()
tmpfile.Close()
nextSnapshotName := path.Join(backupDir, makeFilename(rev))
nextSnapshotName := filepath.Join(backupDir, filename)
err = os.Rename(tmpfile.Name(), nextSnapshotName)
if err != nil {
os.Remove(tmpfile.Name())