Joseph O'Shea

How to fix tmux resurrect when it fails to load your previous sessions

tmuxtmux-resurrect

The problem - somtimes tmux resurrect fails to load your previous tmux sessions

tmux-continuum is an awesome tool which works together with tmux-resurrecthttps://github.com/tmux-plugins/tmux-resurrect to continually back up your tmux sessions and restores them on startup. This is extremely useful if you use tmux to organize your CLI work like I do.

However, sometimes things break. One day I started tmux and to my suprise, all my sessions were gone.

I'm honestly not sure of the exact cause of this. I've seen some comments on github suggesting that it could be caused by a reboot happening at the same time that the plugin is saving.

Regardless of how it happens, the more important question is: how to fix it?

Fixing the problem

Step by step fix

This is the "just show me how to fix it" section. If you'd like to read all the details on how & why this works, please see the "How tmux-resurrect works under the hood" further down in this post.

  1. Find the file name for your most recent "good" backup file.

    To find the list of restore files, run:

    ls -la ~/.tmux/resurrect
    

    (NOTE: ~/.tmux/resurrect is the default, if you have changed this default use which directory you have set instead)

    You will see a list of files that looks something like this:
    (I've truncated the output to only include the relevant parts)

    (truncated)
    -rw-r--r--  1 your_username  staff    41K Feb 20 17:06 tmux_resurrect_2019-02-15T09:43:28.txt
    -rw-r--r--  1 your_username  staff   1.3K Feb 20 17:06 tmux_resurrect_2019-02-15T15:07:21.txt
    -rw-r--r--  1 your_username  staff   2.4K Feb 20 17:06 tmux_resurrect_2019-02-16T11:53:32.txt
    -rw-r--r--  1 your_username  staff   2.4K Feb 20 17:06 tmux_resurrect_2019-02-16T12:08:32.txt
    

    Notice how the file size suddenly drops significantly? This is how we can tell which backup was the last "good" file.
    In my case, my last good file was the file tmux_resurrect_2019-02-15T09:43:28.txt with the file size 41K

  2. Close any existing tmux sessions

    Detach from any tmux sessions you have open, and stop the tmux server by running:

    tmux kill-server
    
  3. Set the "last" save to link to your last "good" save

    Create the symlink last to point to your last good save. Replace tmux_resurrect_2019-02-15T09:43:28.txt with the name of your file found in step 1

    ln -sf ~/.tmux/resurrect/tmux_resurrect_2019-02-15T09:43:28.txt ~/.tmux/resurrect/last
    
  4. Start tmux again with your sessions restored correctly

    tmux
    

How tmux-resurrect works under the hood

The primary goal of tmux-resurrect is to restore your tmux sessions after a restart. In order to do this, tmux-resurrect must save a snapshot of your tmux session somewhere.

By default, tmux-resurrect does this by storing plaintext files in ~/.tmux/resurrect

That directory will look something like this (truncated for brevity):

lrwxr-xr-x  1 joey  staff    34B Aug  8 10:46 last -> tmux_resurrect_20220808T104613.txt                                                               
-rw-r--r--  1 joey  staff   3.0K Aug  8 09:45 tmux_resurrect_20220808T094555.txt
-rw-r--r--  1 joey  staff   2.9K Aug  8 10:01 tmux_resurrect_20220808T100105.txt
-rw-r--r--  1 joey  staff   2.9K Aug  8 10:16 tmux_resurrect_20220808T101613.txt
-rw-r--r--  1 joey  staff   3.0K Aug  8 10:46 tmux_resurrect_20220808T104613.txt

So we see tmux-resurrect is storing multiple timestamped snapshots of our tmux session.

It also maintains a symlink last which points to one of the files in that directory. When tmux-resurrect loads the file linked to by last.

Therefore, if last ends up pointing to the wrong file, tmux-resurrect loads the wrong data.

This means we can fix tmux-resurrect to load the correct data by changing last to point to the correct file.