backups/README.md

93 lines
2.8 KiB
Markdown
Raw Normal View History

2025-01-09 16:16:08 +00:00
# About:
This is a very basic backup script that:
1. Exports databases, stores the md5sums, and then compresses them.
2. Uploads the databases, along with specified directories to S3.
3. Prunes old backups.
It will send an email to the specified email if there's an error.
This is the backup script in use on Woem.men's servers.
# Prerequisites:
**Ubuntu Packages:**
```bash
sudo apt install restic postgresql-client sendemail
```
**External Services:**
1. An S3 provider. (Amazon S3, Backblaze, etc)
1. Email provider which allows SMTP. (Gmail, Fastmail, etc)
# Setup:
You'll need to create the required directory, and pull the files from this repository:
```bash
sudo mkdir /backups/
cd /backups/
2025-01-09 16:17:44 +00:00
git clone https://git.woem.men/woem.men/backups.git
2025-01-09 16:16:08 +00:00
```
Copy the `config_example` to `config`, verify permissions, and then edit using your favorite text editor:
```bash
sudo su
cp config_example config
chown root:root config
chmod 600 config
2025-01-09 16:16:08 +00:00
nano config
```
Next, you'll need to run initial setup on the S3 repository:
```bash
sudo su # if not already root
set -a
source config
restic -r "$S3_BUCKET" init
```
And finally, set your crontab file with `sudo crontab -e`:
```
0 * * * * /backups/backup.sh > /backups/logfile.log
```
# Testing:
Make sure that everything works by running the backup script atleast once by hand:
```bash
sudo su
cd /backups/
./backup.sh
```
Once complete, check that a snapshot exists in the backup:
```bash
set -a
source config
restic -r "$S3_BUCKET" snapshots
```
I also recommend doing a test restore as discussed in the next section.
2025-01-09 16:16:08 +00:00
# Restoring:
You'll need to grab your config variables before restoring:
```bash
sudo su # if not already root
cd /backups/
set -a
source config
```
After that, fetch a list of backups available to you by running `restic -r "$S3_BUCKET" snapshots`.
**Example:**
```
root@guilty-spark:/backups# restic -r "$S3_BUCKET" snapshots
repository 17b028ae opened (version 2, compression level auto)
ID Time Host Tags Paths
---------------------------------------------------------------
724327bb 2025-01-09 07:10:04 guilty-spark /backups
/etc
/home
/root
---------------------------------------------------------------
```
Select one, and run the following command, making adjustments as needed for your setup:
Using one of the snapshots, restore the files you're missing using `restic restore`. I recommend reading the man file for `restic-restore`.
**Examples:**
```
root@guilty-spark:/backups# restic -r "$S3_BUCKET" restore 724327bb -i /home/service/example_file.txt -t /tmp/restore/
```
```
root@guilty-spark:/backups# restic -r "$S3_BUCKET" restore 724327bb -t /tmp/restore/
```