Last active
July 10, 2018 11:46
-
-
Save geo-stanciu/fd4d6484ff95e14c46f59ff3a7dc37c3 to your computer and use it in GitHub Desktop.
Keep x backup files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <config> | |
| <general> | |
| <export-folder></export-folder> | |
| <files2keep>2</files2keep> | |
| </general> | |
| </config> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Copyright (c) 2018, Gheorghita Stanciu gheorghita(dot)stanciu(at)gmail(dot)com | |
| All rights reserved. | |
| Redistribution and use in source and binary forms, with or without | |
| modification, are permitted provided that the following conditions are met: | |
| 1. Redistributions of source code must retain the above copyright notice, this | |
| list of conditions and the following disclaimer. | |
| 2. Redistributions in binary form must reproduce the above copyright notice, | |
| this list of conditions and the following disclaimer in the documentation | |
| and/or other materials provided with the distribution. | |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
| ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
| ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| The views and conclusions contained in the software and documentation are those | |
| of the authors and should not be interpreted as representing official policies, | |
| either expressed or implied, of the FreeBSD Project. | |
| */ | |
| package main | |
| import ( | |
| "encoding/xml" | |
| "fmt" | |
| "io" | |
| "log" | |
| "os" | |
| "path/filepath" | |
| "sort" | |
| "time" | |
| ) | |
| type configuration struct { | |
| XMLName xml.Name `xml:"config"` | |
| General configurationGeneral | |
| } | |
| type configurationGeneral struct { | |
| XMLName xml.Name `xml:"general"` | |
| ExportFolder string `xml:"export-folder"` | |
| Files2Keep int `xml:"files2keep"` | |
| } | |
| var ( | |
| config = configuration{} | |
| ) | |
| func main() { | |
| t := time.Now() | |
| sData := t.Format("20060102") | |
| logFile, err := os.OpenFile(fmt.Sprintf("logs/cleanbackups_%s.txt", sData), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) | |
| if err != nil { | |
| log.Println(err) | |
| return | |
| } | |
| defer logFile.Close() | |
| mw := io.MultiWriter(os.Stdout, logFile) | |
| log.SetOutput(mw) | |
| cfgFile := "./app.config" | |
| err = config.readFromFile(cfgFile) | |
| if err != nil { | |
| log.Println(err) | |
| return | |
| } | |
| directory := getAbsPath(config.General.ExportFolder) | |
| log.Printf("Cleaning old files from \"%s\"\n", directory) | |
| log.Printf("Will keep the last %d files.", config.General.Files2Keep) | |
| err = cleanDir(directory, "exp_svm_*.zip") | |
| if err != nil { | |
| log.Fatal(err) | |
| return | |
| } | |
| err = cleanDir(directory, "exp_svm_*.dmp") | |
| if err != nil { | |
| log.Fatal(err) | |
| return | |
| } | |
| err = cleanDir(directory, "exp_officenet_*.zip") | |
| if err != nil { | |
| log.Fatal(err) | |
| return | |
| } | |
| err = cleanDir(directory, "exp_officenet_*.dmp") | |
| if err != nil { | |
| log.Fatal(err) | |
| return | |
| } | |
| log.Println("Process finished.") | |
| } | |
| func getAbsPath(dir string) string { | |
| directory := dir | |
| if len(directory) == 0 { | |
| directory = "./" | |
| } | |
| directory, err := filepath.Abs(directory) | |
| if err != nil { | |
| log.Fatal(err) | |
| return "./" | |
| } | |
| if directory[len(directory)-1:] == "/" || directory[len(directory)-1:] == "\\" { | |
| directory = directory[0 : len(directory)-1] | |
| } | |
| return directory | |
| } | |
| func cleanDir(directory, pattern string) error { | |
| files, err := filepath.Glob(directory + "/" + pattern) | |
| if err != nil { | |
| return err | |
| } | |
| layout := "02.01.2006" | |
| sort.Slice(files, func(i, j int) bool { | |
| a := files[i] | |
| b := files[j] | |
| if len(a) >= 14 && len(b) >= 14 { | |
| sda := a[len(a)-14 : len(a)-4] | |
| sdb := b[len(b)-14 : len(b)-4] | |
| da, _ := time.Parse(layout, sda) | |
| db, _ := time.Parse(layout, sdb) | |
| return da.After(db) | |
| } | |
| return files[i] < files[j] | |
| }) | |
| for i, f := range files { | |
| if i > config.General.Files2Keep-1 { | |
| log.Printf("deleting \"%s\"...\n", f) | |
| err = os.Remove(f) | |
| if err != nil { | |
| return err | |
| } | |
| } | |
| } | |
| return nil | |
| } | |
| func (c *configuration) readFromFile(cfgFile string) error { | |
| if _, err := os.Stat(cfgFile); os.IsNotExist(err) { | |
| return err | |
| } | |
| file, err := os.Open(cfgFile) | |
| if err != nil { | |
| return err | |
| } | |
| defer file.Close() | |
| decoder := xml.NewDecoder(file) | |
| for { | |
| t, _ := decoder.Token() | |
| if t == nil { | |
| break | |
| } | |
| switch se := t.(type) { | |
| case xml.StartElement: | |
| if se.Name.Local == "config" { | |
| decoder.DecodeElement(c, &se) | |
| if err != nil { | |
| return err | |
| } | |
| } | |
| } | |
| } | |
| return nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment