mirror of
https://github.com/fabianonline/telegram_backup.git
synced 2024-11-22 16:56:16 +00:00
Added a method to create database backups before doing huge database update stuff.
This commit is contained in:
parent
db52b8da47
commit
4e227e55c8
@ -30,6 +30,7 @@ public class Config {
|
||||
public static final String FILE_NAME_AUTH_KEY = "auth.dat";
|
||||
public static final String FILE_NAME_DC = "dc.dat";
|
||||
public static final String FILE_NAME_DB = "database.sqlite";
|
||||
public static final String FILE_NAME_DB_BACKUP = "database.version_%d.backup.sqlite";
|
||||
public static final String FILE_FILES_BASE = "files";
|
||||
public static final String FILE_STICKER_BASE = "stickers";
|
||||
|
||||
|
@ -36,6 +36,8 @@ import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.HashMap;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager;
|
||||
import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory;
|
||||
@ -151,6 +153,7 @@ public class Database {
|
||||
version = 5;
|
||||
}
|
||||
if (version==5) {
|
||||
backupDatabase(5);
|
||||
System.out.println(" Updating to version 6...");
|
||||
stmt.executeUpdate(
|
||||
"CREATE TABLE messages_new (\n" +
|
||||
@ -239,6 +242,20 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
private void backupDatabase(int currentVersion) {
|
||||
String filename = String.format(Config.FILE_NAME_DB_BACKUP, currentVersion);
|
||||
System.out.println(" Creating a backup of your database as " + filename);
|
||||
try {
|
||||
Files.copy(
|
||||
new File(user_manager.getFileBase() + Config.FILE_NAME_DB).toPath(),
|
||||
new File(user_manager.getFileBase() + filename).toPath(),
|
||||
StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Could not create backup.");
|
||||
}
|
||||
}
|
||||
|
||||
public int getTopMessageID() {
|
||||
try {
|
||||
ResultSet rs = stmt.executeQuery("SELECT MAX(id) FROM messages");
|
||||
|
Loading…
Reference in New Issue
Block a user