-- Leo's gemini proxy

-- Connecting to gemini.patatas.ca:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

2024-03-14

A Quick and Dirty Backup Script for GoToSocial & SQLite

The Final Script


I'll start at the end: here's the final script, which can be placed in your gotosocial directory and run with sudo sh backupscript.sh.


Be sure to read through it first - you may need to alter it for your installation!


#!/bin/bash

# This script, along with a detailed explanation, can be found at: https://thedabbler.patatas.ca/pages/gotosocial_backup_and_restore.html

# first create the following directories: /home/gotosocialBAK and /home/gotosocialBAK/storage
# Make sure the paths below are correct for your install (edit this script as necessary);
# place this script in your gotosocial directory and run **as root** using the command:
# 'sudo sh backupscript.sh'


# CLI command to list media attachments uploaded by local users and output list to a file:
./gotosocial --config-path ./config.yaml admin media list-attachments --local-only | grep storage > localmedia.txt;


# shell 'for loop' to copy the list of files, retaining the directory structure (sort of):
for FILE in $(cat ./localmedia.txt)
do
    cp --parents ${FILE} /home/gotosocialBAK/storage/
done


# copy config file:
cp ./config.yaml /home/gotosocialBAK/ ;


# one of the proper methods to backup running SQLite database:
sqlite3 sqlite.db ".backup '/home/gotosocialBAK/sqlite-BAK.db'" ;


# create .tar.gz archive of the backup with today's date in the filename:
tar -cvf /home/gts_bak_$(date +%Y%m%d).tar /home/gotosocialBAK ;
gzip /home/gts_bak*.tar ;

it down

First things first, create the directory structure ./gotosocialBAK/storage in a place that makes sense for you (this script assumes you're creating these directories inside /home, so edit the script if you're creating them elsewhere).


The first command in the script uses the GoToSocial CLI to list the location of all media attachments that were uploaded by local users, and writes the output to a file called localmedia.txt:


./gotosocial --config-path ./config.yaml admin media list-attachments --local-only | grep storage > localmedia.txt;

This file becomes the input of a 'for' loop that iterates through the list of media attachments and copies them to a backup folder.


The --parents flag tells the system to recreate the folder structure, instead of just copying the jpegs etc into a single folder:


for FILE in $(cat ./localmedia.txt)
do
    cp --parents ${FILE} /home/gotosocialBAK/storage/
done

We copy over the config.yaml with


cp ./config.yaml /home/gotosocialBAK/

And then we'll use a proper method to backup the SQLite database.


Why not just copy the sqlite.db file? Isn't that easier? Well, maybe! But it's very risky: if a write operation happens while it's being copied, we can end up with a corrupt backup file.


By using .backup instead, we're ensuring this doesn't happen. Temporary locks are placed on the database as it's being copied, preventing simultaneous write operations.


sqlite3 sqlite.db ".backup '/home/gotosocialBAK/sqlite-BAK.db'"

Lastly, we'll create an archive of our backup, and put today's date in the filename so that we know when it was created.


tar -cvf /home/gts_bak_$(date +%Y%m%d).tar /home/gotosocialBAK ;
gzip /home/gts_bak*.tar ;

--:--

from backup

A backup isn't much good if you can't use it, so, let's go through one method for restoring your instance.


First off: setup the machine according to the official installation guide found here:

=>https://docs.gotosocial.org/en/latest/getting_started/installation/

but we'll skip the part about editing the configuration file.


Create the directories, download and extract the .tar.gz file. Create the systemd service. Configure the firewall, and nginx if you're using it.


Now upload your backup to the server and extract it with

tar -xzf [filename]

copy the media attachments into the new storage folder, and edit the restored config.yaml file if needed (e.g. if you've changed the path to the gotosocial directory).


At this point, it's probably a good idea to ensure that the contents of /gotosocial have the right ownership:

sudo chown -R gotosocial:gotosocial /gotosocial.
import

To restore your database from backup:

sudo sqlite3

and at the sqlite> prompt enter

ATTACH DATABASE 'sqlite.db' AS gotosocial;
.restore gotosocial sqlite-BAK.db
.exit

and then start the service:

sudo systemctl enable --now gotosocial
sudo systemctl start gotosocial

Hope this helped. And if there's something I've gotten wrong, or that could be improved upon, let me know.


--:--


CC BY-SA 4.0

-- Response ended

-- Page fetched on Sun May 12 20:01:11 2024