.. Reminder for header structure:
  Parts (H1)          : #################### with overline
  Chapters (H2)       : ******************** with overline
  Sections (H3)       : ====================
  Subsections (H4)    : --------------------
  Subsubsections (H5) : ^^^^^^^^^^^^^^^^^^^^
  Paragraphs (H6)     : """"""""""""""""""""

.. meta::
  :description: Restoring the WAPT Server
  :keywords: restore, server, WAPT, PostgreSQL, pg_dumpall, FastCopy, WinSCP, documentation

.. _restore_server:

#########################
Restoring the WAPT Server
#########################

In case of a complete crash, restart a standard WAPT Server installation on your WAPT Server. **But don't launch the wapt postconfiguration**.
Then follow this procedure to restore your data.

By default, **we recommend retaining the same IP address and Fully Qualified Domain Name (FQDN) on the new WAPT server**.

To ensure that the new WAPT server has sufficient disk space for the repository, monitor the volume of your old WAPT server after performing the cleanup process. This will help you **determine the appropriate disk space requirements for the new server**.

.. tab-set::

  .. tab-item:: Linux

    For Debian and derivatives:

    .. code-block:: shell

        du -sh /var/www/
        du -sh /opt/wapt/

    For Redhat and derivatives:

    .. code-block:: shell

      du -sh /opt/wapt/
      du -sh /var/www/html/

  .. tab-item:: Windows

    To display the size of the "C:\\wapt"
    repository, follow these steps:

    1- Open File Explorer and navigate to "This PC" or "My Computer."

    2- Locate and right-click on the "C:\\wapt" folder.
    
    3- From the context menu, select "Properties."
    
    4- In the Properties window, you will see various tabs. Ensure you are on the "General" tab.
    
    5- Look for the "Size" and "Size on disk" lines, which will display the total size of the folder and its contents.


.. _linux_restore_server:

*****
Linux
*****

* Stop WAPT related services on the WAPT Server.

.. code-block:: bash

  systemctl stop nginx
  systemctl stop waptserver
  systemctl stop waptserver-uwsgi
  systemctl stop wapttasks

* Restore the following directories.

