================================================
Anaconda repository backup and restore procedure
================================================

Preconditions
=============

The following guide shows how to backup and restore an instance that uses local file system storage. For an instance that uses Amazon S3 or any other storage provider, please consult their specific documentation on backup and restore procedures.

We also assume:

* The instance is installed in ``/home/anaconda-server/repo`` as suggested by the installation guide.
* The instance is owned by the ``anaconda-server`` user.
* The storage directory is ``/opt/anaconda-server/package-storage``.

If your instance has a different installation directory, owner, or storage directory, modify these instructions accordingly.

Run all shell commands while logged in as the ``anaconda-server`` user. Using ``sudo`` privileges, log in as the ``anaconda-server`` user with this command::

    sudo su - anaconda-server

Execute all commands in the working directory ``/home/anaconda-server``::

    $ pwd
    /home/anaconda-server

Backup
======

Before starting the backup process, shut the service down using ``supervisorctl``::

    $ supervisorctl stop all
    binstar-server: stopped
    $ supervisorctl status
    binstar-server                   STOPPED    Jul  6 05:05 PM

Make a ``$VERSION`` environment variable and set it to the version of the currently installed Anaconda Repository::

    $ VERSION=`conda list binstar-server --json | python -c 'import sys, json; print json.load(sys.stdin)[0]["version"]'`
    $ echo $VERSION
    2.32.6

This version string will be used in all backup file names.

It's also useful to add a timestamp to the files, so generate one now::

    $ TIMESTAMP=`date +%Y-%m-%d`
    $ echo $TIMESTAMP
    2017-09-06

Code/Binaries
-------------

Generate a tarfile archive with the installed code, binaries and any dependencies::

    $ tar -cpsf anaconda-server-repo-$VERSION-$TIMESTAMP.tar --exclude var/run -C /home/anaconda-server repo/
    $ sha1sum anaconda-server-repo-$VERSION-$TIMESTAMP.tar > anaconda-server-repo-$VERSION-$TIMESTAMP.tar.sha1

Notice that this also generated a SHA1 checksum. It will be verified when restoring the archive.

Configuration
-------------

This step is only necessary if you stored Anaconda Repository's configuration in a custom location outside of the instance installation folder (usually ``/home/anaconda-server/etc/``). We'll assume that the configuration is stored in ``/etc/anaconda-server``. Generate a tarfile with its SHA1 checksum::

    $ tar -cpsf anaconda-server-etc-$VERSION-$TIMESTAMP.tar /etc/anaconda-server
    $ sha1sum anaconda-server-etc-$VERSION-$TIMESTAMP.tar > anaconda-server-etc-$VERSION-$TIMESTAMP.tar.sha1

Storage
-------

As before, create a tarfile archive and its checksum with the contents of the package storage location::

    $ tar -cpsf anaconda-server-package-storage-$VERSION-$TIMESTAMP.tar -C /opt/anaconda-server/ package-storage
    $ sha1sum anaconda-server-package-storage-$VERSION-$TIMESTAMP.tar > anaconda-server-package-storage-$VERSION-$TIMESTAMP.tar.sha1

Database
--------

Generate a dump of Anaconda Repository's MongoDB database. We recommend you follow MongoDB's guidelines for `backup and restore <https://docs.mongodb.com/manual/core/backups/>`_. This guide only shows how to do it using `MongoDB tools <https://docs.mongodb.com/manual/tutorial/backup-and-restore-tools/>`_::

    $ mongodump --host=127.0.0.1 --port=27017 --archive=anaconda-server-mongodb-$VERSION-$TIMESTAMP.archive
    $ sha1sum anaconda-server-mongodb-$VERSION-$TIMESTAMP.archive > anaconda-server-mongodb-$VERSION-$TIMESTAMP.archive.sha1

.bashrc
-------

If you chose to let the Anaconda Repository installer update the ``.bashrc`` file of the user ``anaconda-server``, you'll also need to back it up::

    $ cp /home/anaconda-server/.bashrc anaconda-server-bashrc-$VERSION-$TIMESTAMP.sh
    $ sha1sum anaconda-server-bashrc-$VERSION-$TIMESTAMP.sh > anaconda-server-bashrc-$VERSION-$TIMESTAMP.sh.sha1

Restore
=======

Preconditions
-------------

Before starting to restore Anaconda Repository, make sure that the environment meets the requirements listed in the :doc:`Installation Guide <install>` for Anaconda Repository. You will need:

* MongoDB (any supported version) installed
* A user account (usually ``anaconda-server``)
* If you're using a local filesystem as storage backend, you'll need a storage directory (usually ``/opt/anaconda-server/package-storage``) owned by the Anaconda Repository user account.

Run all shell commands while logged in as the ``anaconda-server`` user, as you did when backing up Anaconda Repository. Using ``sudo`` privileges, log in as the ``anaconda-server`` user with this command::

    sudo su - anaconda-server

Execute all commands in the working directory ``/home/anaconda-server``.

Verify checksums
----------------

Verify the integrity of the backup files::

    $ sha1sum --check *.sha1
    anaconda-server-bashrc-2.32.6-2017-09-06.sh: OK
    anaconda-server-mongodb-2.32.6-2017-09-06.archive: OK
    anaconda-server-package-storage-2.32.6-2017-09-06.tar: OK
    anaconda-server-repo-2.32.6-2017-09-06.tar: OK

.bashrc
-------

If you backed up the ``.bashrc`` file of the user ``anaconda-server``, restore it::

    cp anaconda-server-bashrc-$VERSION-$TIMESTAMP.sh /home/anaconda-server/.bashrc

You'll need to login as ``anaconda-server`` once again for the changes to take effect.

Database
--------

If you followed the first steps of the Anaconda Repository :doc:`Installation Guide <install>`, MongoDB is up and running and you can use ``mongorestore`` to restore the database archive::

    mongorestore --host=127.0.0.1 --port=27017 --db=binstar --archive=anaconda-server-mongodb-$VERSION-$TIMESTAMP.archive

Storage
-------

Assuming that the storage directory is ``/opt/anaconda-server/package-storage``, restore it with::

    tar -xpsf anaconda-server-package-storage-$VERSION-$TIMESTAMP.tar -C /opt/anaconda-server/

Code/Binaries
-------------

Restore the code and binaries::

    tar -xpsf anaconda-server-repo-$VERSION-$TIMESTAMP.tar -C /home/anaconda-server

Restore the ``supervisord`` configuration::

    repo/bin/anaconda-server-install-supervisord-config.sh

The server should now be up and running. Check the status with ``supervisorctl``::

    $ repo/bin/supervisorctl status
    binstar-server                   RUNNING    pid 8446, uptime 0:03:18
