|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Bitwarden Automated Backup" |
| 4 | +date: 2023-06-26 12:00:00 -0400 |
| 5 | +category: "Service Setup" |
| 6 | +tags: ['linux', 'bitwarden', 'backup'] |
| 7 | +--- |
| 8 | + |
| 9 | +## Purpose |
| 10 | + |
| 11 | +## Installing the Bitwarden CLI |
| 12 | +```bash |
| 13 | +curl -L -o bw.zip "https://vault.bitwarden.com/download/?platform=linux&app=cli" |
| 14 | +unzip bw.zip |
| 15 | +sudo mv ./bw /usr/local/bin |
| 16 | +rm bw.zip |
| 17 | +``` |
| 18 | + |
| 19 | +## Bitwarden API Credentials |
| 20 | + |
| 21 | +/root/.bash_profile |
| 22 | + |
| 23 | +## Running the Script |
| 24 | + |
| 25 | +```text |
| 26 | +# Before you run this script you will need to have the following environment variables set |
| 27 | +# BW_CLIENTID // Bitwarden API app client ID |
| 28 | +# BW_CLIENTSECRET // Bitwarden API app client secret |
| 29 | +# BW_PASSWORD // Bitwarden login password |
| 30 | +# BW_NOTIFICATION_EMAIL // Email address used for notification if job fails |
| 31 | +
|
| 32 | +bw login --apikey |
| 33 | +
|
| 34 | +export BW_SESSION=$(bw unlock --raw $BW_PASSWORD) |
| 35 | +
|
| 36 | +if [ "$BW_SESSION" == "" ]; then |
| 37 | + echo "The automated Bitwarden backup failed when trying to unlock the vault" | mail -s "Bitwarden Backup Failed" $BW_NOTIFICATION |
| 38 | + bw logout |
| 39 | + exit 1 |
| 40 | +fi; |
| 41 | +
|
| 42 | +EXPORT_OUTPUT_BASE="bw_export_" |
| 43 | +TIMESTAMP=$(date "+%Y%m%d%H%M%S") |
| 44 | +ENC_OUTPUT_FILE=$EXPORT_OUTPUT_BASE$TIMESTAMP.enc |
| 45 | +
|
| 46 | +bw --raw --session $BW_SESSION export --format json | openssl enc -aes-256-cbc -pbkdf2 -iter 1000000 -k $BW_PASSWORD -out $ENC_OUTPUT_FILE |
| 47 | +
|
| 48 | +bw logout |
| 49 | +unset BW_SESSION |
| 50 | +``` |
| 51 | + |
| 52 | +## Adding to `crontab` |
| 53 | + |
| 54 | +```bash |
| 55 | +sudo crontab -e |
| 56 | +``` |
| 57 | + |
| 58 | +```conf |
| 59 | +PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin |
| 60 | +0 0 * * * BASH_ENV=/root/.bash_profile /bin/bash /root/backup.sh |
| 61 | +``` |
| 62 | + |
| 63 | +## Validate Decryption |
| 64 | + |
| 65 | +```bash |
| 66 | +OUTNAME=$(basename $1 .enc).json |
| 67 | +openssl enc -aes-256-cbc -pbkdf2 -iter 1000000 -d -nopad -in $1 -out $OUTNAME |
| 68 | +``` |
0 commit comments