Skip to content

Instantly share code, notes, and snippets.

@kidGodzilla
Last active October 18, 2024 03:26
Show Gist options
  • Save kidGodzilla/92bfeb3c96b12c8c266f307052b55b9c to your computer and use it in GitHub Desktop.
Save kidGodzilla/92bfeb3c96b12c8c266f307052b55b9c to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
######################################################################
# This simple backup script generates commands to recreate dokku apps
#
# It creates the app, adds domains, and restores configuration for each app
# It does not handle databases, persistent storage, or other plugin settings
#
# Usage: Run this script on a Dokku server (you must either be logged in as the
# dokku user, or root, or `sudo su` first.
#
# Then, save the ouput of this script to a file. That's your backup.
# You can restore to a fresh dokku server install by running the generated commands
#
# Note: you will need to add remotes and `git push dokku main:master` for each app
# you deploy to the new server
######################################################################
# See Comments in the related GitHub Gist below for usage instructions
######################################################################
# Ensure we are running as root
check_root() {
if [ "$USER" != "root" ]; then
echo "Permission Denied"
echo "Can only be run by root"
exit
fi
}
backup_storage_locations() {
cd /var/lib/dokku/data/storage
if ls -d */ > /dev/null 2>&1; then
echo "##############################"
echo "# Persistent Storage"
echo "##############################"
for d in */ ; do
appname=$(echo "$d" | sed 's:/*$::')
echo "sudo dokku storage:mount $appname /var/lib/dokku/data/storage/$appname:/storage"
done
fi
}
make_backups() {
cd /home/dokku
for d in */ ; do
appname=$(echo "$d" | sed 's:/*$::')
echo "##############################"
echo "# $appname"
echo "##############################"
echo ""
echo "sudo dokku apps:create $appname"
echo ""
# nw_strr="${strr//$'\n'/ }"
# value=`cat config.txt`
domains=`cat "$d/VHOST"`
env=`cat "$d/ENV"`
domains="${domains//$'\n'/ }"
env="${env//$'\n'/ }"
echo "sudo dokku domains:add $appname $domains"
echo ""
echo "sudo dokku config:set $appname $env"
echo "sudo dokku config:unset $appname GIT_REV"
echo ""
report=$(dokku resource:report "$appname")
current_cpu=$(echo "$report" | grep '_default_ limit cpu' | awk '{print $4}')
current_memory=$(echo "$report" | grep '_default_ limit memory' | awk '{print $4}')
echo "sudo dokku resource:limit --memory $current_memory --cpu $current_cpu $appname"
echo ""
# echo "$domains"
# echo "$env"
done
}
check_root
make_backups
backup_storage_locations
@kidGodzilla
Copy link
Author

kidGodzilla commented Apr 10, 2024

Usage

This simple backup script generates commands to recreate dokku apps.

It creates the app, adds domains, and restores configuration for each app.

It does not handle databases, persistent storage, or other plugin settings.

Usage: Run this script on a Dokku server (you must either be logged in as the
dokku user, or root, or sudo su first.

Then, save the ouput of this script to a file. That's your backup.
You can restore to a fresh dokku server install by running the generated commands.

Note: you will need to add remotes and git push dokku main:master for each app you deploy to the new server.

wget https://gist.githubusercontent.com/kidGodzilla/92bfeb3c96b12c8c266f307052b55b9c/raw/6a15705c81ec7963cff84f19ac49748cbfc86a05/dokku_simple_backup.sh

bash ./dokku_simple_backup.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment