.. 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.

    Alternatively, you can use the following script to retrieve the size programmatically.

    .. white_toggle::
      :titleen: Powershell script :
      :titlefr: Script Powershell:

        .. code-block::
          
          # Directory paths
          $waptPath = "C:\wapt\waptserver\repository\wapt"
          $wadsPath = "C:\wapt\waptserver\repository\wads"
          $wuaPath = "C:\wapt\waptserver\repository\waptwua"
          $wapthostPath = "C:\wapt\waptserver\repository\wapt-host"

          # Function to calculate the size of a directory
          function Get-DirectorySize {
              param (
                  [string]$path
              )
              $size = (Get-ChildItem $path -Recurse -File -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
              return $size
          }

          # Calculate sizes for each directory
          $waptSize = Get-DirectorySize -path $waptPath
          $wadsSize = Get-DirectorySize -path $wadsPath
          $wuaSize = Get-DirectorySize -path $wuaPath
          $wapthostSize = Get-DirectorySize -path $wapthostPath

          # Convert to GB and display
          $waptGB = [math]::Round($waptSize / 1GB, 2)
          $wadsGB = [math]::Round($wadsSize / 1GB, 2)
          $wuaGB = [math]::Round($wuaSize / 1GB, 2)
          $wapthostGB = [math]::Round($wapthostSize / 1GB, 2)

          Write-Output "Size of $waptPath : $waptSize octets ($waptGB GB)"
          Write-Output "Size of $wadsPath : $wadsSize octets ($wadsGB GB)"
          Write-Output "Size of $wuaPath : $wuaSize octets ($wuaGB GB)"
          Write-Output "Size of $wapthostPath : $wapthostSize octets ($wapthostGB GB)"

          # Combined total
          $totalSize = $waptSize + $wadsSize + $wuaSize + $wapthostSize
          $totalGB = [math]::Round($totalSize / 1GB, 2)
          Write-Output "Total size to migrate: $totalSize bytes ($totalGB GB)"

.. _linux_restore_server:

*****
Linux
*****

* Follow the standard WAPT server installation process, **but do NOT execute the postconf.py script**.

For Debian and derivatives: :ref:`installation guide <base_debian_server_install>`

For Redhat and derivatives: :ref:`installation guide <base_redhat-based_server_install>`

After installation, ensure the new WAPT server is renamed to match the exact server name and IP address of the previous server (srvwapt).
Update the following configuration files accordingly:

.. code-block::

  /etc/hostname
  /etc/hosts
  /etc/network/interfaces
  /etc/resolv.conf

* 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:: Windows -> Debian

    .. code-block::

      C:\wapt\waptserver\repository\wapt      =>  /var/www/wapt/
      C:\wapt\waptserver\repository\wapt-host =>  /var/www/wapt-host/
      C:\wapt\waptserver\repository\waptwua   =>  /var/www/waptwua/
      C:\wapt\waptserver\repository\wads      =>  /var/www/wads/
      C:\wapt\conf                            =>  /opt/wapt/conf/
      C:\wapt\waptserver\nginx\ssl            =>  /opt/wapt/waptserver/ssl/
      C:\wapt\waptserver\repository\*.json    =>  /var/www/*.json
      
    Modify the data in the waptserver.ini

    Go to :file:`/opt/wapt/conf/waptserver.ini`, and correct the following sentences :

    .. code-block::

      clients_signing_key = c:\wapt\conf\ca-srvwaptname.mydomain.lan.pem => clients_signing_key = /opt/wapt/conf/ca-srvwaptname.mydomain.lan.pem
      clients_signing_certificate = c:\wapt\conf\ca-srvwaptname.mydomain.lan.crt => clients_signing_certificate = /opt/wapt/conf/ca-srvwaptname.mydomain.lan.crt

    Change the path of the server certificate in /etc/nginx/sites-avalaible/wapt.conf

    Modify following the sentence :

    .. code-block::

      ssl_client_certificate "c:\wapt\conf\ca-srvwaptname.mydomain.lan.crt" => ssl_client_certificate "/opt/wapt/ca-srvwaptname.mydomain.lan.crt"

    * Recreate the symbolic link 

    .. code-block::

      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:: Windows -> Redhat

    .. code-block::
    
        C:\wapt\waptserver\repository\wapt      =>  /var/www/html/wapt/
        C:\wapt\waptserver\repository\wapt-host =>  /var/www/html/wapt-host/
        C:\wapt\waptserver\repository\waptwua   =>  /var/www/html/waptwua/
        C:\wapt\waptserver\repository\wads      =>  /var/www/html/wads/
        C:\wapt\conf                            =>  /opt/wapt/conf/
        C:\wapt\waptserver\nginx\ssl            =>  /opt/wapt/waptserver/ssl/
        C:\wapt\waptserver\repository\*.json    =>  /var/www/html/*.json

    Modify the data in the waptserver.ini

    Go to :file:`/opt/wapt/conf/waptserver.ini`, and correct the following sentences :

    .. code-block::

      clients_signing_key = c:\wapt\conf\ca-srvwaptname.mydomain.lan.pem => clients_signing_key = /opt/wapt/conf/ca-srvwaptname.mydomain.lan.pem
      clients_signing_certificate = c:\wapt\conf\ca-srvwaptname.mydomain.lan.crt => clients_signing_certificate = /opt/wapt/conf/ca-srvwaptname.mydomain.lan.crt

    Change the path of the server certificate in /etc/nginx/sites-avalaible/wapt.conf

    Modify following the sentence :

    .. code-block::

      ssl_client_certificate "c:\wapt\conf\ca-srvwaptname.mydomain.lan.crt" => ssl_client_certificate "/opt/wapt/ca-srvwaptname.mydomain.lan.crt"

    * Recreate the symbolic link 

    .. code-block::

      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

  .. tab-item:: Debian -> Redhat

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

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

      vi /opt/wapt/conf/waptserver.ini

      wapt_folder = /var/www/html/wapt

      waptwua_folder= /var/www/html/waptwua

      wapt-host_folder= /var/www/html/wapt-host

      wads_folder= /var/www/html/wads
  
    Adapt path for the :file:`nginx` configuration.

    .. code-block::

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

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

    .. hint::

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

      .. code-block::
  
          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

    * Recreate the symbolic link 

    .. code-block::

      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

  .. tab-item:: Redhat -> Debian

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

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

      vi /opt/wapt/conf/waptserver.ini

      wapt_folder = /var/www/wapt

      waptwua_folder= /var/www/waptwua

      wapt-host_folder= /var/www/wapt-host

      wads_folder= /var/www/wads
  
    Adapt path for the :file:`nginx` configuration.

    .. code-block::

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

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

    .. hint::

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

      .. code-block::

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

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

    * Recreate the symbolic link 

    .. code-block::

      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:: Debian -> Debian

    .. code-block::

      /var/www/wapt/             =>  /var/www/wapt/
      /var/www/wapt-host/        =>  /var/www/wapt-host/
      /var/www/waptwua/          =>  /var/www/waptwua/
      /var/www/wads/             =>  /var/www/wads/
      /opt/wapt/conf/            =>  /opt/wapt/conf/
      /opt/wapt/waptserver/ssl/  =>  /opt/wapt/waptserver/ssl/
      /var/www/*.json            =>  /var/www/*.json

    .. hint::

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

      .. code-block::

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

          sudo chown root:www-data /etc/nginx/http-krb5.keytab
    
    * Recreate the symbolic link 

    .. code-block::

      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 -> Redhat

    .. code-block::

      /var/www/html/wapt/             =>  /var/www/html/wapt/
      /var/www/html/wapt-host/        =>  /var/www/html/wapt-host/
      /var/www/html/waptwua/          =>  /var/www/html/waptwua/
      /var/www/html/wads/             =>  /var/www/html/wads/
      /opt/wapt/conf/                 =>  /opt/wapt/conf/
      /opt/wapt/waptserver/ssl/       =>  /opt/wapt/waptserver/ssl/
      /var/www/html/*.json            =>  /var/www/html/*.json

    .. hint::

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

      .. code-block::
          
          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

    * Recreate the symbolic link 

    .. code-block::

      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

* Launch the postconf.sh

.. code-block::

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

* Stop WAPT related services on the WAPT Server.

.. code-block:: bash

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

* Verify the Database

.. code-block::

  su - postgres
  psql -l
  exit

* 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

    .. code-block::

      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

    .. code-block::
        
      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/

* Run a Scanpackages on your repositories.

.. tab-set::

  .. tab-item:: Debian and derivatives

    .. code-block::
      
      wapt-scanpackages /var/www/wapt/ -linfo

  .. tab-item:: RedHat and derivatives

    .. code-block::

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

* Lastly relaunch the post.conf

.. code-block::

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

The restoration process is now complete

.. _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-15\bin\psql.exe" -U postgres -c "drop database wapt;"
  "C:\wapt\waptserver\pgsql-15\bin\psql.exe" -U postgres -c "create database wapt with owner=wapt encoding='utf-8';"
  "C:\wapt\waptserver\pgsql-15\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
