Skip to content

Instantly share code, notes, and snippets.

@minacle minacle/mastodon-backup.sh
Last active Nov 12, 2017

Embed
What would you like to do?
#!/bin/sh
MASTODON_DIRECTORY='/var/lib/mastodon/live'
BACKUP_DIRECTORY='/var/lib/mastodon/backups'
DATE_FORMAT='%y%m%d%H%M%S'
#COMPRESSOR='bzip2'
#COMPRESSOR='gzip'
#COMPRESSOR='lz4'
COMPRESSOR='lzfse'
#COMPRESSOR='xz'
CHECKSUM='sha1sum'
#CHECKSUM='sha224sum'
#CHECKSUM='sha256sum'
#CHECKSUM='sha384sum'
#CHECKSUM='sha512sum'
#=======================================================================
# Copyright (c) 2017 Mayu Laierlence <minacle@live.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#=======================================================================
MASTODON_ROOT="$MASTODON_DIRECTORY"
TEMP=$(ls -ld "$MASTODON_ROOT")
MASTODON_OWNER=$(echo "$TEMP" | awk '{print $3}')
MASTODON_GROUP=$(echo "$TEMP" | awk '{print $4}')
_UID=$(id -u)
if [ $_UID -ne 0 ] && [ $(id -un) != $MASTODON_OWNER ]
then
echo 'You do not have permission.' >&2
exit 1
fi
BACKUP_ROOT="$BACKUP_DIRECTORY/$(date +"$DATE_FORMAT")"
DATABASE_DUMP_EXTENSION='pg_dump'
ARCHIVE_FILE="$BACKUP_ROOT.tar"
TEMP=$(cat "$MASTODON_ROOT/.env.production")
for KEY in DB_HOST DB_USER DB_NAME DB_PASS DB_PORT
do
eval $(echo "$TEMP" | awk "/^$KEY=/ {print \$0}")
done
PGPASSWORD="$DB_PASS"
mkdir -p "$BACKUP_ROOT"
pg_dump $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER \
>"$BACKUP_ROOT/$DB_NAME.$DATABASE_DUMP_EXTENSION"
mkdir -p "$BACKUP_ROOT/public"
cp -R "$MASTODON_ROOT/public/system" "$BACKUP_ROOT/public"
cp "$MASTODON_ROOT/.env.production" "$BACKUP_ROOT"
test $_UID -eq 0 && chown -R $MASTODON_OWNER:$MASTODON_GROUP "$BACKUP_ROOT"
cd "$BACKUP_ROOT"
case "$COMPRESSOR" in
bzip2)
tar -c --format pax . | bzip2 >"$ARCHIVE_FILE.bz2"
;;
gzip)
tar -c --format pax . | gzip >"$ARCHIVE_FILE.gz"
;;
lz4)
tar -c --format pax . | lz4 >"$ARCHIVE_FILE.lz4"
;;
lzfse)
tar -c --format pax . | lzfse -encode >"$ARCHIVE_FILE.lzfse"
;;
xz)
tar -c --format pax . | xz >"$ARCHIVE_FILE.xz"
;;
'')
tar -cf "$ARCHIVE_FILE" --format pax .
;;
*)
tar -c --format pax . | "$COMPRESSOR" \
>"$ARCHIVE_FILE.$(echo "$COMPRESSOR" | awk '{print $1}')"
;;
esac
ARCHIVE_FILE="$(find .. -name "$(basename "$ARCHIVE_FILE")*")"
case "$CHECKSUM" in
sha1sum)
shasum -a 1 "$ARCHIVE_FILE" >"$ARCHIVE_FILE.sha1"
;;
sha224sum)
shasum -a 224 "$ARCHIVE_FILE" >"$ARCHIVE_FILE.sha224"
;;
sha256sum)
shasum -a 256 "$ARCHIVE_FILE" >"$ARCHIVE_FILE.sha256"
;;
sha384sum)
shasum -a 384 "$ARCHIVE_FILE" >"$ARCHIVE_FILE.sha384"
;;
sha512sum)
shasum -a 512 "$ARCHIVE_FILE" >"$ARCHIVE_FILE.sha512"
;;
'')
;;
*)
"$CHECKSUM" "$ARCHIVE_FILE"* \
>"$ARCHIVE_FILE.$(echo "$CHECKSUM" | awk '{print $1}')"
;;
esac
rm -r "$BACKUP_ROOT"
test $_UID -eq 0 && chown $MASTODON_OWNER:$MASTODON_GROUP "$ARCHIVE_FILE"*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.