diff --git a/apiserver/apiserver/api/management/commands/set_backup_path.py b/apiserver/apiserver/api/management/commands/set_backup_path.py new file mode 100644 index 0000000..8ab1c2c --- /dev/null +++ b/apiserver/apiserver/api/management/commands/set_backup_path.py @@ -0,0 +1,15 @@ +from django.core.management.base import BaseCommand, CommandError +from django.core.cache import cache + +class Command(BaseCommand): + help = 'Record where the last backup was saved.' + + def add_arguments(self, parser): + parser.add_argument('backup_path', type=str) + + + def handle(self, *args, **options): + backup_path = options['backup_path'] + cache.set('backup_path', backup_path) + self.stdout.write('Set backup path to: ' + backup_path) + diff --git a/apiserver/create_backup.sh b/apiserver/create_backup.sh new file mode 100755 index 0000000..bca5dad --- /dev/null +++ b/apiserver/create_backup.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# be safe +set -euf -o pipefail + + +uuid="`cat /proc/sys/kernel/random/uuid`" +date="`date -I`" + +api_folder="/opt/spaceport/apiserver" +data_folder="/opt/spaceport/apiserver/data" +backup_folder="/opt/spaceport/apiserver/backups" + +file_name="spaceport-backup-${date}.tar.gz" +path_name="${backup_folder}/${uuid}" +full_name="${path_name}/${file_name}" + +mkdir "${path_name}" +tar -czf "${full_name}" --directory "${api_folder}" data/ + +echo "Wrote backup to: ${uuid}/${file_name}" + +/opt/spaceport/apiserver/env/bin/python \ + /opt/spaceport/apiserver/manage.py \ + set_backup_path "${uuid}/${file_name}" + +# test these carefully +find "${backup_folder}" -mindepth 1 -type d -print +#find "${backup_folder}" -mindepth 1 -type d -ctime +14 -print +#find "${backup_folder}" -mindepth 1 -type d -ctime +14 -exec rm -r {} \;