github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/store/backup.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package store
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os"
    20  	"path/filepath"
    21  	"strconv"
    22  
    23  	"github.com/coreos/rkt/pkg/fileutil"
    24  	"github.com/coreos/rkt/pkg/uid"
    25  )
    26  
    27  // createBackup backs a database up in a given directory. It basically
    28  // copies this directory into a given backups directory. The backups
    29  // directory has a simple structure - a directory inside named "0" is
    30  // the most recent backup. A directory name for oldest backup is
    31  // deduced from a given limit. For instance, for limit being 5 the
    32  // name for the oldest backup would be "4". If a backups number
    33  // exceeds the given limit then only newest ones are kept and the rest
    34  // is removed.
    35  func createBackup(dbDir, backupsDir string, limit int) error {
    36  	tmpBackupDir := filepath.Join(backupsDir, "tmp")
    37  	if err := os.MkdirAll(backupsDir, defaultPathPerm); err != nil {
    38  		return err
    39  	}
    40  	if err := fileutil.CopyTree(dbDir, tmpBackupDir, uid.NewBlankUidRange()); err != nil {
    41  		return err
    42  	}
    43  	defer os.RemoveAll(tmpBackupDir)
    44  	// prune backups
    45  	if err := pruneOldBackups(backupsDir, limit-1); err != nil {
    46  		return err
    47  	}
    48  	if err := shiftBackups(backupsDir, limit-2); err != nil {
    49  		return err
    50  	}
    51  	if err := os.Rename(tmpBackupDir, filepath.Join(backupsDir, "0")); err != nil {
    52  		return err
    53  	}
    54  	return nil
    55  }
    56  
    57  // pruneOldBackups removes old backups, that is - directories with
    58  // names greater or equal than given limit.
    59  func pruneOldBackups(dir string, limit int) error {
    60  	if list, err := ioutil.ReadDir(dir); err != nil {
    61  		return err
    62  	} else {
    63  		for _, fi := range list {
    64  			if num, err := strconv.Atoi(fi.Name()); err != nil {
    65  				// directory name is not a number,
    66  				// leave it alone
    67  				continue
    68  			} else if num < limit {
    69  				// directory name is a number lower
    70  				// than a limit, leave it alone
    71  				continue
    72  			}
    73  			path := filepath.Join(dir, fi.Name())
    74  			if err := os.RemoveAll(path); err != nil {
    75  				return err
    76  			}
    77  		}
    78  	}
    79  	return nil
    80  }
    81  
    82  // shiftBackups renames all directories with names being numbers up to
    83  // oldest to names with numbers greater by one.
    84  func shiftBackups(dir string, oldest int) error {
    85  	if oldest < 0 {
    86  		return nil
    87  	}
    88  	for i := oldest; i >= 0; i-- {
    89  		current := filepath.Join(dir, strconv.Itoa(i))
    90  		inc := filepath.Join(dir, strconv.Itoa(i+1))
    91  		if err := os.Rename(current, inc); err != nil && !os.IsNotExist(err) {
    92  			return err
    93  		}
    94  	}
    95  	return nil
    96  }