wdcli: Implement backup & snapshot
This commit implements command backup and snapshot.
This commit is contained in:
parent
d818cb93a5
commit
fc3b9170a6
11 changed files with 535 additions and 178 deletions
|
|
@ -11,11 +11,11 @@ import (
|
|||
|
||||
// Package packages the directory src into dst.
|
||||
// onCopy, when not nil, is called for each file being copied into the archive.
|
||||
func Package(dst, src string, onCopy func(src string)) error {
|
||||
func Package(dst, src string, onCopy func(rel string, src string)) (count int64, err error) {
|
||||
// create the target archive
|
||||
archive, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
defer archive.Close()
|
||||
|
||||
|
|
@ -28,21 +28,28 @@ func Package(dst, src string, onCopy func(src string)) error {
|
|||
defer tarHandle.Close()
|
||||
|
||||
// and walk through it!
|
||||
return filepath.Walk(src, func(path string, info fs.FileInfo, err error) error {
|
||||
if onCopy != nil {
|
||||
onCopy(path)
|
||||
}
|
||||
|
||||
err = filepath.Walk(src, func(path string, info fs.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// determine the relative path
|
||||
var relpath string
|
||||
relpath, err = filepath.Rel(src, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if onCopy != nil {
|
||||
onCopy(relpath, path)
|
||||
}
|
||||
|
||||
// create a file info header!
|
||||
tInfo, err := tar.FileInfoHeader(info, path)
|
||||
tInfo, err := tar.FileInfoHeader(info, relpath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tInfo.Name = filepath.ToSlash(path)
|
||||
tInfo.Name = filepath.ToSlash(relpath)
|
||||
|
||||
// write it!
|
||||
if err := tarHandle.WriteHeader(tInfo); err != nil {
|
||||
|
|
@ -62,8 +69,9 @@ func Package(dst, src string, onCopy func(src string)) error {
|
|||
defer handle.Close()
|
||||
|
||||
// and copy it into the archive
|
||||
_, err = io.Copy(tarHandle, handle)
|
||||
ccount, err := io.Copy(tarHandle, handle)
|
||||
count += ccount
|
||||
return err
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue