Posted September 28, 20177 yr comment_13274 I believe this was released by someone else for PI, I've had it forever so credits to whoever released it, didn't take long to convert. Make this class wherever you want. package com.arlania.engine; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import com.arlania.util.Stopwatch; public class CharacterBackup { private static final int TIME = 28800000; //8 hours public static Stopwatch timer = new Stopwatch().reset(); public CharacterBackup() { File f1 = new File("./data/saves/characters/"); File f2 = new File("./data/saves/Backup " + getDate() + ".zip"); if (!f2.exists()) { try { System.out .println("[Auto-Backup] The mainsave has been automatically backed up."); zipDirectory(f1, f2); } catch (Exception e) { e.printStackTrace(); } } else { System.out .println("[Auto-Backup] The mainsave has already been backed up today. Backup aborted."); } } public static void sequence() { if(timer.elapsed(TIME)) { timer.reset(); { File f1 = new File("./data/saves/characters/"); File f2 = new File("./data/saves/Backup " + getDate() + ".zip"); if (!f2.exists()) { try { System.out .println("[BACKUP] Characters successfully backed up."); zipDirectory(f1, f2); } catch (Exception e) { e.printStackTrace(); } } else { System.out .println("[BACKUP] Characters already backed up, backup canceled.."); } } } } public static final void zipDirectory(File f, File zf) throws IOException { ZipOutputStream z = new ZipOutputStream(new FileOutputStream(zf)); zip(f, f, z); z.close(); } private static final void zip(File directory, File base, ZipOutputStream zos) throws IOException { File[] files = directory.listFiles(); byte[] buffer = new byte[8192]; int read = 0; for (int i = 0, n = files.length; i < n; i++) { if (files[i].isDirectory()) { zip(files[i], base, zos); } else { FileInputStream in = new FileInputStream(files[i]); ZipEntry entry = new ZipEntry(files[i].getPath().substring( base.getPath().length() + 1)); zos.putNextEntry(entry); while (-1 != (read = in.read(buffer))) { zos.write(buffer, 0, read); } in.close(); } } } public static String getDate() { DateFormat dateFormat = new SimpleDateFormat("MM dd yyyy"); Date date = new Date(); String currentDate = dateFormat.format(date); date = null; dateFormat = null; return currentDate; } } In the World Class add this: CharacterBackup.sequence(); I made a command also if you ever need it. if(command[0].equals("backup")) { CharacterBackup.timer.reset(0); player.getPacketSender().sendConsoleMessage("Backup Method Called."); } Enjoy. If you want it to back up more frequently (less than 24 hours) modify + getDate() +.
August 20, 20186 yr comment_21577 What do you mean by modifying getDate() ? How exactly could I get the system to back up every, let's say, 2 hours?
August 12, 20195 yr comment_35023 On 8/20/2018 at 6:43 AM, Versatile said: What do you mean by modifying getDate() ? How exactly could I get the system to back up every, let's say, 2 hours? what he said
May 9, 20205 yr comment_54864 This is pretty slick. Good job! For those asking about the backup frequency, if you'd like it to be something like every 2 hours, make the following changes. Replace the following variable: private static final int TIME = 28800000; //8 hours With this: private static final int TIME =7200000//2 hours Next, replace the getDate function: public static String getDate() { DateFormat dateFormat = new SimpleDateFormat("MM dd yyyy"); Date date = new Date(); String currentDate = dateFormat.format(date); date = null; dateFormat = null; return currentDate; } With this: public static String getDate() { DateFormat dateFormat = new SimpleDateFormat("MM dd yyyy HH mm"); Date date = new Date(); String currentDate = dateFormat.format(date); date = null; dateFormat = null; return currentDate; } What does this do? Changing the numeric value decreases the number of miliseconds between backing up from 28800000 (8 hours) to 7200000 (2 hours) which is calculated in the sequence function. By changing the arguments for the SimpleDateformat to include "HH mm" we specify an hour in our backup. This is important because more than one back up a day would overwrite or fail to write entirely based on permissions. Edited May 9, 20205 yr by SpiritOfTheLau
July 3, 20205 yr comment_59731 Backups are very important, thanks for code brother I will use it in my studies
Create an account or sign in to comment