[GH-ISSUE #470] Backup script for SQLite database. Will it work and are they correct? #290

Closed
opened 2026-03-03 01:27:40 +03:00 by kerem · 3 comments
Owner

Originally created by @BobWs on GitHub (May 2, 2019).
Original GitHub issue: https://github.com/dani-garcia/vaultwarden/issues/470

I was wondering which one of these scripts is best for backing up my bitwarden database. Script 1 has been converted from a mysql database and script 2 googled and adapted to my preferences.

Script 1:

#!/bin/bash
#
DIR=/path/to/backup/destination/
BACKUP_FILE=/path/to/file/db.sqlite3
DATESTAMP=$(date +%Y%m%d%H%M%S)

#create backup dir if it does not exist
mkdir -p ${DIR}

#remove all backups except the $KEEP latests
KEEP=5
BACKUPS=find ${DIR} -name "sqldump-.gz" | wc -l | sed 's/\ //g' while [ $BACKUPS -ge $KEEP ] do ls -tr1 ${DIR}sqldump-.gz | head -n 1 | xargs rm -f BACKUPS=expr $BACKUPS - 1
done

#list sqlit3 databases and dump each
FILENAME=${DIR}sqldump-${DATESTAMP}.gz
cp $BACKUP_FILE $DIR --flush-logs | gzip > $FILENAME

Script 2:

#!/bin/bash

BACKUP_FILE="/path/to/file/db.sqlite3"
BACKUP_DIR="/path/to/backup/destination/“

today=$(date "+%Y-%m-%d")

# Less than 31 days old, i.e. 30 days or younger
if find "$BACKUP_FILE" -type f -mtime -31 | grep -q .
then
find "$BACKUP_DIR" -type f -mtime +30 -delete
fi

last_backup=$(ls -t "$BACKUP_DIR" | head -n 1)
if [ ! "$last_backup" ] || ! cmp -s "$BACKUP_FILE" "$BACKUP_DIR/$last_backup"
then
cp "$BACKUP_FILE" "$BACKUP_DIR/$today.sqlite3

Originally created by @BobWs on GitHub (May 2, 2019). Original GitHub issue: https://github.com/dani-garcia/vaultwarden/issues/470 I was wondering which one of these scripts is best for backing up my bitwarden database. Script 1 has been converted from a mysql database and script 2 googled and adapted to my preferences. **Script 1:** `#!/bin/bash` `#` `DIR=/path/to/backup/destination/` `BACKUP_FILE=/path/to/file/db.sqlite3` `DATESTAMP=$(date +%Y%m%d%H%M%S)` `#create backup dir if it does not exist` `mkdir -p ${DIR}` `#remove all backups except the $KEEP latests` `KEEP=5` `BACKUPS=`find ${DIR} -name "sqldump-*.gz" | wc -l | sed 's/\ //g'` `while [ $BACKUPS -ge $KEEP ]` `do` `ls -tr1 ${DIR}sqldump-*.gz | head -n 1 | xargs rm -f` `BACKUPS=`expr $BACKUPS - 1` `done` `#list sqlit3 databases and dump each` `FILENAME=${DIR}sqldump-${DATESTAMP}.gz` `cp $BACKUP_FILE $DIR --flush-logs | gzip > $FILENAME` **Script 2:** `#!/bin/bash` `BACKUP_FILE="/path/to/file/db.sqlite3"` `BACKUP_DIR="/path/to/backup/destination/“` `today=$(date "+%Y-%m-%d")` `# Less than 31 days old, i.e. 30 days or younger` ` if find "$BACKUP_FILE" -type f -mtime -31 | grep -q .` ` then` `find "$BACKUP_DIR" -type f -mtime +30 -delete` `fi` `last_backup=$(ls -t "$BACKUP_DIR" | head -n 1)` `if [ ! "$last_backup" ] || ! cmp -s "$BACKUP_FILE" "$BACKUP_DIR/$last_backup"` `then` `cp "$BACKUP_FILE" "$BACKUP_DIR/$today.sqlite3`
kerem closed this issue 2026-03-03 01:27:40 +03:00
Author
Owner

@mprasil commented on GitHub (May 2, 2019):

Simply copying file might lead to corrupted backup. (not very likely, but also not the best practice) so keep that in mind. Maybe consider dumping the DB instead as described in the wiki, which also covers backing up other files.

As for rotating backup files, I'd say the second script looks somewhat cleaner although I don't quite see what's the point of smp -s.... if you're going to run the backup daily.

<!-- gh-comment-id:488639651 --> @mprasil commented on GitHub (May 2, 2019): Simply copying file might lead to corrupted backup. (not very likely, but also not the best practice) so keep that in mind. Maybe consider dumping the DB instead as [described in the wiki](https://github.com/dani-garcia/bitwarden_rs/wiki/Backing-up-your-vault), which also covers backing up other files. As for rotating backup files, I'd say the second script looks somewhat cleaner although I don't quite see what's the point of `smp -s....` if you're going to run the backup daily.
Author
Owner

@mprasil commented on GitHub (May 16, 2019):

I'm going to close this as the question probably has been answered with the link to wiki. Feel free to reopen if something still isn't clear.

<!-- gh-comment-id:493099647 --> @mprasil commented on GitHub (May 16, 2019): I'm going to close this as the question probably has been answered with the link to wiki. Feel free to reopen if something still isn't clear.
Author
Owner

@BobWs commented on GitHub (May 17, 2019):

Okay thanks, at the moment I'm using Hyper Backup to backup Bitwarden on my Synology.
https://www.synology.com/en-global/dsm/feature/hyper_backup

<!-- gh-comment-id:493420887 --> @BobWs commented on GitHub (May 17, 2019): Okay thanks, at the moment I'm using Hyper Backup to backup Bitwarden on my Synology. https://www.synology.com/en-global/dsm/feature/hyper_backup
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vaultwarden#290
No description provided.