.. tab-set::

  .. tab-item:: Debian and derivatives

    .. code-block::
        
      /var/www/wapt/
      /var/www/wapt-host/
      /var/www/waptwua/
      /var/www/wads/
      /opt/wapt/conf/
      /opt/wapt/waptserver/ssl/
      /var/www/*.json

  .. tab-item:: RedHat and derivatives

    .. code-block::
        
      /var/www/html/wapt/
      /var/www/html/wapt-host/
      /var/www/html/waptwua/
      /var/www/html/wads/
      /opt/wapt/conf/
      /opt/wapt/waptserver/ssl/
      /var/www/html/*.json

.. hint::

  If you use Kerberos to authenticate hosts and users, restore the file and rights.

  .. tab-set::

    .. tab-item:: Debian and derivatives

      sudo chmod 640 /etc/nginx/http-krb5.keytab

      sudo chown root:www-data /etc/nginx/http-krb5.keytab

    .. tab-item:: RedHat and derivatives
      
      sudo chown root:nginx /etc/nginx/http-krb5.keytab

      sudo chmod 640 /etc/nginx/http-krb5.keytab
      
      restorecon -v -R /etc/nginx/http-krb5.keytab


* Restore the database (adapt the name of your file).
  The first command **deletes** the WAPT database (if it exists).
  Make sure that your dump file is correct before deleting!

.. warning::

  Check the encoding before creating the wapt database, if the dumped file is in en_US, your new base has to be en_US.

.. code-block:: bash

  cd /tmp
  sudo -u postgres psql -c "drop database wapt"
  sudo -u postgres psql -c "create database wapt with owner=wapt encoding='utf-8'"
  sudo -u postgres psql < /tmp/backup_wapt.sql

* Apply ownership rights to the restored folders.

.. tab-set::

  .. tab-item:: Debian and derivatives

    chown -R wapt:www-data /var/www/wapt/
    chown -R wapt:www-data /var/www/wapt-host/
    chown -R wapt:www-data /var/www/waptwua/
    chown -R wapt:www-data /var/www/wads/
    chown -R wapt /opt/wapt/conf/
    chown -R wapt /opt/wapt/waptserver/ssl/

  .. tab-item:: RedHat and derivatives

    chown -R wapt:nginx /var/www/html/wapt/
    chown -R wapt:nginx /var/www/html/wapt-host/
    chown -R wapt:nginx /var/www/html/waptwua/
    chown -R wapt:nginx /var/www/html/wads/
    chown -R wapt /opt/wapt/conf/
    chown -R wapt /opt/wapt/waptserver/ssl/

.. important::

  **If you change waptserver Debian for Redhat or vice versa**. Don't forget to check somethings :

  Adapt news paths for parameter on your new server in the :file:`waptserver.ini`

  .. tab-set::

    .. tab-item:: Debian and derivatives

      vi /opt/wapt/conf/waptserver.ini

      wapt_folder = /var/www/wapt
      waptwua_folder= /var/www/waptwua

    .. tab-item:: RedHat and derivatives

      vi /opt/wapt/conf/waptserver.ini

      wapt_folder = /var/www/html/wapt
      waptwua_folder= /var/www/html/waptwua
  
  Adapt path for the :file:`nginx` configuration.

  .. tab-set::

    .. tab-item:: Debian and derivatives

      vi /etc/nginx/sites-available/wapt.conf

      Root paths must point to "/var/www/"
    
    .. tab-item:: RedHat and derivatives

      vi /etc/nginx/conf.d/wapt.conf

      Root paths must point to "/var/www/html/"


  Recreating symbolic links.

  .. tab-set::

    .. tab-item:: Debian and derivatives

      ln -s /var/www/wapt/waptagent/waptsetup.exe waptsetup.exe
      ln -s /var/www/wapt/waptagent/waptsetup.exe waptsetup-tis.exe
      ln -s /var/www/wapt/waptagent/waptdeploy.exe waptdeploy.exe

    .. tab-item:: RedHat and derivatives

      ln -s /var/www/html/wapt/waptagent/waptsetup.exe waptsetup.exe
      ln -s /var/www/html/wapt/waptagent/waptsetup.exe waptsetup-tis.exe
      ln -s /var/www/html/wapt/waptagent/waptdeploy.exe waptdeploy.exe


* Run a Scanpackages on your repositories.


.. tab-set::

  .. tab-item:: Debian and derivatives

    wapt-scanpackages /var/www/wapt/

  .. tab-item:: RedHat and derivatives

    wapt-scanpackages /var/www/html/wapt/

* Restart WAPT related services on the WAPT Server.

.. code-block:: bash

  systemctl start wapttasks
  systemctl start waptserver
  systemctl start nginx

* Launch the postconf.sh

.. code-block::

  /opt/wapt/waptserver/scripts/postconf.sh


.. _windows_restore_server:

*******
Windows
*******

* Stop WAPT related services on the WAPT Server.

.. code-block:: bash

  net stop WAPTServer
  net stop WAPTService
  net stop WAPTTftpServer

* Restore the following directories.

.. code-block:: bash

  C:\wapt\waptserver\repository\wapt
  C:\wapt\waptserver\repository\wapt-host
  C:\wapt\waptserver\repository\waptwua
  C:\wapt\waptserver\repository\wads
  C:\wapt\conf
  C:\wapt\waptserver\nginx\ssl

* Apply full rights to the folder :file:`C:\\wapt\\waptserver\\repository` for the "Network Service" group.

* Restore the database (adapt the name of your file).
  The first command **deletes** the WAPT database (if it exists).
  Make sure that your dump file is correct before deleting!

.. warning::

  Check the encoding before creating the wapt database, if the dumped file is in en_US, your new base has to be en_US.

.. code-block:: bash

  "C:\wapt\waptserver\pgsql-14\bin\psql.exe" -U postgres -c "drop database wapt;"
  "C:\wapt\waptserver\pgsql-14\bin\psql.exe" -U postgres -c "create database wapt with owner=wapt encoding='utf-8';"
  "C:\wapt\waptserver\pgsql-14\bin\psql.exe" -f c:\backup_wapt.sql -U postgres

* Scan package repositories.

.. code-block:: bash

  wapt-scanpackages "C:\wapt\waptserver\repository\wapt"

* Restart WAPT related services on the WAPT Server.

.. code-block:: bash

  net start WAPTServer
  net start WAPTService
  net start WAPTTftpServer
