Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

50
LINES

< > BotCompany Repo | #1022822 // thinArbitraryDatedBackups - keeps all hourly backups in last 2 days, one per day in last 31 days, one per week after

JavaX fragment (include)

1  
// file names need to include the pattern "-YYYYMMDD-HH"
2  
static void thinArbitraryDatedBackups(File dir, bool doIt) {
3  
  new L<File> files;
4  
  new Map<File, Double> ageMap;
5  
  
6  
  Pattern pat = regexp("-(20\\d\\d)(\\d\\d)(\\d\\d)-(\\d\\d)");
7  
  for (File f : listFilesNotDirs(dir)) {
8  
    S s = f.getName();
9  
    Matcher matcher = pat.matcher(s);
10  
    continue unless matcher.find();
11  
    //print("Found backup: " + sfu(matcherGroups(matcher)));
12  
    int year = matcherInt(matcher, 1);
13  
    int month = matcherInt(matcher, 2);
14  
    int day = matcherInt(matcher, 3);
15  
    int hour = matcherInt(matcher, 4);
16  
    long time = timestampFromYMDH(year, month, day, hour);
17  
    double age = ((now()-time)/1000.0/60/60/24;
18  
    //print("Age: " + age + " days");
19  
    ageMap.put(f, age);
20  
    files.add(f);
21  
  }
22  
  
23  
  int numDeleted = 0;
24  
  sortByMap_inPlace(files, ageMap);
25  
  double lastAge = -1;
26  
  for (File f : files) {
27  
    double age = ageMap.get(f);
28  
    if (!thinArbitraryDatedBackups_shouldKeep(age, lastAge)) {
29  
      //print("Deleting: " + f);
30  
      ++numDeleted;
31  
      if (doIt) {
32  
        print("Deleting: " + f);
33  
        f.delete();
34  
      }
35  
    } else {
36  
      print("Keeping: " + f);
37  
      lastAge = age;
38  
    }
39  
  }
40  
  if (numDeleted != 0)
41  
    print((doIt ? "Deleted: " : "Would delete: ") + n(numDeleted, "file"));
42  
}
43  
44  
// age = age in days
45  
sbool thinArbitraryDatedBackups_shouldKeep(double age, double lastAge) {
46  
  if (age <= 2) true;
47  
  if (age <= 31 && age >= lastAge+1) true;
48  
  if (age >= lastAge+7) true;
49  
  false;
50  
}

Author comment

Began life as a copy of #1011269

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1022822
Snippet name: thinArbitraryDatedBackups - keeps all hourly backups in last 2 days, one per day in last 31 days, one per week after
Eternal ID of this version: #1022822/1
Text MD5: 27905c9385e6a22b6f825efbb11c9cb1
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-04-09 11:41:50
Source code size: 1544 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 210 / 248
Referenced in: [show references]