Pod data backup

From diaspora* project wiki
Revision as of 04:09, 17 August 2017 by Waithamai (talk | contribs) (m)

Collecting pod data

You should do backups of all user data, that is the whole database and uploaded images. For the images just make copies of the public/uploads directory. Then just dump the diaspora_production database from your database server.

You may find the following Bash script useful for collecting your pod data and packing it into archive. It dumps your database using diaspora database configuration information, picks public/uploads and config/diaspora.yml and packs that all into a single tar file. The presented version only supports PostgreSQL for now, but MySQL support could easily be added. The script expects to be placed in a subfolder of diaspora installation (e.g. script). If placed somewhere else it would fail with "Unexpected directory" error. In case of correct run the script will create an archive with pod data and file name of diaspora_hostname_date.tar pattern.

#!/bin/bash

# ensure right directory
cd $(dirname $0)/..

if [ "$(cat .ruby-gemset)" != "diaspora" ]; then
  echo "Unexpected directory"
  exit 1
fi

DBCONFIG=$( ruby -r yaml -e "puts YAML.load_file('config/database.yml')['production'].values_at('adapter','host','port','username','password','database','encoding').join(';')" )
IFS=';' read -a DBCONFIG_ARRAY <<< "$DBCONFIG"

ADAPTER=${DBCONFIG_ARRAY[0]}
HOST=${DBCONFIG_ARRAY[1]}
PORT=${DBCONFIG_ARRAY[2]}
USERNAME=${DBCONFIG_ARRAY[3]}
PASSWORD=${DBCONFIG_ARRAY[4]}
DATABASE=${DBCONFIG_ARRAY[5]}
ENCODING=${DBCONFIG_ARRAY[6]}

DATE=$( date +%F_%H-%M-%S )
DUMP_FILE_NAME=${ADAPTER}_${DATABASE}_${DATE}
ARCHIVE_FILE_NAME=diaspora_$(hostname)_${DATE}.tar

case $ADAPTER in
  postgresql)
    echo "Running PostgreSQL dump..."
    TMP_FILE=$( mktemp )
    env PGPASSWORD=$PASSWORD pg_dump -h $HOST -p $PORT -U $USERNAME -E $ENCODING $DATABASE > $TMP_FILE
    status=$?
    if [ $status -ne 0 ]; then
      echo "Error running dump command: $status"
      exit $status
    fi
    gzip -n $TMP_FILE
    mv $TMP_FILE.gz $DUMP_FILE_NAME.sql.gz
    ;;
  *)
    echo "Unknown adapter $ADAPTER"
    exit 1
esac

echo "Creating archive $ARCHIVE_FILE_NAME..."
tar -cf $ARCHIVE_FILE_NAME $DUMP_FILE_NAME.sql.gz public/uploads config/diaspora.yml

status=$?
if [ $status -eq 0 ]; then
  echo "Successfully created archive $ARCHIVE_FILE_NAME."
else
  echo "Error $status running tar. Archive wasn't created."
fi

unlink $DUMP_FILE_NAME.sql.gz
exit $status