Skip to content

Instantly share code, notes, and snippets.

@godber
Forked from tyrm/mastodon_s3_backup.sh
Created November 23, 2022 11:28
Show Gist options
  • Save godber/010a2e290495b62d7bb7bbc43febbafb to your computer and use it in GitHub Desktop.
Save godber/010a2e290495b62d7bb7bbc43febbafb to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
#imports
. /etc/default/backup
# config
NAME=$1
# generated
case $NAME in
ptzo-masto-pettingzoo)
BUCKET=ptzo-masto-pettingzoo
ALIAS=do-sfo3
;;
*)
echo Instance code $INSTANCE unknown.
exit 1
;;
esac
BACKUPPATH=ph/backups-s3/${ALIAS}/$(date +%Y)/$(date +%m)/$(date +%d)/
SCRATCH=/scratch/s3-mirror/${ALIAS}
TARBALL=${SCRATCH}/${BUCKET}.txz
# sync files from cloud
if [ ! -d "${SCRATCH}" ]; then
echo
echo Creating scratch folder
mkdir -p ${SCRATCH};
fi
echo
echo Syncing Files
nice mc --no-color \
mirror \
--overwrite \
--remove \
--monitoring-address localhost:9969 \
${ALIAS}/${BUCKET} ${SCRATCH}/${BUCKET}
# compress
if [ -f "${TARBALL}" -o -f "${TARBALL}.gpg" ]; then
echo
echo Removing Old Tarball
rm -Rvf ${TARBALL} ${TARBALL}.gpg
fi
CORES=$(grep -c ^processor /proc/cpuinfo)
COREDIV=2
COMPTHREADS=$(( CORES / COREDIV ))
echo
echo Compressing using ${CORES} threads
cd ${SCRATCH}
#nice tar --create --xz --file=${TARBALL} ${BUCKET}$
nice tar cf - ${BUCKET} | nice xz -T ${CORES} -z - > ${TARBALL}
# encrypt
echo
echo Encrypting
nice gpg --yes --batch --passphrase=${GPG_PASSWORD} -c ${TARBALL}
# upload to backups
nice mc cp ${TARBALL}.gpg ${BACKUPPATH}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment