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

.. meta::
  :description: Backing up the WAPT Server
  :keywords: backup server, WAPT, PostgreSQL, pg_dumpall, FastCopy, WinSCP, documentation

.. _backup_server:

##########################
Backing up the WAPT Server
##########################

To backup your WAPT Server follow this procedure. **Regular backups are recommended**.

Before starting the migration of the WAPT server, it is highly recommended to **perform a thorough cleanup of the repository**. This will facilitate the migration by **reducing the amount of data to be transferred**, speeding up the process, and minimizing the risks of corruption or errors.


.. white_toggle::
  :titleen: Cleanup Steps of packages in the waptconsole:
  :titlefr: Etapes de nettoyage des paquets de la waptconsole :

    *1- Package Identification:*

    Review all packages present in the repository.
    Identify packages that have multiple versions, such as Firefox and others.

    .. hint::

      In the WAPT Package tab you can select all packages, and do a right-click :guilabel:`Select obsolete packages`.
      **Use with caution and at your own risk.**

    *2- Keeping Current Versions:*

    For each package with multiple versions, keep only the version currently in production.
    Remove old versions and unused versions.

    *3- Comprehensive Verification:*

    Ensure this operation is performed for all packages in the repository.
    Verify that the retained versions are indeed those used in production.


.. _linux_backup_server:

*****
Linux
*****

* Stop WAPT related services on the WAPT Server.

.. code-block:: bash

  systemctl stop wapttasks
  systemctl stop waptserver-uwsgi (if used)
  systemctl stop waptserver
  systemctl stop nginx


* Backup recursively these directories using a backup tool (ex: :program:`rsync`, :program:`WInSCP`, etc..).

.. 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, save the :file:`keytab` file too.
  The :file:`keytab` file is located in the :file:`nginx` folder.

  .. tab-set::

    .. tab-item:: Debian and derivatives

      /etc/nginx/http-krb5.keytab

    .. tab-item:: RedHat and derivatives

      /etc/nginx/http-krb5.keytab



* Backup the PostgreSQL database using the :program:`pg_dumpall` utility (adapt filename with your requirements).

.. code-block:: bash

  sudo -u postgres pg_dumpall > /tmp/backup_wapt.sql

* Restart WAPT related services on the WAPT Server.

.. code-block:: bash

  systemctl start wapttasks
  systemctl start waptserver
  systemctl start nginx

.. _windows_backup_server:

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

* Calculate the amount of data to migrate

To determine the data size, navigate to the following directory: **C:\\wapt\\waptserver\\repository**.

Then, check the folder size by: Right-clicking the repository folder, Selecting Properties, and Viewing the Size field.

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)"

* Stop WAPT related services on the WAPT Server.

.. code-block:: bash

  net stop WAPTServer
  net stop WAPTService
  net stop WAPTTftpServer

* Backup the WAPT repository folder on a remote backup destination.

.. code-block:: bash

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

* Backup PostgreSQL Database with :program:`pg_dump.exe`.

.. note::

  The commands may require adaptation. For example, if your repository is located at "C:\\wapt\\waptserver\\pgsql-15", your PostgreSQL database is version 15. 
  
  Always verify the version of PostgreSQL installed on your system before proceeding.

.. code-block:: bash

  "C:\wapt\waptserver\pgsql-15\bin\pg_dumpall.exe" -U postgres -f C:\backup_wapt.sql

* Restart WAPT related services on the WAPT Server.

.. code-block:: bash

  net start WAPTPostgresql
  net start WAPTServer
  net start WAPTService
  net start WAPTTftpServer

