Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.82 KB | None | 0 0
  1. using System;
  2. using System.Data.SqlClient;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Net.Mime;
  6. using System.Threading;
  7. using System.IO.Compression;
  8.  
  9. namespace ValheimBackupUtil
  10. {
  11.     internal class Program
  12.     {
  13.         private static int _interval;
  14.         private static string sourcePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\AppData\LocalLow\IronGate\Valheim\";
  15.         private static string backupPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\ValheimBackups\";
  16.         public static void Main(string[] args)
  17.         {
  18.             Console.WriteLine("This utility will only backup whenever you are currently playing.");
  19.             Console.WriteLine("Your data will be saved in your Documents folder ({0})", backupPath);
  20.             Console.WriteLine("How often would you like to backup? (in minutes)");
  21.             string input = Console.ReadLine();
  22.             if (int.TryParse(input, out var minutes)) //Check if input is an integer
  23.             {
  24.                 _interval = minutes;
  25.                 Console.WriteLine("How long would you like to keep backups? (in days)");
  26.                 input = Console.ReadLine();
  27.                
  28.                 if (int.TryParse(input, out var days)) //Check if input is an integer
  29.                 {
  30.                     if (days == 0)
  31.                     {
  32.                         Console.WriteLine("Setting deletion rate to 0 instantly deletes the backups silly...");
  33.                         Console.WriteLine("Defaulting to 2 day deletion");
  34.                         days = 2;
  35.                     }
  36.                     backup(minutes*60000, days); //*60,000 bc Sleep function is in milliseconds
  37.                                                                     //value of 1 = 1ms, value of 1*60,000 = 1 minute
  38.                 }
  39.                 else                          //input was not an integer.. cursed user
  40.                 {
  41.                     Console.WriteLine("{0} is not a number...", input);
  42.                     Console.WriteLine("Come back when you can think of one");
  43.                     Console.WriteLine("geez");
  44.                     Thread.Sleep(3000);
  45.                     Environment.Exit(1);
  46.                 }
  47.             }
  48.             else                             //input was not an integer.. cursed user
  49.             {
  50.                 Console.WriteLine("{0} is not a number...", input);
  51.                 Console.WriteLine("Come back when you can think of one");
  52.                 Console.WriteLine("geez");
  53.                 Thread.Sleep(3000);
  54.                 Environment.Exit(1);
  55.             }
  56.  
  57.  
  58.         }
  59.  
  60.         static void backup(int interval, int deleteCycle)
  61.         {
  62.             Console.Clear();
  63.             Console.WriteLine("Backup interval: {0} minute(s)", _interval);
  64.             Console.WriteLine("Backup deletion: {0} day(s)", deleteCycle);
  65.             if (!Directory.Exists(backupPath))                  //if backup directory doesnt exist yet, make one real quick
  66.                 Directory.CreateDirectory(backupPath);
  67.  
  68.             while (true)
  69.             {
  70.                 if (Process.GetProcessesByName("valheim").Length > 0)                                                 //checks if valheim is running
  71.                 {
  72.                     string backupName = backupPath + "backup_"+DateTime.Now.ToString("MM.dd.yyyy_HH.mm");     //"backupName" is the path to the current datetime backup folder
  73.                     Directory.CreateDirectory(backupName);                                                           //create said folder, it will not exist. time goes forward, but surely the user will break that too
  74.                     Console.WriteLine("[<(*beep boop backup machine initialized*)>]");
  75.                     CloneDirectory(sourcePath, backupName);                                              //the actual backup function
  76.                     if(!Directory.Exists(backupName+".zip"))
  77.                         ZipFile.CreateFromDirectory(backupName, backupName + ".zip");       //create zip from previous created backup
  78.                    
  79.                     Directory.Delete(backupName, true);                                                   //delete backup folder, as we are preserving the zip
  80.                    
  81.                     foreach (string file in Directory.GetFiles(backupPath))                                       //for each backup in backups folder
  82.                     {
  83.                         FileInfo fi = new FileInfo(file);                                                         //get the file info
  84.                         if (fi.CreationTime < DateTime.Now.AddDays(-deleteCycle))                                 //if file is older than specified days
  85.                         {
  86.                             fi.Delete();                                                                          //delete that shit
  87.                             Console.WriteLine("[<(*deleted old backup beep beep*)>]");
  88.                         }
  89.                     }
  90.                    
  91.                     Console.WriteLine("[<(*{0} complete boop beep*)>]", backupName+".zip");
  92.                     Console.WriteLine("[<(*taking a nap for {0} minute(s)*)>]", _interval);
  93.                 }
  94.                 else
  95.                 {
  96.                     Console.WriteLine("Valheim is not currently running, checking again in {0} minute(s)", _interval);
  97.                 }
  98.                 Thread.Sleep(interval);
  99.             }
  100.         }
  101.  
  102.         static void CloneDirectory(string root, string dest)
  103.         {
  104.             if (Directory.Exists(root))                                                //make sure the valheim directory actually exists, dunno why it wouldnt though
  105.             {
  106.                 foreach (var directory in Directory.GetDirectories(root))       //for each folder in valheim directory
  107.                 {
  108.                     string dirName = Path.GetFileName(directory);                     //identify the folders name
  109.                     if (!Directory.Exists(Path.Combine(dest, dirName)))              
  110.                     {
  111.                         Directory.CreateDirectory(Path.Combine(dest, dirName));       //and create the same folder in the backup dir
  112.                     }
  113.  
  114.                     CloneDirectory(directory, Path.Combine(dest, dirName)); //do this again, but checking inside of folders
  115.                 }
  116.  
  117.                 foreach (var file in Directory.GetFiles(root))                   //for each individual file in valheim dir
  118.                 {
  119.                     File.Copy(file, Path.Combine(dest, Path.GetFileName(file))); //copy to backup dir
  120.                 }
  121.             }
  122.             else
  123.             {
  124.                 Console.WriteLine("oh fuck... valheim sourcepath is null or some shit brooooo");
  125.             }
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement