backups/README.md
2025-01-09 08:17:44 -08:00

2.5 KiB

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:

sudo apt install restic postgresql-client sendemail

External Services:

  1. An S3 provider. (Amazon S3, Backblaze, etc)
  2. Email provider which allows SMTP. (Gmail, Fastmail, etc)

Setup:

You'll need to create the required directory, and pull the files from this repository:

sudo mkdir /backups/
cd /backups/
git clone https://git.woem.men/woem.men/backups.git

Copy the config_example to config, verify permissions, and then edit using your favorite text editor:

sudo su
cp config_example config
chown root:root config
chmod 700 config
nano config

Next, you'll need to run initial setup on the S3 repository:

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

Restoring:

You'll need to grab your config variables before restoring:

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/