Added automatic cleanup of backups folder

By default mcMMO keeps the following files:
 * All files from the last 24 hours
 * Daily backups of the past week
 * Weekly backups of the past months

Adds #1574
This commit is contained in:
TfT_02 2013-11-01 16:07:56 +01:00
parent 6d5f3a52f4
commit 408b8b2107
5 changed files with 137 additions and 3 deletions

View File

@ -11,6 +11,7 @@ Version 1.4.08-dev
+ Added the possibility to gain experience when using Fishing "Shake"
+ Added config options to disable various sound effects
+ Smelting now works with custom ores - add smelting XP value to blocks.yml, or it will default to 1/10th of normal XP.
+ Added automatic cleanup of backups folder.
= Fixed bug with toggle commands not properly displaying the success message.
= Fixed IllegalArgumentException caused by an empty Fishing treasure category
= Fixed bug with Salvage not reading the config value for the anvil material.

View File

@ -206,7 +206,6 @@ public class Config extends AutoUpdateConfigLoader {
public boolean getUpdateCheckEnabled() { return config.getBoolean("General.Update_Check", true); }
public boolean getPreferBeta() { return config.getBoolean("General.Prefer_Beta", false); }
public boolean getEventCallbackEnabled() { return config.getBoolean("General.Event_Callback", true); }
public boolean getBackupsEnabled() { return config.getBoolean("General.Generate_Backups", true); }
public boolean getVerboseLoggingEnabled() { return config.getBoolean("General.Verbose_Logging", false); }
public boolean getConfigOverwriteEnabled() { return config.getBoolean("General.Config_Update_Overwrite", true); }
@ -268,6 +267,12 @@ public class Config extends AutoUpdateConfigLoader {
public int getPurgeInterval() { return config.getInt("Database_Purging.Purge_Interval", -1); }
public int getOldUsersCutoff() { return config.getInt("Database_Purging.Old_User_Cutoff", 6); }
/* Backups */
public boolean getBackupsEnabled() { return config.getBoolean("Backups.Enabled", true); }
public boolean getKeepLast24Hours() { return config.getBoolean("Backups.Keep.Last_24_Hours", true); }
public boolean getKeepDailyLastWeek() { return config.getBoolean("Backups.Keep.Daily_Last_Week", true); }
public boolean getKeepWeeklyPastMonth() { return config.getBoolean("Backups.Keep.Weekly_Past_Months", true); }
/* mySQL */
public boolean getUseMySQL() { return config.getBoolean("MySQL.Enabled", false); }
public String getMySQLTablePrefix() { return config.getString("MySQL.Database.TablePrefix", "mcmmo_"); }

View File

@ -32,6 +32,7 @@ import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.metrics.MetricsManager;
import com.gmail.nossr50.party.PartyManager;
import com.gmail.nossr50.runnables.SaveTimerTask;
import com.gmail.nossr50.runnables.backups.CleanBackupsTask;
import com.gmail.nossr50.runnables.database.UserPurgeTask;
import com.gmail.nossr50.runnables.party.PartyAutoKickTask;
import com.gmail.nossr50.runnables.player.PowerLevelUpdatingTask;
@ -222,6 +223,8 @@ public class mcMMO extends JavaPlugin {
getLogger().severe(e.toString());
}
}
new CleanBackupsTask().runTaskAsynchronously(mcMMO.p);
}
debug("Was disabled."); // How informative!

View File

@ -0,0 +1,115 @@
package com.gmail.nossr50.runnables.backups;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.bukkit.scheduler.BukkitRunnable;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.mcMMO;
public class CleanBackupsTask extends BukkitRunnable {
private static final String BACKUP_DIRECTORY = mcMMO.getMainDirectory() + "backup" + File.separator;
private static final File BACKUP_DIR = new File(BACKUP_DIRECTORY);
@Override
public void run() {
List<Integer> savedDays = new ArrayList<Integer>();
List<Integer> savedWeeks = new ArrayList<Integer>();
List<File> toDelete = new ArrayList<File>();
int amountTotal = 0;
int amountDeleted = 0;
// Check files in backup folder from oldest to newest
for (File file : BACKUP_DIR.listFiles()) {
if (!file.isFile()) {
continue;
}
amountTotal++;
String fileName = file.getName();
fileName = fileName.split("[.]")[0];
Date date = getDate(fileName);
if (date == null) {
mcMMO.p.debug("Could not determine date for file: " + fileName);
continue;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int weekOfYear = cal.get(Calendar.WEEK_OF_YEAR);
if (isPast24Hours(date) && Config.getInstance().getKeepLast24Hours()) {
// Keep all files from the last 24 hours
continue;
}
else if (isLastWeek(date) && !savedDays.contains(dayOfWeek) && Config.getInstance().getKeepDailyLastWeek()) {
// Keep daily backups of the past week
savedDays.add(dayOfWeek);
continue;
}
else if (!savedWeeks.contains(weekOfYear) && Config.getInstance().getKeepWeeklyPastMonth()) {
// Keep one backup of each week
savedWeeks.add(weekOfYear);
continue;
}
amountDeleted++;
toDelete.add(file);
}
mcMMO.p.getLogger().info("Cleaning backup files... (" + amountDeleted + "/" + amountTotal + ")");
for (File file : toDelete) {
mcMMO.p.debug("Deleted: " + file.getName());
file.delete();
}
}
/**
* Check if date is within last 24 hours
*
* @param date date to check
*
* @return true is date is within last 24 hours, false if otherwise
*/
private boolean isPast24Hours(Date date) {
Date modifiedDate = new Date(System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(24, TimeUnit.HOURS));
return date.after(modifiedDate);
}
/**
* Check if date is within the last week
*
* @param date date to check
*
* @return true is date is within the last week, false if otherwise
*/
private boolean isLastWeek(Date date) {
Date modifiedDate = new Date(System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(7, TimeUnit.DAYS));
return date.after(modifiedDate);
}
private Date getDate(String fileName) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date date;
try {
date = dateFormat.parse(fileName);
}
catch (ParseException e) {
return null;
}
return date;
}
}

View File

@ -19,8 +19,6 @@ General:
Prefer_Beta: false
# Allow mcMMO to inform other plugins of damage being dealt
Event_Callback: true
# Allow mcMMO to create zip backups for flatfile data on shutdown.
Generate_Backups: true
Power_Level_Cap: 0
# Should mcMMO print out debug messages?
Verbose_Logging: false
@ -101,6 +99,18 @@ Database_Purging:
# To never purge old users, set to -1
Old_User_Cutoff: 6
#
# Settings for Backups
###
Backups:
# Allow mcMMO to create zip backups for config files and flatfile data on shutdown.
Enabled: true
Keep:
Last_24_Hours: true
Daily_Last_Week: true
Weekly_Past_Months: true
#
# Settings for using a mySQL database
###