--- title: Bitwarden --- ### 1. Prepare the environment 1.1 Create custom webapp called `warden` in the control panel 1.2 Configure Ruby 2.4 by editing `~/.bash_aliases` and adding these aliases ```sh alias ruby=ruby2.4 alias gem=gem2.4 ``` 1.3 Install the bundle gem ```sh gem install bundle ``` ### 2. Install Bitwarden Ruby into the directory ```sh cd ~/webapps clone https://github.com/jcs/bitwarden-ruby.git warden cd warden bundle install ``` ### 3. Start / Stop / Restart and Cron scripts 3.1 `Start` script where `$PORT` is the port of the custom webapp ```sh #!/bin/sh pgrep -f "ruby2.4 /home/tanshu/gems/bin/rackup -p $PORT config.ru" > /dev/null 2>&1 && exit 0 mkdir -p /home/tanshu/webapps/warden/run cd /home/tanshu/webapps/warden nohup env RACK_ENV=production bundle exec rackup -p $PORT config.ru > /dev/null 2>&1 & sleep 3 pgrep -f "ruby2.4 /home/tanshu/gems/bin/rackup -p $PORT config.ru" > /home/tanshu/webapps/warden/run/ruby.pid ``` 3.2 `Stop` script where `$PORT` is the port of the custom webapp ```sh #!/bin/sh /usr/bin/pkill -f "ruby2.4 /home/tanshu/gems/bin/rackup -p $PORT config.ru" && rm -f /home/tanshu/webapps/warden/run/ruby.pid ``` 3.3 `Restart` script ```sh #!/bin/bash $HOME/webapps/warden/bin/stop sleep 3 $HOME/webapps/warden/bin/start ``` 3.4 Edit crontab using `export VISUAL=nano; crontab -e` and add the following line ```sh */20 * * * * ~/webapps/warden/bin/start ``` ### 4. Backup your database #### Install gdrive Install gdrive to backup the database to google account in case of data loss at webfaction Go to `https://github.com/prasmussen/gdrive` and download `gdrive-linux-x64` in `~/bin/` ```sh wget -o ~/bin/gdrive $URL_FOR_GDRIVE_LINUX_X64_BINARY ``` Make it executable `chmod +x ~/bin/gdrive` #### Setup local directory for upload ```sh mkdir -p ~/Data/warden cd ~/Data/warden ln -s ~/webapps/warden/db/production.sqlite3 production.sqlite3 ``` #### Configure Google Drive Link your gdrive to the account by running `gdrive list` and following the instructions. Create a folder called `Bitwarden` and get its id by running `gdrive list`. Create upload script named `bw.sh` in the home directory and make it executable `chmod +x ~/bw.sh` ```sh #!/usr/bin/env bash cd ~/Data/warden if sha512sum --status --check production.sha512sum; then echo "File already uploaded. No update needed" else echo "Files are different. Uploading" ~/bin/gdrive upload --parent $FOLDER_ID --name production_$(date +%F-%H:%M).sqlite3 ~/Data/warden/production.sqlite3 sha512sum production.sqlite3 > production.sha512sum fi ``` #### Automate Backups Edit crontab using `export VISUAL=nano; crontab -e` and add the following line ```sh 0 1 * * * ~/bw.sh ```