Sanity 435 Posted September 28, 2017 Report Share Posted September 28, 2017 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() +. 2 Link to comment Share on other sites More sharing options...
Versatile 0 Posted August 20, 2018 Report Share Posted August 20, 2018 What do you mean by modifying getDate() ? How exactly could I get the system to back up every, let's say, 2 hours? Link to comment Share on other sites More sharing options...
superfly 0 Posted August 12, 2019 Report Share Posted August 12, 2019 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 Link to comment Share on other sites More sharing options...
Leakin 699 Posted December 16, 2019 Report Share Posted December 16, 2019 Thanks for the share. Link to comment Share on other sites More sharing options...
Baller pk10 0 Posted January 15, 2020 Report Share Posted January 15, 2020 thank you for the contribution Link to comment Share on other sites More sharing options...
SpiritOfTheLau 0 Posted May 9, 2020 Report Share Posted May 9, 2020 (edited) 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, 2020 by SpiritOfTheLau Link to comment Share on other sites More sharing options...
Cammighty 0 Posted July 3, 2020 Report Share Posted July 3, 2020 Backups are very important, thanks for code brother I will use it in my studies Link to comment Share on other sites More sharing options...
TestySauce 1 Posted December 2, 2021 Report Share Posted December 2, 2021 interesting thankssssssssss Link to comment Share on other sites More sharing options...
Machinee 0 Posted December 17, 2021 Report Share Posted December 17, 2021 Thanks Link to comment Share on other sites More sharing options...
shadowisdom 1 Posted February 6, 2022 Report Share Posted February 6, 2022 thanks for this Link to comment Share on other sites More sharing options...
Rubyy 5 Posted March 4, 2022 Report Share Posted March 4, 2022 awesome just what i needed Link to comment Share on other sites More sharing options...
Snooperal 0 Posted April 17, 2022 Report Share Posted April 17, 2022 ty for contribution Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now