Skip to content

Setting up backups on the server

Gaurang Tandon edited this page Jun 6, 2021 · 2 revisions

The backup strategy for the bot data is the same as for any MongoDB database. We use crontab and mongodump. A script runs daily to dump the mongo db contents to a predefined location:

#!/bin/bash

mongodump --db DB_NAME --collection users --out output-location

Then, setup another laptop to have ssh access to the above location (using ssh-key password-less identification). Use the following script on this laptop:

#!/bin/bash

# CONFIGURAION START

FROM_IP="[email protected]"

FROM_DIR="" # LCA directory of all the data collections (that need to be backed up)
TO_DIR="" # destination directory for the backups

# individual paths of the data collections
FROM_PATHS=( "/path1/dump1" "path2/dump2" )
TO_PATHS=( "/dest1" "/dest2" )

# CONFIGURAION END

timestamp() {
    date +"%Y-%m-%d_%H-%M"
}

for i in "${!FROM_PATHS[@]}"; do
    from=${FROM_PATHS[i]}
    to=${TO_PATHS[i]}

    FROM_DIR_EXACT="$FROM_DIR$from"
    TO_DIR_EXACT="$TO_DIR$to"

    FROM_ADDR="$FROM_IP:$FROM_DIR_EXACT"

    CURR_DIR="$TO_DIR_EXACT/$(timestamp)"
    mkdir -p "$CURR_DIR"

    scp -r "$FROM_ADDR" "$CURR_DIR"
done

Use crontab -e to set up these scripts to run daily. For example, like so: 0 5 * * * /home/user/backup.sh

Clone this wiki locally