Skip to content

Commit

Permalink
Merge pull request #104 from rafalp/v0-prepare
Browse files Browse the repository at this point in the history
V0 prepare
  • Loading branch information
rafalp authored May 30, 2023
2 parents 3e6fd15 + a2e2b4e commit 18c3349
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 55 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 0.1 (2023-05-30)

- Moved backup logic inside the `misago` container (#98).
- Added `resetcron` command to `./appctl`.
81 changes: 31 additions & 50 deletions appctl
Original file line number Diff line number Diff line change
Expand Up @@ -60,45 +60,46 @@ intro() {
echo
echo "Setup and upgrade:"
echo
echo " ${BOLD}setup${NORMAL} setup new Misago site."
echo " ${BOLD}upgrade${NORMAL} backup, rebuild, migrate and collect static."
echo " ${BOLD}setup${NORMAL} Setup new Misago site."
echo " ${BOLD}upgrade${NORMAL} Backup, rebuild, migrate and collect static."
echo
echo "Docker management:"
echo
echo " ${BOLD}start${NORMAL} start all containers."
echo " ${BOLD}stop${NORMAL} stop all containers."
echo " ${BOLD}restart${NORMAL} stop and start all docker containers."
echo " ${BOLD}rebuild${NORMAL} rebuild and restart Misago and celery container containers."
echo " ${BOLD}rebuildall${NORMAL} pull, rebuild and restart all containers."
echo " ${BOLD}stats${NORMAL} see list and stats of running docker containers."
echo " ${BOLD}start${NORMAL} Start all containers."
echo " ${BOLD}stop${NORMAL} Stop all containers."
echo " ${BOLD}restart${NORMAL} Stop and start all docker containers."
echo " ${BOLD}rebuild${NORMAL} Rebuild and restart Misago and celery container containers."
echo " ${BOLD}rebuildall${NORMAL} Pull, rebuild and restart all containers."
echo " ${BOLD}stats${NORMAL} See list and stats of running docker containers."
echo
echo "Change configuration:"
echo
echo " ${BOLD}forumindex${NORMAL} switch forum index between threads and categories."
echo " ${BOLD}email${NORMAL} change email setup."
echo " ${BOLD}hostname${NORMAL} change hostname setup."
echo " ${BOLD}locale${NORMAL} change locale setup."
echo " ${BOLD}timezone${NORMAL} change timezone setup."
echo " ${BOLD}avatargallery${NORMAL} load avatar gallery."
echo " ${BOLD}sentry${NORMAL} enable or disable Sentry (sentry.io) for logging."
echo " ${BOLD}debug${NORMAL} change debug mode."
echo " ${BOLD}secret${NORMAL} reset secret key."
echo " ${BOLD}forumindex${NORMAL} Switch forum index between threads and categories."
echo " ${BOLD}email${NORMAL} Change email setup."
echo " ${BOLD}hostname${NORMAL} Change hostname setup."
echo " ${BOLD}locale${NORMAL} Change locale setup."
echo " ${BOLD}timezone${NORMAL} Change timezone setup."
echo " ${BOLD}avatargallery${NORMAL} Load avatar gallery."
echo " ${BOLD}sentry${NORMAL} Enable or disable Sentry (sentry.io) for logging."
echo " ${BOLD}debug${NORMAL} Change debug mode."
echo " ${BOLD}secret${NORMAL} Reset secret key."
echo
echo " Note: you need to rebuild Misago container for changes made with those utils to take effect."
echo
echo "Backup:"
echo
echo " ${BOLD}backup${NORMAL} backup and archive database and media."
echo " ${BOLD}restore BACKUP${NORMAL} restore database and media from BACKUP archive."
echo " ${BOLD}backup${NORMAL} Backup and archive database and media."
echo " ${BOLD}restore BACKUP${NORMAL} Restore database and media from BACKUP archive."
echo
echo "Utilities:"
echo
echo " ${BOLD}collectstatic${NORMAL} collect static assets."
echo " ${BOLD}manage.py${NORMAL} runs \"python manage.py\" inside docker."
echo " ${BOLD}bash${NORMAL} starts bash session inside running Misago container."
echo " ${BOLD}run${NORMAL} runs \"docker-compose run --rm misago\"."
echo " ${BOLD}psql${NORMAL} runs psql connected to database."
echo " ${BOLD}flushredis${NORMAL} flush the redis cache."
echo " ${BOLD}collectstatic${NORMAL} Collect static assets."
echo " ${BOLD}manage.py${NORMAL} Runs \"python manage.py\" inside docker."
echo " ${BOLD}bash${NORMAL} Starts bash session inside running Misago container."
echo " ${BOLD}run${NORMAL} Runs \"docker-compose run --rm misago\"."
echo " ${BOLD}psql${NORMAL} Runs psql connected to database."
echo " ${BOLD}flushredis${NORMAL} Flush the redis cache."
echo " ${BOLD}resetcron${NORMAL} Replace host's crontab with default one."
echo
}

Expand Down Expand Up @@ -159,7 +160,7 @@ set_crontab() {
rm -f cron_tmp
touch cron_tmp
echo "30 1 * * * cd $current_path && $docker_compose run --rm misago ./cron" >> cron_tmp
echo "0 1 * * * cd $current_path && ./appctl daily_backup" >> cron_tmp
echo "0 1 * * * cd $current_path && $docker_compose run --rm misago ./.run backup \"auto-\$(date +%Y%m%d%H%M%S)\"" >> cron_tmp
echo "0 1 * * * find $current_path/backups/ -name auto-* -type f -mtime +10 -delete" >> cron_tmp
echo "" >> cron_tmp
crontab cron_tmp
Expand Down Expand Up @@ -320,30 +321,8 @@ reset_secret_key() {
# Create new backup
create_new_backup() {
require_setup
if [[ $1 ]]; then
backup_type="$1"
else
backup_type="manual"
fi
# create tmp backup dir
backup_dir="$backup_type-$(date +%Y%m%d%H%M%S)"
mkdir "./backups/$backup_dir"
# backup database
echo "Backing up database..."
$docker_compose run --rm misago ./.run backup_db $backup_dir
# backup media
echo "Backing up media..."
cp -r ./misago/media "./backups/$backup_dir/media"
cd ./backups/
# archive backup dir
backup_archive="$backup_type-$(date +%Y%m%d%H%M%S).tar.gz"
GZIP=-9
tar -zcf $backup_archive "$backup_dir"
# delete backup dir as its no longer required
rm -rf $backup_dir
cd ..
echo "New backup has been created at backups/$backup_archive"
echo
backup_name="manual-$(date +%Y%m%d%H%M%S)"
$docker_compose run --rm misago ./.run backup $backup_name
}

# Restore from backup
Expand Down Expand Up @@ -472,6 +451,8 @@ if [[ $1 ]]; then
docker_run $@
elif [[ $1 = "psql" ]]; then
run_psql
elif [[ $1 = "resetcron" ]]; then
set_crontab
else
invalid_argument $1
fi
Expand Down
40 changes: 35 additions & 5 deletions misago/.run
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,38 @@ run_psql() {
psql --username $POSTGRES_USER --host $POSTGRES_HOST $POSTGRES_USER
}

# Backup database
backup_db() {
# Create new backup
create_backup() {
if [[ ! $1 ]]; then
echo -e "Missing argument: ${RED}backup_name${NORMAL}"
echo
exit 1
fi
backup=$1
backup_tmp="/tmp/$backup"
backup_archive="$backup.tar.gz"
wait_for_db
echo "Initializing backup $backup"
mkdir $backup_tmp
echo "Preparing database..."
pg_dump -U $POSTGRES_USER -h $POSTGRES_HOST $POSTGRES_USER -Oxc > "$backup_tmp/database.sql"
echo "Preparing media files..."
cp -r /misago/media "$backup_tmp/media"
echo "Archiving..."
GZIP=-9
cd "/tmp/"
tar -zcf $backup_archive $backup
echo "Cleaning up..."
cd /misago/
mv "/tmp/$backup_archive" "/misago/backups/$backup_archive"
rm -rf $backup_tmp
echo "New backup: $backup.tar.gz"
}

# Dump database to backups for upgrade script
prepare_v1_upgrade() {
wait_for_db
pg_dump -U $POSTGRES_USER -h $POSTGRES_HOST $POSTGRES_USER -Oxc > "/misago/backups/$1/database.sql"
pg_dump -U $POSTGRES_USER -h $POSTGRES_HOST $POSTGRES_USER -Oxc > "/misago/backups/postgresql10.sql"
}

# Command dispatcher
Expand All @@ -68,8 +96,10 @@ if [[ $1 ]]; then
wait_for_db
elif [[ $1 = "psql" ]]; then
run_psql
elif [[ $1 = "backup_db" ]]; then
backup_db $2
elif [[ $1 = "backup" ]]; then
create_backup $2
elif [[ $1 = "prepare_v1_upgrade" ]]; then
prepare_v1_upgrade
else
invalid_argument $1
fi
Expand Down

0 comments on commit 18c3349

Please sign in to comment.