Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data.SqlClient;
- using System.Diagnostics;
- using System.IO;
- using System.Net.Mime;
- using System.Threading;
- using System.IO.Compression;
- namespace ValheimBackupUtil
- {
- internal class Program
- {
- private static int _interval;
- private static string sourcePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\AppData\LocalLow\IronGate\Valheim\";
- private static string backupPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\ValheimBackups\";
- public static void Main(string[] args)
- {
- Console.WriteLine("This utility will only backup whenever you are currently playing.");
- Console.WriteLine("Your data will be saved in your Documents folder ({0})", backupPath);
- Console.WriteLine("How often would you like to backup? (in minutes)");
- string input = Console.ReadLine();
- if (int.TryParse(input, out var minutes)) //Check if input is an integer
- {
- _interval = minutes;
- Console.WriteLine("How long would you like to keep backups? (in days)");
- input = Console.ReadLine();
- if (int.TryParse(input, out var days)) //Check if input is an integer
- {
- if (days == 0)
- {
- Console.WriteLine("Setting deletion rate to 0 instantly deletes the backups silly...");
- Console.WriteLine("Defaulting to 2 day deletion");
- days = 2;
- }
- backup(minutes*60000, days); //*60,000 bc Sleep function is in milliseconds
- //value of 1 = 1ms, value of 1*60,000 = 1 minute
- }
- else //input was not an integer.. cursed user
- {
- Console.WriteLine("{0} is not a number...", input);
- Console.WriteLine("Come back when you can think of one");
- Console.WriteLine("geez");
- Thread.Sleep(3000);
- Environment.Exit(1);
- }
- }
- else //input was not an integer.. cursed user
- {
- Console.WriteLine("{0} is not a number...", input);
- Console.WriteLine("Come back when you can think of one");
- Console.WriteLine("geez");
- Thread.Sleep(3000);
- Environment.Exit(1);
- }
- }
- static void backup(int interval, int deleteCycle)
- {
- Console.Clear();
- Console.WriteLine("Backup interval: {0} minute(s)", _interval);
- Console.WriteLine("Backup deletion: {0} day(s)", deleteCycle);
- if (!Directory.Exists(backupPath)) //if backup directory doesnt exist yet, make one real quick
- Directory.CreateDirectory(backupPath);
- while (true)
- {
- if (Process.GetProcessesByName("valheim").Length > 0) //checks if valheim is running
- {
- string backupName = backupPath + "backup_"+DateTime.Now.ToString("MM.dd.yyyy_HH.mm"); //"backupName" is the path to the current datetime backup folder
- Directory.CreateDirectory(backupName); //create said folder, it will not exist. time goes forward, but surely the user will break that too
- Console.WriteLine("[<(*beep boop backup machine initialized*)>]");
- CloneDirectory(sourcePath, backupName); //the actual backup function
- if(!Directory.Exists(backupName+".zip"))
- ZipFile.CreateFromDirectory(backupName, backupName + ".zip"); //create zip from previous created backup
- Directory.Delete(backupName, true); //delete backup folder, as we are preserving the zip
- foreach (string file in Directory.GetFiles(backupPath)) //for each backup in backups folder
- {
- FileInfo fi = new FileInfo(file); //get the file info
- if (fi.CreationTime < DateTime.Now.AddDays(-deleteCycle)) //if file is older than specified days
- {
- fi.Delete(); //delete that shit
- Console.WriteLine("[<(*deleted old backup beep beep*)>]");
- }
- }
- Console.WriteLine("[<(*{0} complete boop beep*)>]", backupName+".zip");
- Console.WriteLine("[<(*taking a nap for {0} minute(s)*)>]", _interval);
- }
- else
- {
- Console.WriteLine("Valheim is not currently running, checking again in {0} minute(s)", _interval);
- }
- Thread.Sleep(interval);
- }
- }
- static void CloneDirectory(string root, string dest)
- {
- if (Directory.Exists(root)) //make sure the valheim directory actually exists, dunno why it wouldnt though
- {
- foreach (var directory in Directory.GetDirectories(root)) //for each folder in valheim directory
- {
- string dirName = Path.GetFileName(directory); //identify the folders name
- if (!Directory.Exists(Path.Combine(dest, dirName)))
- {
- Directory.CreateDirectory(Path.Combine(dest, dirName)); //and create the same folder in the backup dir
- }
- CloneDirectory(directory, Path.Combine(dest, dirName)); //do this again, but checking inside of folders
- }
- foreach (var file in Directory.GetFiles(root)) //for each individual file in valheim dir
- {
- File.Copy(file, Path.Combine(dest, Path.GetFileName(file))); //copy to backup dir
- }
- }
- else
- {
- Console.WriteLine("oh fuck... valheim sourcepath is null or some shit brooooo");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement