# Backup and restore

NextGIS Web uses two data storages for different types of data:

- PostgreSQL database used as general purpose relational database.
- Filesystem storage used for storing BLOBs like raster, attachments, etc.

Also NextGIS Web provides tools for backup and restore this data to / from
single-file archive. You can use any destination directory but these tools
provide some defaults wich may be usefull.

So before we begin, let’s create default backup directory in `/srv/ngw`:

```none
$ mkdir backup
```

And set up backup directory in `config/config.ini`:

```ini
[core]
# Other config options go here
backup.path = /srv/ngw/backup
```

## Backup

To create a backup you need to run command `nextgisweb backup` and it will
create backup and print it’s filename:

```none
$ nextgisweb backup
/srv/ngw/backup/20200721-234619.ngwbackup
```

By default NextGIS Web uses [strftime](https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior) formatted for filename which is
`%Y%m%d-%H%M%S.ngwbackup`. In the example below 2020 is a year, 07 is a
month, 21 is a day and so on. Note that timestamps are always stored in UTC.

## Restore

Now try to restore NextGIS Web from previously created backup. You don’t need to
stop NextGIS Web service during backup but you need to stop it during restore.
If you used systemd in the previous section just run as root following command:

```none
# systemctl stop ngw.service
```

Then run `nextgisweb restore` command with filename argument:

```none
$ nextgisweb restore backup/20200721-234619.ngwbackup
```

If you are restoring backup which made from some previous version, it may
require to apply migrations. The process of applying migrations is described in
[“Backup and restore”](upgrade.html.md#applying-migrations) section. Follow instruction from
there, and then start NextGIS Web service again:

```none
# systemctl start ngw.service
```
