# Backup and restore of Swarmica on your own equipment
# Description

This article describes the procedure for backing up (backup) and restoring (restore) the Swarmica application installed on the customer's side (on-premise) using the standard functions of the basic operating system.

:::caution
**IMPORTANT! It is recommended to use specialized solutions for creating backups at the server level to ensure guaranteed data integrity and recovery speed after a failure.**
:::

This article provides minimal instructions for creating backups for recovery after a failure or data migration to another server without guaranteeing the recovery speed (RTO) for all data at the time of backup.

What needs to be saved:

- Configuration files located in the Swarmica installation folder (default `/root/swarmica`)
- Database (using Postgres)
- User files (Docker Volumes)

## Creating a backup (backup)

### Configuration files

Save the contents of the `/root/swarmica` folder:

```bash
tar -cvjf sw-backup-configs.tbz /root/swarmica\
    /root/.docker/
```

### Database

Run the `pg_dump` command in the `swarmica-postgres` container:

```bash
cd /root/swarmica
source .env
docker exec -it --env-file .env swarmica-postgres pg_dump -Fc --clean -U$POSTGRES_USER $POSTGRES_DB -f /backups/db.`date +%Y%m%d%H%M%S`.sql
```

If necessary, copy the dump file to the server (this may be necessary if you plan to restore only the database on another server, for example, for testing):

```bash
docker cp swarmica-postgres:/backups/db.YOURDATE.sql ./
```

### User files

User data is stored in Docker Volumes with the following names:

- swarmica_ugc
- swarmica_runtime
- swarmica_cron_d
- swarmica_ai_assistant

To back up all files, run the following command:

```bash
tar -cvjf sw-backup-files.tbz \
    /var/lib/docker/volumes/swarmica_swarmica_postgres_data_backups/_data/ \
    /var/lib/docker/volumes/swarmica_swarmica_ugc/_data/ \
    /var/lib/docker/volumes/swarmica_swarmica_runtime/_data/ \
    /var/lib/docker/volumes/swarmica_ai_assistant/_data/
```

**IMPORTANT! Make sure there is enough free space on the disk where you create the backup archive to accommodate all user data.**

- Command to check free space `df -h`
- Command to check the size of user data `docker system df | grep Volumes`

## Restoring a backup (restore)



:::danger
IMPORTANT! This procedure will completely restore ALL Swarmica data, including email settings - if you want to create a clone of the system for testing but not send notifications to existing users, **be sure to change the Email configuration to FILE in the Settings > Swarmica Settings section**
:::

Before restoring a backup on another server, you must install all the required software for Swarmica to work.



::::tabs
:::tab-item{title="Ubuntu \ Astra"}


```shell
apt-get update && \
apt-get install --no-install-recommends -y \
      curl \
      jq \
      host \
      python3 \
      docker.io \
      docker-compose-v2
```


:::

:::tab-item{title="RedOS"}


```bash
dnf install -y \
      curl \
      jq \
      bzip2 \
      python3.12 \
      docker-ce \
      docker-compose
```


:::
::::


1. Copy to the target server and unzip the archives created in the previous step:

```bash
tar -xvjf sw-backup-files.tbz -C /
tar -xvjf sw-backup-configs.tbz -C /
```

2. Pull the container images:

```bash
cd /root/swarmica
docker compose pull
```

This step can be skipped if you are restoring data on the same server where the backup was created

3. Start the Postgres container:

```bash
docker compose up -d postgres
```

4. If you are restoring the database dump on a 'clean' installation, first copy it into the container:

```bash
docker cp db.YOURDATE.sql swarmica-postgres:/backups/
```

This step can be skipped if you are restoring data from a backup created following all steps in this article.

5. Restore the dump:

```bash
cd /root/swarmica
source .env
docker exec -it --env-file .env swarmica-postgres pg_restore --if-exists --clean -U$POSTGRES_USER -d$POSTGRES_DB /backups/db.YOURDATE.sql
```

6. Start the remaining containers:

```bash
docker compose up -d
```

7. Perform the final procedures:

```bash
docker exec --env-file .env -it swarmica-django-1 python manage.py post_start
```