diff --git a/README.md b/README.md index 2d57acc..417b7d3 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ other way is download binary for your system, in the release page Copy binary for `/usr/local/bin/` then create cron -`00 2 * * * root simple-backup --endpoint=localhost:9000 --ak=accesskey --sk=secretkey --secureendpoint=(true|false) --bucket=system-backup --bucketlocation=us-east-1 --backudir=/srv/backup/mysql` +`00 2 * * * root simple-backup --endpoint=localhost:9000 --ak=accesskey --sk=secretkey --secureendpoint=(true|false) --bucket=system-backup --bucketlocation=us-east-1 --backudir=/srv/backup/mysql/ --daytodelete=7` You can run ``simple-backup -h`` to see help, simple-backup delete all backup from storage server older than 7 days diff --git a/core/core.go b/core/core.go index 06d9ccf..1948a99 100644 --- a/core/core.go +++ b/core/core.go @@ -2,8 +2,11 @@ package core import ( "fmt" + "io/ioutil" _ "log" + "os" "path/filepath" + "time" ) func FindFile(targetDir string, pattern []string) ([]string, error) { @@ -18,3 +21,33 @@ func FindFile(targetDir string, pattern []string) ([]string, error) { return matches, err } + +func RemoveOldFile(dir string, days float64) { + /* + Function to remove all file in dir older than *days + */ + + //Read dir to search old files + files, _ := ioutil.ReadDir(dir) + + for _, f := range files { + fi, err := os.Stat(dir + f.Name()) + if err != nil { + fmt.Println(err) + } + + // Calculate the difference between now and ModTime + now := time.Now() + currTime := fi.ModTime() + diff := now.Sub(currTime) + + //if the file ModTime is larger than days*24 then delete file + if days*24 < diff.Hours() { + err := os.RemoveAll(dir + f.Name()) + if err != nil { + fmt.Println(err) + } + + } + } +} diff --git a/main.go b/main.go index d933f42..d4c9eb2 100644 --- a/main.go +++ b/main.go @@ -31,9 +31,19 @@ func main() { bucket := flag.String("bucket", "alamesa-db", "Buket Name") bucketLocation := flag.String("bucketlocation", "us-east-1", "Bucket Zone") backupDir := flag.String("backupdir", "backup", "Backup directory location") + dayToDelete := flag.Float64("daytodelete", 7, "Days to delete old files from (backup dir)") flag.Parse() // + /* + I could create a subroutine to do this but then, + if there are many files to be deleted, + things that should be deleted would be uploaded to the backup, + so it is not put as a subroutine, it is expected that everything will be + erased and then the backup will be made + */ + core.RemoveOldFile(*backupDir, *dayToDelete) + // Create a new client for the storage server s3Client, err := minio.New(*endPoint, *ak, *sk, *secureEndPoint) if err != nil { @@ -60,7 +70,7 @@ func main() { // for all file in listFile we send to storage server for _, file := range listFile { fileName := filepath.Base(file) - //log.Printf(file_name) + // We save the file inside a folder with a name that is today's date n, err := s3Client.FPutObject(*bucket, jodaTime.Format("YYYYMMdd", time.Now())+"/"+fileName, file, minio.PutObjectOptions{ContentType: ""}) if err != nil { log.Fatalln(err)