Skip to content

Commit ecbc9bc

Browse files
authored
Merge pull request #27 from buildplan/background_restore
CHANGELOG: Restic Backup Script v0.37.2 → v0.38 ## NEW FEATURES 1. BACKGROUND RESTORE MODE (--background-restore) ---------------------------------------------- - NEW: Non-interactive restore that runs as a background process - Usage: sudo restic-backup.sh --background-restore <snapshot_id> <dest_path> - Features: * Accepts 'latest' as snapshot ID (auto-resolves to most recent snapshot) * Creates dedicated timestamped log files for each restore job * Runs in detached background process with output redirection * Sends notifications on completion/failure via configured channels * Automatically handles file ownership for /home/* paths * No terminal interaction required after launch - Implementation Details: * Uses subshell backgrounding: ( ... ) > "$restore_log" 2>&1 & * Log files: /tmp/restic-restore-${snapshot_id:0:8}-$(date +%s).log * Integrates with notification system (ntfy, Discord, Slack, Teams) * Runs pre-flight checks before starting background job - Use Cases: * Large dataset restoration without blocking terminal * Remote server restores over SSH * Long-running operations that should survive terminal disconnection 2. SYNC RESTORE MODE (--sync-restore) ------------------------------------ - NEW: Non-interactive foreground restore for automation/cron - Usage: sudo restic-backup.sh --sync-restore <snapshot_id> <dest_path> [paths...] - Features: * Runs synchronously (blocks until completion) * Returns proper exit codes for scheduler/cron monitoring * Accepts optional specific file/directory paths to restore * Integrates with Healthchecks.io for success/failure pinging * Supports 'latest' snapshot ID resolution * Automatic ownership handling for user directories - Implementation Details: * Exit code 0 on success, 1 on failure * Healthchecks.io ping on completion: $HEALTHCHECKS_URL or $HEALTHCHECKS_URL/fail * Full logging to main log file * Notification support for all configured channels - Use Cases: * Automated backup pull for 3-2-1 backup strategy * Cron-scheduled regular restores to secondary servers * DR (Disaster Recovery) automation workflows * CI/CD backup restoration pipelines
2 parents 1518712 + df66534 commit ecbc9bc

File tree

3 files changed

+264
-23
lines changed

3 files changed

+264
-23
lines changed

README.md

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,47 @@ This script automates encrypted, deduplicated backups of local directories to a
2424

2525
-----
2626

27+
## Quick Start
28+
29+
For those familiar with setting up backup scripts, here is a fast track to get you up and running.
30+
31+
1. **Download Files:**
32+
33+
```sh
34+
mkdir -p /root/scripts/backup && cd /root/scripts/backup
35+
curl -LO https://raw.githubusercontent.com/buildplan/restic-backup-script/refs/heads/main/restic-backup.sh
36+
curl -LO https://raw.githubusercontent.com/buildplan/restic-backup-script/refs/heads/main/restic-backup.conf
37+
curl -LO https://raw.githubusercontent.com/buildplan/restic-backup-script/refs/heads/main/restic-excludes.txt
38+
chmod +x restic-backup.sh
39+
```
40+
41+
2. **Edit Configuration:**
42+
- Modify `restic-backup.conf` with your repository details, source paths, and password file location.
43+
- Set secure permissions: `chmod 600 restic-backup.conf`.
44+
45+
3. **Create Password & Initialize:**
46+
47+
```sh
48+
# Create the password file (use a strong password)
49+
echo 'your-very-secure-password' | sudo tee /root/.restic-password
50+
sudo chmod 400 /root/.restic-password
51+
52+
# Initialize the remote repository
53+
sudo ./restic-backup.sh --init
54+
```
55+
56+
4. **Run First Backup & Schedule:**
57+
58+
```sh
59+
# Run your first backup with verbose output
60+
sudo ./restic-backup.sh --verbose
61+
62+
# Set up a recurring schedule with the interactive wizard
63+
sudo ./restic-backup.sh --install-scheduler
64+
```
65+
66+
-----
67+
2768
## Usage
2869

2970
### Run Modes
@@ -38,6 +79,8 @@ This script automates encrypted, deduplicated backups of local directories to a
3879
- `sudo ./restic-backup.sh --install-scheduler` - Run the interactive wizard to set up an automated backup schedule (systemd/cron).
3980
- `sudo ./restic-backup.sh --uninstall-scheduler` - Remove a schedule created by the wizard.
4081
- `sudo ./restic-backup.sh --restore` - Start the interactive restore wizard.
82+
- `sudo ./restic-backup.sh --background-restore <snapshot> <dest>` - Restore in the background (non-blocking).
83+
- `sudo ./restic-backup.sh --sync-restore <snapshot> <dest>` - Restore in a cronjob (helpful for 3-2-1 backup strategy).
4184
- `sudo ./restic-backup.sh --forget` - Manually apply the retention policy and prune old data.
4285
- `sudo ./restic-backup.sh --diff` - Show a summary of changes between the last two snapshots.
4386
- `sudo ./restic-backup.sh --stats` - Display repository size, file counts, and stats.
@@ -49,6 +92,75 @@ This script automates encrypted, deduplicated backups of local directories to a
4992

5093
> *Default log location: `/var/log/restic-backup.log`*
5194

