First version
This commit is contained in:
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
9
.idea/archiver.iml
generated
Normal file
9
.idea/archiver.iml
generated
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="WEB_MODULE" version="4">
|
||||||
|
<component name="Go" enabled="true" />
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/archiver.iml" filepath="$PROJECT_DIR$/.idea/archiver.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
3
README.md
Normal file
3
README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# archiver
|
||||||
|
|
||||||
|
This script allows to copy a folder to make a backup, I compress it in a .tar.gz file and I send it on a remote server with rsync
|
||||||
90
main.go
Normal file
90
main.go
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
path := flag.String("path", "", "Path to the folder to archive")
|
||||||
|
backupFolder := flag.String("backup", "./archive", "Path to the backup folder")
|
||||||
|
count := flag.Int("count", 3, "Number of backup to keep in the archive")
|
||||||
|
flag.Parse()
|
||||||
|
if len(*path) == 0 {
|
||||||
|
fmt.Println("missing --path argument")
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
if *count < 1 {
|
||||||
|
fmt.Println("--count must be greater than 0")
|
||||||
|
os.Exit(3)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(*path); err != nil {
|
||||||
|
err = os.MkdirAll(*path, 0750)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(4)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
absSourcePath, err := filepath.Abs(*path)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(5)
|
||||||
|
}
|
||||||
|
absDstPath, err := filepath.Abs(*backupFolder)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(5)
|
||||||
|
}
|
||||||
|
if err := makeBackup(absSourcePath, absDstPath, *count); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(7)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// makeBackup make a backup
|
||||||
|
func makeBackup(source, dst string, count int) error {
|
||||||
|
err := cleanOldBackup(dst, count-1)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
name := time.Now().Format("2006_01_02__15_04_05")
|
||||||
|
output, err := exec.Command("cp", "-r", source, filepath.Join(dst, name)).CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(string(output))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// cleanOldBackup clean up old archives so as not to exceed the quota
|
||||||
|
func cleanOldBackup(backupFolder string, count int) error {
|
||||||
|
files, err := ioutil.ReadDir(backupFolder)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for len(files) > count {
|
||||||
|
oldestTime := time.Now()
|
||||||
|
var oldestFile os.FileInfo
|
||||||
|
for _, file := range files {
|
||||||
|
|
||||||
|
if file.ModTime().Before(oldestTime) {
|
||||||
|
oldestFile = file
|
||||||
|
oldestTime = file.ModTime()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = os.RemoveAll(filepath.Join(backupFolder, oldestFile.Name()))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
files, err = ioutil.ReadDir(backupFolder)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user