Firefox Sessionstore backup program

  1. /*
  2.  *  I love Firefox,  I love Tabs.  But sometimes these two don't
  3.  *  get along.  I've lost my tabs plenty of times, which is very
  4.  *  annoying since I have usually 100+ tabs open.
  5.  *  
  6.  *  After loosing all my tabs today, and then learning about
  7.  *  the Sessionstore.js and loosing the latest copy as I was trying
  8.  *  to restore my tabs I felt the need to backup that file.
  9.  *  
  10.  * So,  here is a simple program you can put into Task Scheduler
  11.  * that will backup the sessionstore.js file of every Firefox profile
  12.  * into a directory called SessionStoreBackups which is located in each
  13.  * each profile dir.
  14.  *
  15.  * Hopefully I won't loose my tabs again or at least it won't be a travesty :)
  16.  *
  17.  */
  18.  
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Text;
  22. using System.IO;
  23.  
  24. namespace FirefoxSessionBackup
  25. {
  26.     class FFBack
  27.     {
  28.         static void Main(string[] args)
  29.         {
  30.             try
  31.             {
  32.                 // Format our date into YYYYMMDD
  33.                 string year = DateTime.Now.Year.ToString();
  34.                 string month = DateTime.Now.Month.ToString();
  35.                 if (month.Length == 1) { month = "0" + month; }
  36.                 string day = DateTime.Now.Day.ToString();
  37.                 if (day.Length == 1) { day = "0" + day; }
  38.                 string date = year + month + day;
  39.  
  40.                 // Get the AppData path
  41.                 string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  42.  
  43.                 // Get the root path of the Mozilla Profiles. I Assume the path is always "Mozilla\Firefox\Profiles".
  44.                 string MozProfiles = AppData + Path.DirectorySeparatorChar + "Mozilla" + Path.DirectorySeparatorChar + "Firefox" + Path.DirectorySeparatorChar + "Profiles";
  45.  
  46.                 // Get all the profiles
  47.                 DirectoryInfo MozProfileDI = new DirectoryInfo(MozProfiles);
  48.                 DirectoryInfo[] MozProfilesCollections = MozProfileDI.GetDirectories();
  49.  
  50.                 // Go through all the profiles and back up the Sessionstore for each profile.
  51.                 foreach (DirectoryInfo Profile in MozProfilesCollections)
  52.                 {
  53.                     // Construct misc. paths we need
  54.                     string ProfileName = Profile.Name;
  55.                     string ProfileRoot = MozProfiles + Path.DirectorySeparatorChar + ProfileName;
  56.                     string BackupFolderRoot = ProfileRoot + Path.DirectorySeparatorChar + "SessionStoreBackups";
  57.                     string BackupFolder = BackupFolderRoot + Path.DirectorySeparatorChar + date;
  58.  
  59.                     // Create our backup root if it doesn't exist.
  60.                     if (!Directory.Exists(BackupFolderRoot))
  61.                     {
  62.                         Directory.CreateDirectory(BackupFolderRoot);
  63.                     }
  64.                     // Create our backupFolder if it doesn't exist
  65.                     if (!Directory.Exists(BackupFolder))
  66.                     {
  67.                         Directory.CreateDirectory(BackupFolder);
  68.                     }
  69.                     // Copy the file,  if it doesn't exist already
  70.                     if(!File.Exists(BackupFolder + Path.DirectorySeparatorChar + "sessionstore.js"))
  71.                     {
  72.                         File.Copy(ProfileRoot + Path.DirectorySeparatorChar + "sessionstore.js", BackupFolder + Path.DirectorySeparatorChar + "sessionstore.js");
  73.                     }
  74.                 }
  75.             }
  76.             catch(Exception e)
  77.             {
  78.                 // Error handling here
  79.             }
  80.         }
  81.     }
  82. }