I love Firefox, I love Tabs. But sometimes these two don’t get along. I’ve lost my tabs plenty of times, which is very annoying since I have usually 100+ tabs open. After loosing all my tabs today, and then learning about the Sessionstore.js and loosing the latest copy as I was trying to restore my tabs I felt the need to backup that file. So, here is a simple program you can put into Task Scheduler that will backup the sessionstore.js file of every Firefox profile into a directory called SessionStoreBackups which is located in each each profile dir.

Hopefully I won’t loose my tabs again or at least it won’t be a travesty :)

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace FirefoxSessionBackup
{
  class FFBack
  {
    static void Main(string[] args)
    {
      try
      {
        // Format our date into YYYYMMDD
        string year = DateTime.Now.Year.ToString();
        string month = DateTime.Now.Month.ToString();

        if (month.Length == 1) { month = "0" + month; }
        string day = DateTime.Now.Day.ToString();
        if (day.Length == 1) { day = "0" + day; }
        string date = year + month + day;

        // Get the AppData path
        string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

        // Get the root path of the Mozilla Profiles. I Assume the path is always "Mozilla\Firefox\Profiles".
        string MozProfiles = AppData + Path.DirectorySeparatorChar + "Mozilla" + Path.DirectorySeparatorChar + "Firefox" + Path.DirectorySeparatorChar + "Profiles";

        // Get all the profiles
        DirectoryInfo MozProfileDI = new DirectoryInfo(MozProfiles);
        DirectoryInfo[] MozProfilesCollections = MozProfileDI.GetDirectories();

        // Go through all the profiles and back up the Sessionstore for each profile.
        foreach (DirectoryInfo Profile in MozProfilesCollections)
        {  
          // Construct misc. paths we need
          string ProfileName = Profile.Name;
          string ProfileRoot = MozProfiles + Path.DirectorySeparatorChar + ProfileName;
          string BackupFolderRoot = ProfileRoot + Path.DirectorySeparatorChar + "SessionStoreBackups";
          string BackupFolder = BackupFolderRoot + Path.DirectorySeparatorChar + date;

          // Create our backup root if it doesn't exist.
          if (!Directory.Exists(BackupFolderRoot))
          {
            Directory.CreateDirectory(BackupFolderRoot);
          }

          // Create our backupFolder if it doesn't exist
          if (!Directory.Exists(BackupFolder))
          {
            Directory.CreateDirectory(BackupFolder);
          }

          // Copy the file, if it doesn't exist already
          if(!File.Exists(BackupFolder + Path.DirectorySeparatorChar + "sessionstore.js"))
          {
            File.Copy(ProfileRoot + Path.DirectorySeparatorChar + "sessionstore.js", BackupFolder + Path.DirectorySeparatorChar + "sessionstore.js");
          }
        }
      }
      catch(Exception e)
      {
        // Error handling here
      }
    }
  }
}
Tagged with: