Backup script for SQLite database. Will it work and are they correct? #289

Closed
opened 2025-11-07 06:31:34 -06:00 by GiteaMirror · 3 comments
Owner

Originally created by @BobWs on GitHub (May 2, 2019).

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). 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`
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.

@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.

@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

@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.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/vaultwarden#289