Skip to content

Commit

Permalink
Merge from dev, add function to delete old file from backup dir
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandrojnm committed Jan 3, 2019
2 parents d78c24c + 05d4b50 commit 1df18a2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 33 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package core

import (
"fmt"
"io/ioutil"
_ "log"
"os"
"path/filepath"
"time"
)

func FindFile(targetDir string, pattern []string) ([]string, error) {
Expand All @@ -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)
}

}
}
}
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 1df18a2

Please sign in to comment.