diff --git a/docs/docker/tips.md b/docs/docker/tips.md index f27621049..d3fc30a5d 100644 --- a/docs/docker/tips.md +++ b/docs/docker/tips.md @@ -57,3 +57,26 @@ cardanocommunity/cardano-node:latest # Mandatory: image to run #### Un-Official Docker managment cli tool - [Lazydocker](https://github.com/jesseduffield/lazydocker) + +### Docker backups and restores + +The docker container has an optional backup and restore functionality that can be used to backup the `/opt/cardano/cnode/db` directory. To have the +backup persist longer than the countainer, the backup directory should be mounted as a volume. + +[!NOTE] +The backup and restore functionality is disabled by default. + +[!WARNING] +Make sure adequate space exists on the host as the backup will double the space consumed by the database. + +#### Creating a Backup + +When the container is started with the **ENABLE_BACKUP** environment variable set to **Y** the container will automatically create a +backup in the `/opt/cardano/cnode/backup/$NETWORK-db` directory. The backup will be created when the container is started and if the +backup directory is smaller than the db directory. + +#### Restoring from a Backup + +When the container is started with the **ENABLE_RESTORE** environment variable set to **Y** the container will automatically restore +the latest backup from the `/opt/cardano/cnode/backup/$NETWORK-db` directory. The database will be restored when the container is started +and if the backup directory is larger than the db directory. \ No newline at end of file diff --git a/files/docker/node/addons/entrypoint.sh b/files/docker/node/addons/entrypoint.sh index f91b737d6..8247eac40 100755 --- a/files/docker/node/addons/entrypoint.sh +++ b/files/docker/node/addons/entrypoint.sh @@ -16,15 +16,21 @@ echo "NETWORK: $NETWORK $POOL_NAME $TOPOLOGY"; echo "NODE: $HOSTNAME - Port:$CNODE_PORT - $POOL_NAME"; cardano-node --version; -dbsize=$(du -s ${CNODE_HOME}/db | awk '{print $1}') -bksizedb=$(du -s $CNODE_HOME/priv/$NETWORK-db 2>/dev/null | awk '{print $1}') +if [[ "${ENABLE_BACKUP}" == "Y" ]] || [[ "${ENABLE_RESTORE}" == "Y" ]]; then + [[ ! -d "${CNODE_HOME}"/backup/$NETWORK-db ]] && mkdir -p $CNODE_HOME/backup/$NETWORK-db + dbsize=$(du -s $CNODE_HOME/db | awk '{print $1}') + bksizedb=$(du -s $CNODE_HOME/backup/$NETWORK-db 2>/dev/null | awk '{print $1}') + if [[ "${ENABLE_RESTORE}" == "Y" ]] && [[ "$dbsize" -lt "$bksizedb" ]]; then + echo "Backup Started" + cp -rf "${CNODE_HOME}"/backup/"${NETWORK}"-db/* "${CNODE_HOME}"/db 2>/dev/null + echo "Backup Finished" + fi -if [[ "$dbsize" -lt "$bksizedb" ]]; then -cp -rf $CNODE_HOME/priv/$NETWORK-db/* ${CNODE_HOME}/db 2>/dev/null -fi - -if [[ "$dbsize" -gt "$bksizedb" ]]; then -cp -rf $CNODE_HOME/db/* $CNODE_HOME/priv/$NETWORK-db/ 2>/dev/null + if [[ "${ENABLE_BACKUP}" == "Y" ]] && [[ "$dbsize" -gt "$bksizedb" ]]; then + echo "Restore Started" + cp -rf "${CNODE_HOME}"/db/* "${CNODE_HOME}"/backup/"${NETWORK}"-db/ 2>/dev/null + echo "Restore Finished" + fi fi # Customisation