🗃️ Using Docker to archive a service (BookStack)
Tags: docker, opensource, tutorial, beginners
Published: 2023-02-09 14:04:38

I was on a process to dockerize my infrastructure and I wanted to archive a service I couldn't simply migrate to a newer version nor really use anymore. 

So I used Docker to archive it without depending on any dependency.

```bash
# First I go into the website's directory
cd /var/www/BookStack

# I generate a SQL script as a backup of the database named "bookstack_db"
mysqldump -u root bookstack_db > bookstack-backup-db.sql

# Then I used tar to create an archive of files (.env, uploads)
tar -czvf bookstack-backup-files.tar.gz .env public/uploads storage/uploads

# Then I take the version of BookStack
cat version
```

Then I download those files back to my laptop.

```bash
mkdir bookstack-backup
cd bookstack-backup
rsync -rv debian@snowcode.ovh:/var/www/BookStack/bookstack-backup-* .
```

Then, I created a `docker-compose` file with the following content to describe my infrastructure.

```yml
version: "2"
services:
  bookstack:
    image: ghcr.io/linuxserver/bookstack:version-v0.31.4 # Here goes my bookstack version
    container_name: bookstack
    environment:
      - PUID=1000
      - PGID=1000
      - APP_URL=http://localhost:8080
      - DB_HOST=bookstack_db
      - DB_PORT=3306
      - DB_USER=bookstack
      - DB_PASS=secret
      - DB_DATABASE=bookstackapp
    volumes:
      - ./bookstack_files:/config
    ports:
      - 8080:80
    depends_on:
      - bookstack_db
  bookstack_db:
    image: lscr.io/linuxserver/mariadb:10.6.10
    container_name: bookstack_db
    environment:
      - PUID=1000
      - PGID=1000
      - MYSQL_ROOT_PASSWORD=secret
      - TZ=Europe/Brussels
      - MYSQL_DATABASE=bookstackapp
      - MYSQL_USER=bookstack
      - MYSQL_PASSWORD=secret
    volumes:
      - ./bookstack_db:/config
```

Finally I can start the server with that config...

```bash
sudo docker-compose up
```

...and place the files into the right volumes/directories.

```bash
sudo su # I need to be root for accessing the volumes
mv bookstack-backup-files.tar.gz bookstack_files
mv bookstack-backup-db.sql bookstack_db
cd bookstack_files
tar xvzf bookstack-backup-files.tar.gz
```

Finally I can enter the running container of my database to import the script

```bash
sudo docker exec -it bookstack_db bash -c "mysql -u bookstack -p bookstackapp < /config/bookstack-backup-db.sql"
# The default password is secret as defined in the docker-compose
```

The website can be accessed at `http://localhost:8080`

## Archiving the Docker images
Now to not depend of the Docker registeries we can also archive the images of `mariadb` and `bookstack`

First we can list the images:

```bash
sudo docker images
```

Then take the IDs of the `mariadb` and `bookstack` images to export them into a TAR archive.

```bash
sudo docker image save aad0c49aebf3 -o bookstack.tar
sudo docker image save 39a4293c3071 -o mariadb.tar
```

Now it's done ! When we need it we can import the images back from the archives using those commands: 

```bash
sudo docker image import bookstack.tar ghcr.io/linuxserver/bookstack:version-v0.31.4 
sudo docker image import mariadb.tar lscr.io/linuxserver/mariadb:10.6.10
```

And now you no longer depends on those registeries either for using this backup.