Backup: Restic: Difference between revisions

From OnnoCenterWiki
Jump to navigationJump to search
Created page with "Running `restic` every 5 seconds to back up a MySQL database on a remote Ubuntu server is technically possible but **not recommended** due to: * Backup overhead (even increme..."
 
No edit summary
 
Line 1: Line 1:
Running `restic` every 5 seconds to back up a MySQL database on a remote Ubuntu server is technically possible but **not recommended** due to:
Running `restic` every 5 seconds to back up a MySQL database on a remote Ubuntu server is technically possible but '''not recommended''' due to:


* Backup overhead (even incremental backups every 5 seconds is resource-intensive).
* Backup overhead (even incremental backups every 5 seconds is resource-intensive).
Line 5: Line 5:
* `cron` does not support per-second precision; you'd need `systemd` timers for that.
* `cron` does not support per-second precision; you'd need `systemd` timers for that.


That said, here's a **safe and practical** version (e.g., every 1–5 minutes), and also a **technical example** for every 5 seconds using `systemd`.
That said, here's a '''safe and practical''' version (e.g., every 1–5 minutes), and also a '''technical example''' for every 5 seconds using `systemd`.


---
==Recommended Approach: Every 1 Minute Using systemd Timer==


## ✅ Recommended Approach: Every 1 Minute Using systemd Timer
'''Step 1: Create MySQL Dump Script'''


### Step 1: Create MySQL Dump Script
/usr/local/bin/mysql_backup_restic.sh


/usr/local/bin/mysql_backup_restic.sh
```bash
  #!/bin/bash
  #!/bin/bash
   
   
Line 34: Line 32:
   
   
  restic backup "$DUMP_FILE"
  restic backup "$DUMP_FILE"
```


Make it executable:
Make it executable:
```bash
chmod +x /usr/local/bin/mysql_backup_restic.sh
```


---
chmod +x /usr/local/bin/mysql_backup_restic.sh


### Step 2: Create systemd Service


`/etc/systemd/system/mysql-restic-backup.service`
'''Step 2: Create systemd Service'''
```ini
[Unit]
Description=Backup MySQL and upload to restic


[Service]
/etc/systemd/system/mysql-restic-backup.service
Type=oneshot
ExecStart=/usr/local/bin/mysql_backup_restic.sh
```


---
[Unit]
Description=Backup MySQL and upload to restic
[Service]
Type=oneshot
ExecStart=/usr/local/bin/mysql_backup_restic.sh


### Step 3: Create Timer (Every Minute)
'''Step 3: Create Timer (Every Minute)'''


`/etc/systemd/system/mysql-restic-backup.timer`
/etc/systemd/system/mysql-restic-backup.timer  
```ini
[Unit]
Description=Run mysql-restic-backup every minute


[Timer]
[Unit]
OnBootSec=1min
Description=Run mysql-restic-backup every minute
OnUnitActiveSec=1min
Unit=mysql-restic-backup.service
[Timer]
 
OnBootSec=1min
[Install]
OnUnitActiveSec=1min
WantedBy=timers.target
Unit=mysql-restic-backup.service
```
[Install]
WantedBy=timers.target


Enable and start:
Enable and start:
```bash
sudo systemctl daemon-reexec
sudo systemctl enable --now mysql-restic-backup.timer
```


---
sudo systemctl daemon-reexec
sudo systemctl enable --now mysql-restic-backup.timer


## ⚠️ Every 5 Seconds (Experimental)
==Every 5 Seconds (Experimental)==


Use this only for testing. Real backups this frequently are unsafe.
Use this only for testing. Real backups this frequently are unsafe.


Edit timer:
Edit timer:
```ini
[Timer]
OnBootSec=5
OnUnitActiveSec=5
Unit=mysql-restic-backup.service
```


This runs the service every 5 seconds. **Warning**: It may trigger overlaps or heavy disk/network usage.
[Timer]
OnBootSec=5
OnUnitActiveSec=5
Unit=mysql-restic-backup.service
 
This runs the service every 5 seconds. '''Warning''': It may trigger overlaps or heavy disk/network usage.


To avoid overlap, you can use a systemd feature to prevent concurrent executions:
To avoid overlap, you can use a systemd feature to prevent concurrent executions:


Edit the service:
Edit the service:
```ini
[Service]
Type=oneshot
ExecStart=/usr/local/bin/mysql_backup_restic.sh
ExecStartPre=/bin/sleep 1
ExecStartPost=/bin/sleep 1
TimeoutStartSec=4
```


---
[Service]
Type=oneshot
ExecStart=/usr/local/bin/mysql_backup_restic.sh
ExecStartPre=/bin/sleep 1
ExecStartPost=/bin/sleep 1
TimeoutStartSec=4


Would you like a `rest-server` (remote repo) example included too?
Would you like a `rest-server` (remote repo) example included too?

Latest revision as of 11:55, 5 May 2025

Running `restic` every 5 seconds to back up a MySQL database on a remote Ubuntu server is technically possible but not recommended due to:

  • Backup overhead (even incremental backups every 5 seconds is resource-intensive).
  • Potential data inconsistency and corruption due to overlapping jobs.
  • `cron` does not support per-second precision; you'd need `systemd` timers for that.

That said, here's a safe and practical version (e.g., every 1–5 minutes), and also a technical example for every 5 seconds using `systemd`.

Step 1: Create MySQL Dump Script

/usr/local/bin/mysql_backup_restic.sh
#!/bin/bash

# Settings
MYSQL_USER="root"
MYSQL_PASS="yourpassword"
BACKUP_DIR="/var/backups/mysql"
DUMP_FILE="$BACKUP_DIR/mysql_dump.sql"
RESTIC_REPO="/path/to/restic/repo"
RESTIC_PASSWORD="yourresticpassword"

# Dump MySQL
mkdir -p "$BACKUP_DIR"
mysqldump -u "$MYSQL_USER" -p"$MYSQL_PASS" --all-databases > "$DUMP_FILE"

# Backup with restic
export RESTIC_REPO
export RESTIC_PASSWORD

restic backup "$DUMP_FILE"

Make it executable:

chmod +x /usr/local/bin/mysql_backup_restic.sh


Step 2: Create systemd Service

/etc/systemd/system/mysql-restic-backup.service
[Unit]
Description=Backup MySQL and upload to restic

[Service]
Type=oneshot
ExecStart=/usr/local/bin/mysql_backup_restic.sh

Step 3: Create Timer (Every Minute)

/etc/systemd/system/mysql-restic-backup.timer 
[Unit]
Description=Run mysql-restic-backup every minute

[Timer]
OnBootSec=1min
OnUnitActiveSec=1min
Unit=mysql-restic-backup.service

[Install]
WantedBy=timers.target

Enable and start:

sudo systemctl daemon-reexec
sudo systemctl enable --now mysql-restic-backup.timer

Every 5 Seconds (Experimental)

Use this only for testing. Real backups this frequently are unsafe.

Edit timer:

[Timer]
OnBootSec=5
OnUnitActiveSec=5
Unit=mysql-restic-backup.service

This runs the service every 5 seconds. Warning: It may trigger overlaps or heavy disk/network usage.

To avoid overlap, you can use a systemd feature to prevent concurrent executions:

Edit the service:

[Service]
Type=oneshot
ExecStart=/usr/local/bin/mysql_backup_restic.sh
ExecStartPre=/bin/sleep 1
ExecStartPost=/bin/sleep 1
TimeoutStartSec=4

Would you like a `rest-server` (remote repo) example included too?