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

This guide is for backing up and restoring an Anaconda Repository instance
that uses local file system storage. If your instance uses Amazon S3 or any
other storage provider, please consult their specific documentation on backup
and restore procedures.

Before you start
================

These instructions are for a Repository that is:

* Installed in the directory ``/home/anaconda-server/repo`` as suggested by the installation guide.
* Owned by the ``anaconda-server`` user.
* Using the storage directory ``/opt/anaconda-server/package-storage``.
* Storing the configuration file in ``/etc/anaconda-server``.

If any of these items are different for your instance, modify these
instructions accordingly.

* Unless noted, 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.33.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-11-27

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. This checksum will be used to
verify when you restore the archive.

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

This step is necessary only if you stored Anaconda Repository's configuration
in a custom location outside of the instance installation folder
(usually ``/home/anaconda-server/etc/``).

These commands show how to generate the tarfile if the configuration is stored
in ``/etc/anaconda-server``.

Generate the 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 uses
`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``, 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
=======

Before you start
----------------

* Verify that the restore 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``)
  * A storage directory (usually ``/opt/anaconda-server/package-storage``) owned
    by the Anaconda Repository user account. This is only needed if you're using
    a local filesystem as a storage backend.

* 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.33.6-2017-11-27.sh: OK
    anaconda-server-mongodb-2.33.6-2017-11-27.archive: OK
    anaconda-server-package-storage-2.33.6-2017-11-27.tar: OK
    anaconda-server-repo-2.33.6-2017-11-27.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

After restoring this file, log out and log in as ``anaconda-server`` again for
the changes to take effect.

Database
--------

If you followed 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
