# Cleaning inventory and device-auth sync commands

**URL:** <https://hub.mender.io/t/cleaning-inventory-and-device-auth-sync-commands/6136>\
**Category:** General Discussions\
**Created:** [August 22, 2023, 12:28pm UTC](https://hub.mender.io/t/cleaning-inventory-and-device-auth-sync-commands/6136 "2023-08-22T12:28:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://hub.mender.io/u/peter)\
**Post date:** [August 22, 2023, 12:28pm UTC](https://hub.mender.io/t/cleaning-inventory-and-device-auth-sync-commands/6136/1 "2023-08-22T12:28:28Z")

</div>

Hey Yoann

I have started a new thread @Piocky in order not to trash [Mender server deviceauth propagate issue - #22 by peter](https://hub.mender.io/t/mender-server-deviceauth-propagate-issue/4831/22).  
the commands with the inventory db you have to run in the mongo shell:

```auto
docker exec -i `docker ps | grep mongo | head -1 | awk '{print($1);}'` mongo

```

and there

```auto
use inventory
db.devices.deleteMany({})

```

this is assuming you are running docker (I thought you mentioned that). you have to locate your inventory db with `show dbs` unless you went to some extra work there should be one with the prefix `inventory`. (it maybe just `inventory`).  
the other commands I was mentioning, just try to run them with help argument and see if they exist:

```auto
docker exec -i `docker ps | grep device-auth | head -1 | awk '{print($1);}'` deviceauth propagate-inventory-statuses --help
docker exec -i `docker ps | grep device-auth | head -1 | awk '{print($1);}'` deviceauth propagate-inventory-id-data --help
it

```

let me know how it goes.

best regards,  
peter

---

<div class="post-metadata">

**Author:** ![Piocky](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/piocky/32/1840_2.png) [@Piocky](https://hub.mender.io/u/Piocky)\
**Post date:** [August 22, 2023, 2:37pm UTC](https://hub.mender.io/t/cleaning-inventory-and-device-auth-sync-commands/6136/2 "2023-08-22T14:37:12Z")

</div>

Hi @peter,

Ok good idea, I thought it was a related issue.

The commands worked, but I’ve lost all the groups that I made… I used that script to save my db:

```auto
# more save_db.sh
docker run -it --rm -v db-artifacts:/from -v $PWD:/to -w /from alpine ash -c 'tar c * | gzip -9 > /to/db-artifacts-backup.tar.gz'
docker run -it --rm -v mender-artifacts:/from -v $PWD:/to -w /from alpine ash -c 'tar c * | gzip -9 > /to/mender-artifacts-backup.tar.gz'
docker run -it --rm -v mender-db:/from -v $PWD:/to -w /from alpine ash -c 'tar c * | gzip -9 > /to/mender-db-backup.tar.gz'
docker run -it --rm -v mender-elasticsearch-db:/from -v $PWD:/to -w /from alpine ash -c 'tar c * | gzip -9 > /to/mender-elasticsearch-db-backup.tar.gz'
docker run -it --rm -v mender-redis-db:/from -v $PWD:/to -w /from alpine ash -c 'tar c * | gzip -9 > /to/mender-redis-db-backup.tar.gz'

```

But I don’t know how to recover my db from that?

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://hub.mender.io/u/peter)\
**Post date:** [August 22, 2023, 2:49pm UTC](https://hub.mender.io/t/cleaning-inventory-and-device-auth-sync-commands/6136/3 "2023-08-22T14:49:00Z")

</div>

ok, the groups. right. how many did you have?  
are the devices all visible?

peter

---

<div class="post-metadata">

**Author:** ![Piocky](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/piocky/32/1840_2.png) [@Piocky](https://hub.mender.io/u/Piocky)\
**Post date:** [August 22, 2023, 2:56pm UTC](https://hub.mender.io/t/cleaning-inventory-and-device-auth-sync-commands/6136/4 "2023-08-22T14:56:24Z")

</div>

I’d say around 10-15 maybe.  
I see all my devices yes.

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://hub.mender.io/u/peter)\
**Post date:** [August 22, 2023, 3:48pm UTC](https://hub.mender.io/t/cleaning-inventory-and-device-auth-sync-commands/6136/5 "2023-08-22T15:48:35Z")

</div>

send me the mender-db-backup.tar.gz dump peter@northern.tech and I will see what we can do.

peter

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://hub.mender.io/u/peter)\
**Post date:** [August 22, 2023, 8:30pm UTC](https://hub.mender.io/t/cleaning-inventory-and-device-auth-sync-commands/6136/6 "2023-08-22T20:30:03Z")

</div>

Hey Yoann,

it is better to use mongodunp/mongorestore to backup/restore the database.  
with the backup you have, you have to run mongo and decompress the archive into data base root (e.g.: the default /data/db or other that you will point mongod to). thats what I did, then in the mongodb shell you can run:

```auto
var c=db.devices.find({"attributes.system-group.value":{"$exists":1}},{"attributes.system-group.value":1}).pretty(); while (c.hasNext()) { var e=c.next(); var id=e._id; var group=e.attributes["system-group"].value; print('db.devices.updateOne({"_id":"'+id+'"},{"$set":{"attributes.system-group.value":"'+group+'","name":"group","scope":"system"}})'); };

```

to get the required script to update your database to restore the device groups.  
thats what I also did and I sent you the script to run which will restore the groups over email, just run it in the mongo shell of your current database.

best regards,  
peter

---

<div class="post-metadata">

**Author:** ![Piocky](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/piocky/32/1840_2.png) [@Piocky](https://hub.mender.io/u/Piocky)\
**Post date:** [August 22, 2023, 10:02pm UTC](https://hub.mender.io/t/cleaning-inventory-and-device-auth-sync-commands/6136/7 "2023-08-22T22:02:48Z")

</div>

Hey Peter,

Ok I see, do you have any commands or link(s) for a procedure to follow, so I could improve my save\_db script for the future? Is it only for mender-db or for others too?

Sorry for my little knowledge on this, I am not the one that deployed the Mender Server, just the one in charge of maintaining it.

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://hub.mender.io/u/peter)\
**Post date:** [August 22, 2023, 10:27pm UTC](https://hub.mender.io/t/cleaning-inventory-and-device-auth-sync-commands/6136/8 "2023-08-22T22:27:48Z")

</div>

Hey Yoann,

you can refer to 3.1 docs: [Backup and restore | Mender documentation](https://docs.mender.io/3.1/server-installation/installation-with-docker-compose/backup-and-restore#database)  
and in general using the [mongodump](https://www.mongodb.com/docs/database-tools/mongodump/) command is a better way to backup than the raw copy of the database data directory.

best regards,  
peter

---

<div class="post-metadata">

**Author:** ![Piocky](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/piocky/32/1840_2.png) [@Piocky](https://hub.mender.io/u/Piocky)\
**Post date:** [August 23, 2023, 8:13am UTC](https://hub.mender.io/t/cleaning-inventory-and-device-auth-sync-commands/6136/9 "2023-08-23T08:13:46Z")

</div>

Thanks, so if I understand correctly, I should add `../../migration/dump-db` at the end of my save\_db.sh file, and if necessary use `restore-db` to restore the MongoDB DB, right?

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://hub.mender.io/u/peter)\
**Post date:** [August 23, 2023, 8:24am UTC](https://hub.mender.io/t/cleaning-inventory-and-device-auth-sync-commands/6136/10 "2023-08-23T08:24:32Z")

</div>

should work, and if it does not let me know.

pg
