// Backup make backup from src to dest.
488967
func (c *SQLiteConn) Backup(dest string, conn *SQLiteConn, src string) (*SQLiteBackup, error) {
	destptr := C.CString(dest)
	defer C.free(unsafe.Pointer(destptr))
	srcptr := C.CString(src)
	defer C.free(unsafe.Pointer(srcptr))

	if b := C.sqlite3_backup_init(c.db, destptr, conn.db, srcptr); b != nil {
		bb := &SQLiteBackup{b: b}
		runtime.SetFinalizer(bb, (*SQLiteBackup).Finish)
		return bb, nil
	}
	return nil, c.lastError()
}
533804
func (p CassandraPlugin) Backup(endpoint plugin.ShieldEndpoint) error {
	cassandra, err := cassandraInfo(endpoint)
	if err != nil {
		return err
	}

	plugin.DEBUG("Cleaning any stale '%s' snapshot", SnapshotName)
	cmd := fmt.Sprintf("nodetool clearsnapshot -t %s \"%s\"", SnapshotName, cassandra.Keyspace)
	plugin.DEBUG("Executing: `%s`", cmd)
	err = plugin.Exec(cmd, plugin.STDIN)
	if err != nil {
		fmt.Fprintf(os.Stderr, "@R{\u2717 Clean any stale snapshot}\n")
		return err
	}
	fmt.Fprintf(os.Stderr, "@G{\u2713 Clean any stale snapshot}\n")

	defer func() {
		plugin.DEBUG("Clearing snapshot '%s'", SnapshotName)
		cmd := fmt.Sprintf("nodetool clearsnapshot -t %s \"%s\"", SnapshotName, cassandra.Keyspace)
		plugin.DEBUG("Executing: `%s`", cmd)
		err := plugin.Exec(cmd, plugin.STDIN)
		if err != nil {
			fmt.Fprintf(os.Stderr, "@R{\u2717 Clean snapshot}\n")
			return
		}
		fmt.Fprintf(os.Stderr, "@G{\u2713 Clean snapshot}\n")
	}()

	plugin.DEBUG("Creating a new '%s' snapshot", SnapshotName)
	cmd = fmt.Sprintf("nodetool snapshot -t %s \"%s\"", SnapshotName, cassandra.Keyspace)
	plugin.DEBUG("Executing: `%s`", cmd)
	err = plugin.Exec(cmd, plugin.STDIN)
	if err != nil {
		fmt.Fprintf(os.Stderr, "@R{\u2717 Create new snapshot}\n")
		return err
	}
	fmt.Fprintf(os.Stderr, "@G{\u2713 Create new snapshot}\n")

	// Here we need to hard-link the snapshots/shield-backup directories into
	// a {keyspace}/{tablename} structure that we'll temporarily put in
	// /var/vcap/store/shield/cassandra. Then we can tar it all and stream
	// that to stdout.

	baseDir := "/var/vcap/store/shield/cassandra"
	plugin.DEBUG("Creating any missing directories for '%s', with 0755 permissions", baseDir)
	err = os.MkdirAll(baseDir, 0755)
	if err != nil {
		fmt.Fprintf(os.Stderr, "@R{\u2717 Create base temporary directory}\n")
		return err
	}
	fmt.Fprintf(os.Stderr, "@G{\u2713 Create base temporary directory}\n")

	// Recursively remove /var/vcap/store/shield/cassandra/{keyspace}, if any
	tmpKeyspaceDir := filepath.Join(baseDir, cassandra.Keyspace)
	plugin.DEBUG("Removing any stale '%s' directory", tmpKeyspaceDir)
	cmd = fmt.Sprintf("rm -rf \"%s\"", tmpKeyspaceDir)
	plugin.DEBUG("Executing `%s`", cmd)
	err = plugin.Exec(cmd, plugin.STDOUT)
	if err != nil {
		fmt.Fprintf(os.Stderr, "@R{\u2717 Clear base temporary directory}\n")
		return err
	}
	fmt.Fprintf(os.Stderr, "@G{\u2713 Clear base temporary directory}\n")

	defer func() {
		// Recursively remove /var/vcap/store/shield/cassandra/{keyspace}, if any
		plugin.DEBUG("Cleaning the '%s' directory up", tmpKeyspaceDir)
		cmd := fmt.Sprintf("rm -rf \"%s\"", tmpKeyspaceDir)
		plugin.DEBUG("Executing `%s`", cmd)
		err := plugin.Exec(cmd, plugin.STDOUT)
		if err != nil {
			fmt.Fprintf(os.Stderr, "@R{\u2717 Clean base temporary directory}\n")
			return
		}
		fmt.Fprintf(os.Stderr, "@G{\u2713 Clean base temporary directory}\n")
	}()

	plugin.DEBUG("Creating directory '%s' with 0700 permissions", tmpKeyspaceDir)
	err = os.Mkdir(tmpKeyspaceDir, 0700)
	if err != nil {
		fmt.Fprintf(os.Stderr, "@R{\u2717 Create temporary directory}\n")
		return err
	}
	fmt.Fprintf(os.Stderr, "@G{\u2713 Create temporary directory}\n")

	// Iterate through {dataDir}/{keyspace}/{tablename}/snapshots/shield-backup/*
	// and for all the immutable files we find here, we hard-link them
	// to /var/vcap/store/shield/cassandra/{keyspace}/{tablename}
	//
	// We chose to hard-link because copying those immutable files is
	// unnecessary anyway. It could lead to performance issues and would
	// consume twice the disk space it should.

	info, err := os.Lstat(cassandra.DataDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "@R{\u2717 Recursive hard-link snapshot files}\n")
		return err
	}
	if !info.IsDir() {
		fmt.Fprintf(os.Stderr, "@R{\u2717 Recursive hard-link snapshot files}\n")
		return fmt.Errorf("cassandra DataDir is not a directory")
	}

	srcKeyspaceDir := filepath.Join(cassandra.DataDir, cassandra.Keyspace)
	dir, err := os.Open(srcKeyspaceDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "@R{\u2717 Recursive hard-link snapshot files}\n")
		return err
	}
	defer dir.Close()

	entries, err := dir.Readdir(-1)
	if err != nil {
		fmt.Fprintf(os.Stderr, "@R{\u2717 Recursive hard-link snapshot files}\n")
		return err
	}
	for _, tableDirInfo := range entries {
		if !tableDirInfo.IsDir() {
			continue
		}

		src_dir := filepath.Join(srcKeyspaceDir, tableDirInfo.Name(), "snapshots", SnapshotName)

		tableName := tableDirInfo.Name()
		if idx := strings.LastIndex(tableName, "-"); idx >= 0 {
			tableName = tableName[:idx]
		}

		dst_dir := filepath.Join(tmpKeyspaceDir, tableName)
		plugin.DEBUG("Creating destination directory '%s'", dst_dir)
		err = os.MkdirAll(dst_dir, 0755)
		if err != nil {
			fmt.Fprintf(os.Stderr, "@R{\u2717 Recursive hard-link snapshot files}\n")
			return err
		}

		plugin.DEBUG("Hard-linking all '%s/*' files to '%s/'", src_dir, dst_dir)
		err = hardLinkAll(src_dir, dst_dir)
		if err != nil {
			fmt.Fprintf(os.Stderr, "@R{\u2717 Recursive hard-link snapshot files in temp dir}\n")
			return err
		}
	}
	fmt.Fprintf(os.Stderr, "@G{\u2713 Recursive hard-link snapshot files in temp dir}\n")

	plugin.DEBUG("Setting ownership of all backup files to '%s'", VcapOwnership)
	cmd = fmt.Sprintf("chown -R vcap:vcap \"%s\"", tmpKeyspaceDir)
	plugin.DEBUG("Executing `%s`", cmd)
	err = plugin.Exec(cmd, plugin.STDOUT)
	if err != nil {
		fmt.Fprintf(os.Stderr, "@R{\u2717 Set ownership of snapshot hard-links}\n")
		return err
	}
	fmt.Fprintf(os.Stderr, "@G{\u2713 Set ownership of snapshot hard-links}\n")

	plugin.DEBUG("Streaming output tar file")
	cmd = fmt.Sprintf("tar -c -C /var/vcap/store/shield/cassandra -f - \"%s\"", cassandra.Keyspace)
	plugin.DEBUG("Executing `%s`", cmd)
	err = plugin.Exec(cmd, plugin.STDOUT)
	if err != nil {
		fmt.Fprintf(os.Stderr, "@R{\u2717 Stream tar of snapshots files}\n")
		return err
	}
	fmt.Fprintf(os.Stderr, "@G{\u2713 Stream tar of snapshots files}\n")

	return nil
}
937175
func (fbh *FileBackupHandle) EndBackup(ctx context.Context) error {
	if fbh.readOnly {
		return fmt.Errorf("EndBackup cannot be called on read-only backup")
	}
	return nil
}
174280
func (s *Scheme) Copy(src Object) (Object, error) {
	dst, err := s.DeepCopy(src)
	if err != nil {
		return nil, err
	}
	return dst.(Object), nil
}
1129583
func (e *CombinedEtcdClient) Backup(version *EtcdVersion, backupDir string) error {
	// We cannot use etcd/client (v2) to make this call. It is implemented in the etcdctl client code.
	if version.Major != 2 {
		return fmt.Errorf("etcd 2.x required but got version '%s'", version)
	}
	return e.runEtcdctlCommand(version,
		"--debug",
		"backup",
		"--data-dir", e.cfg.dataDirectory,
		"--backup-dir", backupDir,
	)
}
945115
func (l *lexer) backup() {
	l.pos -= l.width
	// Correct newline count.
	if l.width == 1 && l.input[l.pos] == '\n' {
		l.line--
	}
}
1067420
func CopyDir(src string, dst string) error {
	var err error
	var fds []os.FileInfo
	var info os.FileInfo
	if info, err = os.Stat(src); err != nil {
		return err
	}
	if err = os.MkdirAll(dst, info.Mode()); err != nil {
		return err
	}

	if fds, err = ioutil.ReadDir(src); err != nil {
		return err
	}
	for _, fd := range fds {
		sourcePath := path.Join(src, fd.Name())
		destinationPath := path.Join(dst, fd.Name())

		if fd.IsDir() {
			if err = CopyDir(sourcePath, destinationPath); err != nil {
				break
			}
		} else {
			if err = Copy(sourcePath, destinationPath); err != nil {
				break
			}
		}
	}
	return err
}
1302286
func (client *FakeTabletManagerClient) Backup(ctx context.Context, tablet *topodatapb.Tablet, concurrency int, allowMaster bool) (logutil.EventStream, error) {
	return &eofEventStream{}, nil
}
317607
func (p *Process) Backup() (io.ReadCloser, error) {
	r := &backupReadCloser{}

	cmd := exec.Command(
		filepath.Join(p.BinDir, "innobackupex"),
		"--defaults-file="+p.ConfigPath(),
		"--host=127.0.0.1",
		"--port="+p.Port,
		"--user=flynn",
		"--password="+p.Password,
		"--socket=",
		"--stream=xbstream",
		".",
	)
	cmd.Dir = p.DataDir
	cmd.Stderr = &r.stderr

	stdout, err := cmd.StdoutPipe()
	if err != nil {
		return nil, err
	}
	if err := cmd.Start(); err != nil {
		stdout.Close()
		return nil, err
	}

	// Attach to reader wrapper.
	r.cmd = cmd
	r.stdout = stdout

	return r, nil
}
137272
func (c *Client) RestoreBackup(id, backupID string) error {
	values := url.Values{
		"SUBID":    {id},
		"BACKUPID": {backupID},
	}

	if err := c.post(`server/restore_backup`, values, nil); err != nil {
		return err
	}
	return nil
}
82171
func Download(src string, dest string) error {
	res, err := http.Get(src)
	if err != nil {
		return err
	}
	if res.StatusCode != 200 {
		return errors.New(res.Status)
	}
	defer res.Body.Close()
	data, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return err
	}
	if err = ioutil.WriteFile(dest, data, 0644); err != nil {
		return err
	}
	return nil
}
233518
func Copy(ctx context.Context, src, dst string, fn SessionFunc) (err error) {
	// Check context
	if err = ctx.Err(); err != nil {
		return
	}

	// Stat src
	var statSrc os.FileInfo
	if statSrc, err = os.Stat(src); err != nil {
		err = errors.Wrapf(err, "stating %s failed", src)
		return
	}

	// Dir
	if statSrc.IsDir() {
		if err = filepath.Walk(src, func(path string, info os.FileInfo, errWalk error) (err error) {
			// Check error
			if errWalk != nil {
				err = errWalk
				return
			}

			// Do not process root
			if src == path {
				return
			}

			// Copy
			var p = filepath.Join(dst, strings.TrimPrefix(path, filepath.Clean(src)))
			if err = Copy(ctx, path, p, fn); err != nil {
				err = errors.Wrapf(err, "copying %s to %s failed", path, p)
				return

			}
			return
		}); err != nil {
			return
		}
		return
	}

	// Create ssh session
	var s *ssh.Session
	var c *astidefer.Closer
	if s, c, err = fn(); err != nil {
		err = errors.Wrap(err, "main: creating ssh session failed")
		return
	}
	defer c.Close()

	// Create the destination folder
	if err = s.Run("mkdir -p " + filepath.Dir(dst)); err != nil {
		err = errors.Wrapf(err, "astissh: creating %s failed", filepath.Dir(dst))
		return
	}

	// Open file
	var f *os.File
	if f, err = os.Open(src); err != nil {
		err = errors.Wrapf(err, "astissh: opening %s failed", src)
		return
	}
	defer f.Close()

	// Create ssh session
	if s, c, err = fn(); err != nil {
		err = errors.Wrap(err, "main: creating ssh session failed")
		return
	}
	defer c.Close()

	// Create stdin pipe
	var stdin io.WriteCloser
	if stdin, err = s.StdinPipe(); err != nil {
		err = errors.Wrap(err, "astissh: creating stdin pipe failed")
		return
	}
	defer stdin.Close()

	// Start "scp" command
	astilog.Debugf("astissh: copying %s to %s", filepath.Base(dst), filepath.Dir(dst))
	if err = s.Start("scp -qt " + filepath.Dir(dst)); err != nil {
		err = errors.Wrapf(err, "astissh: scp to %s failed", dst)
		return
	}

	// Send metadata
	if _, err = fmt.Fprintln(stdin, fmt.Sprintf("C%04o", statSrc.Mode().Perm()), statSrc.Size(), filepath.Base(dst)); err != nil {
		err = errors.Wrapf(err, "astissh: sending metadata failed")
		return
	}

	// Copy
	if _, err = astiio.Copy(ctx, f, stdin); err != nil {
		err = errors.Wrap(err, "astissh: copying failed")
		return
	}

	// Send close
	if _, err = fmt.Fprint(stdin, "\x00"); err != nil {
		err = errors.Wrap(err, "astissh: sending close failed")
		return
	}

	// Close stdin
	stdin.Close()

	// Wait
	if err = s.Wait(); err != nil {
		err = errors.Wrap(err, "astissh: waiting failed")
		return
	}
	return
}
1341658
func (s *Store) backupDB() error {
	if os.Geteuid() != 0 {
		return ErrDBUpdateNeedsRoot
	}
	backupsDir := filepath.Join(s.dir, "db-backups")
	return backup.CreateBackup(s.dbDir(), backupsDir, backupsNumber)
}
771553
func (bh *S3BackupHandle) EndBackup(ctx context.Context) error {
	if bh.readOnly {
		return fmt.Errorf("EndBackup cannot be called on read-only backup")
	}
	bh.waitGroup.Wait()
	return bh.errors.Error()
}
1265068
func (l *lexer) backup() {
	if l.prev.byteNo == lexEOF {
		panic("backup called with no valid previous rune")
	}
	l.pos = l.prev
	l.prev = position{byteNo: lexEOF}
}
1160908
func (l *lexer) backup() {
	l.pos -= l.width
	l.col, l.line = l.lastCol, l.lastLine
}
1171390
func (c *Client) copy(ctx context.Context, version string) error {
	backupDir := path.Join(path.Dir(c.Path), "backup")
	if _, err := os.Stat(backupDir); os.IsNotExist(err) {
		if err = os.Mkdir(backupDir, 0700); err != nil {
			return err
		}
	} else if err != nil {
		return err
	}

	fromFile, err := os.Open(c.Path)
	if err != nil {
		return err
	}
	defer fromFile.Close()

	toName := fmt.Sprintf("%s.%s", path.Base(c.Path), version)
	toPath := path.Join(backupDir, toName)
	toFile, err := os.OpenFile(toPath, os.O_RDWR|os.O_CREATE, 0600)
	if err != nil {
		return err
	}
	defer toFile.Close()

	_, err = io.Copy(toFile, fromFile)
	if err != nil {
		return err
	}

	c.logger.Info("Successfully created ", toPath)

	return nil
}