95+
-----
96+
97+
### Restoring Data
98+
99+
Script provides three distinct modes for restoring data, each designed for a different scenario.
100+
101+
#### 1. Interactive Restore (`--restore`)
102+
103+
This is an interactive wizard for guided restores. It is the best option when you are at the terminal and need to find and recover specific files or directories.
104+
105+
- **Best for**: Visually finding and restoring specific files or small directories.
106+
- **Process**:
107+
- Lists available snapshots for you to choose from.
108+
- Asks for a destination path.
109+
- Performs a "dry run" to show you what will be restored before making any changes.
110+
- Requires your confirmation before proceeding with the actual restore.
111+
112+
**Usage:**
113+
114+
```sh
115+
sudo ./restic-backup.sh --restore
116+
```
117+
118+
#### 2. Background Restore (`--background-restore`)
119+
120+
This mode is designed for restoring large amounts of data (e.g., a full server recovery) without needing to keep your terminal session active.
121+
122+
- **Best for**: Large, time-consuming restores or recovering data over a slow network connection.
123+
- **How it works**:
124+
- This command is **non-interactive**. You must provide the snapshot ID and destination path as arguments directly on the command line.
125+
- The restore job is launched in the background, immediately freeing up terminal.
126+
- All output is saved to a log file in `/tmp/`.
127+
- A success or failure notification (via ntfy, Discord, etc.) upon completion.
128+
129+
**Usage:**
130+
131+
```sh
132+
# Restore the latest snapshot to a specific directory in the background
133+
sudo ./restic-backup.sh --background-restore latest /mnt/disaster-recovery
134+
135+
# Restore a specific snapshot by its ID
136+
sudo ./restic-backup.sh --background-restore a1b2c3d4 /mnt/disaster-recovery
137+
```
138+
139+
#### 3. Synchronous Restore (`--sync-restore`)
140+
141+
This mode runs the restore in the foreground and waits for it to complete before exiting. It's a reliable, non-interactive way to create a complete, consistent copy of backup data.
142+
143+
- **Best for**: Creating a secondary copy of backup (for example, via a cron job) on another server (for a 3-2-1 strategy) or for use in any automation where subsequent steps depend on the restore being finished.
144+
- **How it works**:
145+
- This command is **non-interactive** and requires the snapshot ID and destination path as command-line arguments.
146+
- It runs as a synchronous (blocking) process. When a cron job executes the command, the job itself will not finish until the restore is 100% complete.
147+
- This guarantees the data copy is finished before any other commands are run or the cron job is marked as complete.
148+
149+
**Usage:**
150+
151+
```sh
152+
# On a second server, pull a full copy of the latest backup
153+
sudo ./restic-backup.sh --sync-restore latest /mnt/local-backup-copy
154+
155+
# On your secondary server, run a sync-restore every day at 5:00 AM.
156+
0 5 * * * /path/to/your/script/restic-backup.sh --sync-restore latest /path/to/local/restore/copy >> /var/log/restic-restore.log 2>&1
157+
158+
# Can also be used in a script to ensure a process runs only after a restore
159+
sudo ./restic-backup.sh --sync-restore latest /srv/app/data && systemctl restart my-app
160+
```
161+
162+
-----
163+
52164
#### Diagnostics & Error Codes
53165
54166
The script uses specific exit codes for different failures to help with debugging automated runs.
@@ -115,8 +227,11 @@ uname -m
115227
116228
```sh
117229
# Download the latest binary for your architecture from the Restic GitHub page
118-
# Example 0.18.0 is latest as of Aug,2025 for amd64:
119-
curl -LO https://github.com/restic/restic/releases/download/v0.18.0/restic_0.18.0_linux_amd64.bz2
230+
# Go to the Restic GitHub releases page to find the URL for the latest version:
231+
# https://github.com/restic/restic/releases
232+
233+
# Download the latest binary for your architecture (replace URL with the one you found)
234+
curl -LO <URL_of_latest_restic_linux_amd64.bz2>
120235
```
121236
122237
```sh
@@ -126,6 +241,8 @@ chmod +x restic_*
126241
sudo mv restic_* /usr/local/bin/restic
127242
```
128243
244+
-----
245+
129246
#### Package Breakdown
130247
131248
| Package | Required For |
@@ -183,6 +300,8 @@ The most reliable way for the script to connect to a remote server is via an SSH
183300
sudo ssh storagebox pwd
184301
```
185302
303+
-----
304+
186305
### 3. Place and Configure Files
187306
188307
1. Create your script directory:
@@ -287,6 +406,8 @@ Before the first backup, you need to create the repository password file and ini
287406
sudo ./restic-backup.sh --init
288407
```
289408

409+
-----
410+
290411
### 5. Set up an Automated Schedule (Recommended)
291412

292413
The easiest and most reliable way to schedule your backups is to use the script's built-in interactive wizard. It will guide you through creating and enabling either a modern `systemd timer` (recommended) or a traditional `cron job`.
@@ -334,6 +455,6 @@ To run the backup automatically, edit the root crontab.
334455
335456
```
336457
337-
*For pune job in your `restic-backup.conf`, set `PRUNE_AFTER_FORGET=true`.*
458+
*For prune job in your `restic-backup.conf`, set `PRUNE_AFTER_FORGET=true`.*
338459
*For more details on how forget flag work, see the [official Restic documentation on removing snapshots](https://restic.readthedocs.io/en/stable/060_forget.html).*
339460
*Redirecting output to `/dev/null` is recommended, as the script handles its own logging and notifications.*

0 commit comments

Comments
 (0)