Jump to content

Featured Replies

Posted
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() +.

  • 10 months later...
  • 11 months later...
  • 4 months later...
  • 5 weeks later...
  • 3 months later...
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 by SpiritOfTheLau

  • 1 month later...
  • 1 year later...
  • 2 weeks later...
  • 1 month later...
  • 4 weeks later...
  • 1 month later...

Create an account or sign in to comment