tmux_backup/lib.rs
1#![warn(missing_docs)]
2
3//! A backup & restore solution for Tmux sessions.
4//!
5//! Version requirement: _rustc 1.74+_
6//!
7//! ## Features
8//!
9//! - Backup and restore of your tmux environment:
10//! - tmux sessions windows, panes, with layout, titles & pane history
11//! - current and last session.
12//! - Fast: less than 1 sec for 16 sessions, 45 windows and 80 panes.
13//! - Show the catalog of backups, with age, file size, content description & archive format
14//! - 2 strategies are available:
15//! - keep the `n` most recent backups
16//! - classic backup strategy:
17//! - the lastest backup per hour for the past 24 hours (max 23 backups - exclude the past hour),
18//! - the lastest backup per day for the past 7 days (max 6 backups - exclude the past 24 hours),
19//! - the lastest backup per week of the past 4 weeks (max 3 backups - exclude the past week),
20//! - the lastest backup per month of this year (max 11 backups - exclude the past month).
21//! - Because you decide where backups are stored, you can use both strategies, combining the
22//! benefits of high-frequency backups and on demand backups like in tmux-resurrect.
23//!
24//! ## Getting started
25//!
26//! After installation (see below), you can either use it from the command line, or via tmux
27//! bindings.
28//!
29//! The catalog is located by default in `$XDG_STATE_HOME/tmux-backup/`, or
30//! `§HOME/.state/tmux-backup` otherwise. The default strategy is "most-recent", but you can change
31//! it with `--strategy classic`. Check usage with `tmux-backup --help` for detailed help.
32//!
33//! ### View the catalog of existing backups
34//!
35//! ```console
36//! $ tmux-backup catalog list --details
37//! Strategy: KeepMostRecent: 10
38//! Location: `$HOME/.local/state/tmux-backup`
39//!
40//! NAME AGE STATUS FILESIZE VERSION CONTENT
41//! 11. backup-20220907T224553.156103.tar.zst 2 days purgeable 644.17 kB 1.0 16 sessions 43 windows 79 panes
42//! 10. backup-20220907T224926.103771.tar.zst 2 days retainable 644.38 kB 1.0 16 sessions 43 windows 79 panes
43//! 9. backup-20220908T092341.125258.tar.zst 2 days retainable 654.76 kB 1.0 16 sessions 43 windows 79 panes
44//! 8. backup-20220909T224742.781818.tar.zst 18 hours retainable 599.64 kB 1.0 16 sessions 42 windows 77 panes
45//! 7. backup-20220909T225158.305403.tar.zst 18 hours retainable 600.32 kB 1.0 16 sessions 42 windows 79 panes
46//! 6. backup-20220910T152551.807672.tar.zst 1 hour retainable 608.79 kB 1.0 16 sessions 43 windows 80 panes
47//! 5. backup-20220910T165118.250800.tar.zst 29 minutes retainable 614.16 kB 1.0 16 sessions 43 windows 80 panes
48//! 4. backup-20220910T171812.893389.tar.zst 2 minutes retainable 614.33 kB 1.0 16 sessions 43 windows 80 panes
49//! 3. backup-20220910T172016.924711.tar.zst 11 seconds retainable 614.44 kB 1.0 16 sessions 43 windows 80 panes
50//! 2. backup-20220910T172019.320809.tar.zst 8 seconds retainable 614.42 kB 1.0 16 sessions 43 windows 80 panes
51//! 1. backup-20220910T172024.141993.tar.zst 3 seconds retainable 614.38 kB 1.0 16 sessions 43 windows 80 panes
52//!
53//! 11 backups: 10 retainable, 1 purgeable
54//! ```
55//!
56//! If you installed the plugin config into tmux, then the default tmux bindings for listing
57//! backups are
58//!
59//! - `prefix + b + l` to show the simple catalog
60//! - `prefix + b + L` to show the detailed catalog (adds the filesize, version & content columns)
61//!
62//! Both of these bindings will open a tmux popup showing the catalog content.
63//!
64//! ### Save the current tmux environment
65//!
66//! ```console
67//! $ tmux-backup save
68//! ✅ 16 sessions 43 windows 80 panes, persisted to `/Users/graelo/.local/state/tmux-backup/backup-20220910T171812.893389.tar.zst`
69//! ```
70//!
71//! By default, the tmux binding for saving a new backup are
72//!
73//! - `prefix + b + s` save and compact (delete purgeable backups)
74//! - `prefix + b + b` save but not compact the catalog
75//!
76//! Both of these bindings will print the same report as above in the tmux status bar.
77//!
78//! ### Restore from a backup
79//!
80//! Typing `tmux-backup restore` in your shell outside of tmux will
81//!
82//! - start a tmux server if none is running
83//! - restore all sessions from the latest backup
84//! - but you still have to `tmux attach -t <your-last-session>`
85//!
86//! The same command typed in a shell inside tmux will erase session `0` (the default start
87//! session) and restore your tmux environment in place.
88//!
89//! By default, the tmux binding for restoring the latest backup is
90//!
91//! - `prefix + b + r` restore sessions from the latest backup
92//!
93//! ## Installation
94//!
95//! ### Installing the binary
96//!
97//! On macOS
98//!
99//! ```shell
100//! brew install graelo/homebrew-tap/tmux-backup # will also install shell completions
101//! ```
102//!
103//! On linux
104//!
105//! ```shell
106//! curl \
107//! https://github.com/graelo/tmux-backup/releases/download/v0.4.0/tmux-backup-x86_64-unknown-linux-gnu.tar.xz \
108//! | tar xf - > /usr/local/bin/tmux-backup
109//! chmod +x /usr/local/bin/tmux-backup
110//! ```
111//!
112//! On linux, to install completions, type
113//!
114//! ```shell
115//! tmux-backup generate-completion zsh|bash|fish > /path/to/your/completions/folder
116//! ```
117//!
118//! ### Installing the tmux plugin hook
119//!
120//! Type
121//!
122//! ```shell
123//! mkdir ~/.tmux/plugins/tmux-backup
124//! tmux-backup init > ~/.tmux/plugins/tmux-backup/tmux-backup.tmux
125//! ```
126//!
127//! If you don't use [tpm](https://github.com/tmux-plugins/tpm), just add this to your
128//! `.tmux.conf`:
129//!
130//! ```text
131//! source-file ~/.tmux/plugins/tmux-backup/tmux-backup.tmux
132//! ```
133//!
134//! Alternatively, if you use tpm, declare the tmux-backup plugin to TPM in your `~/.tmux.conf`:
135//!
136//! ```tmux
137//! set -g @tpm_plugins ' \
138//! tmux-plugins/tpm \
139//! tmux-plugins/tmux-backup \ <-- here
140//! tmux-plugins/tmux-copyrat \
141//! tmux-plugins/tmux-yank \
142//! tmux-plugins/tmux-resurrect \
143//! tmux-plugins/tmux-sessionist \
144//! '
145//! ```
146//!
147//! The next time you start tmux, the `tmux-backup.tmux` configuration will be loaded.
148//!
149//! ## Caveats
150//!
151//! - This is a beta version
152//! - Does not handle multiple clients: help is welcome if you have clear scenarios for this.
153//! - Does not handle session groups: help is also welcome.
154//!
155//! ## License
156//!
157//! Licensed under either of
158//!
159//! - [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
160//! - [MIT license](http://opensource.org/licenses/MIT)
161//!
162//! at your option.
163//!
164//! ### Contribution
165//!
166//! Unless you explicitly state otherwise, any contribution intentionally submitted
167//! for inclusion in the work by you, as defined in the Apache-2.0 license, shall
168//! be dual licensed as above, without any additional terms or conditions.
169
170pub mod actions;
171pub mod config;
172pub mod error;
173pub mod management;
174pub use tmux_lib as tmux;
175
176/// Result type for this crate.
177pub type Result<T> = std::result::Result<T, error::Error>;