Skip to content

Commit 18c3349

Browse files
authored
Merge pull request #104 from rafalp/v0-prepare
V0 prepare
2 parents 3e6fd15 + a2e2b4e commit 18c3349

File tree

3 files changed

+70
-55
lines changed

3 files changed

+70
-55
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 0.1 (2023-05-30)
2+
3+
- Moved backup logic inside the `misago` container (#98).
4+
- Added `resetcron` command to `./appctl`.

appctl

+31-50
Original file line numberDiff line numberDiff line change
@@ -60,45 +60,46 @@ intro() {
6060
echo
6161
echo "Setup and upgrade:"
6262
echo
63-
echo " ${BOLD}setup${NORMAL} setup new Misago site."
64-
echo " ${BOLD}upgrade${NORMAL} backup, rebuild, migrate and collect static."
63+
echo " ${BOLD}setup${NORMAL} Setup new Misago site."
64+
echo " ${BOLD}upgrade${NORMAL} Backup, rebuild, migrate and collect static."
6565
echo
6666
echo "Docker management:"
6767
echo
68-
echo " ${BOLD}start${NORMAL} start all containers."
69-
echo " ${BOLD}stop${NORMAL} stop all containers."
70-
echo " ${BOLD}restart${NORMAL} stop and start all docker containers."
71-
echo " ${BOLD}rebuild${NORMAL} rebuild and restart Misago and celery container containers."
72-
echo " ${BOLD}rebuildall${NORMAL} pull, rebuild and restart all containers."
73-
echo " ${BOLD}stats${NORMAL} see list and stats of running docker containers."
68+
echo " ${BOLD}start${NORMAL} Start all containers."
69+
echo " ${BOLD}stop${NORMAL} Stop all containers."
70+
echo " ${BOLD}restart${NORMAL} Stop and start all docker containers."
71+
echo " ${BOLD}rebuild${NORMAL} Rebuild and restart Misago and celery container containers."
72+
echo " ${BOLD}rebuildall${NORMAL} Pull, rebuild and restart all containers."
73+
echo " ${BOLD}stats${NORMAL} See list and stats of running docker containers."
7474
echo
7575
echo "Change configuration:"
7676
echo
77-
echo " ${BOLD}forumindex${NORMAL} switch forum index between threads and categories."
78-
echo " ${BOLD}email${NORMAL} change email setup."
79-
echo " ${BOLD}hostname${NORMAL} change hostname setup."
80-
echo " ${BOLD}locale${NORMAL} change locale setup."
81-
echo " ${BOLD}timezone${NORMAL} change timezone setup."
82-
echo " ${BOLD}avatargallery${NORMAL} load avatar gallery."
83-
echo " ${BOLD}sentry${NORMAL} enable or disable Sentry (sentry.io) for logging."
84-
echo " ${BOLD}debug${NORMAL} change debug mode."
85-
echo " ${BOLD}secret${NORMAL} reset secret key."
77+
echo " ${BOLD}forumindex${NORMAL} Switch forum index between threads and categories."
78+
echo " ${BOLD}email${NORMAL} Change email setup."
79+
echo " ${BOLD}hostname${NORMAL} Change hostname setup."
80+
echo " ${BOLD}locale${NORMAL} Change locale setup."
81+
echo " ${BOLD}timezone${NORMAL} Change timezone setup."
82+
echo " ${BOLD}avatargallery${NORMAL} Load avatar gallery."
83+
echo " ${BOLD}sentry${NORMAL} Enable or disable Sentry (sentry.io) for logging."
84+
echo " ${BOLD}debug${NORMAL} Change debug mode."
85+
echo " ${BOLD}secret${NORMAL} Reset secret key."
8686
echo
8787
echo " Note: you need to rebuild Misago container for changes made with those utils to take effect."
8888
echo
8989
echo "Backup:"
9090
echo
91-
echo " ${BOLD}backup${NORMAL} backup and archive database and media."
92-
echo " ${BOLD}restore BACKUP${NORMAL} restore database and media from BACKUP archive."
91+
echo " ${BOLD}backup${NORMAL} Backup and archive database and media."
92+
echo " ${BOLD}restore BACKUP${NORMAL} Restore database and media from BACKUP archive."
9393
echo
9494
echo "Utilities:"
9595
echo
96-
echo " ${BOLD}collectstatic${NORMAL} collect static assets."
97-
echo " ${BOLD}manage.py${NORMAL} runs \"python manage.py\" inside docker."
98-
echo " ${BOLD}bash${NORMAL} starts bash session inside running Misago container."
99-
echo " ${BOLD}run${NORMAL} runs \"docker-compose run --rm misago\"."
100-
echo " ${BOLD}psql${NORMAL} runs psql connected to database."
101-
echo " ${BOLD}flushredis${NORMAL} flush the redis cache."
96+
echo " ${BOLD}collectstatic${NORMAL} Collect static assets."
97+
echo " ${BOLD}manage.py${NORMAL} Runs \"python manage.py\" inside docker."
98+
echo " ${BOLD}bash${NORMAL} Starts bash session inside running Misago container."
99+
echo " ${BOLD}run${NORMAL} Runs \"docker-compose run --rm misago\"."
100+
echo " ${BOLD}psql${NORMAL} Runs psql connected to database."
101+
echo " ${BOLD}flushredis${NORMAL} Flush the redis cache."
102+
echo " ${BOLD}resetcron${NORMAL} Replace host's crontab with default one."
102103
echo
103104
}
104105

@@ -159,7 +160,7 @@ set_crontab() {
159160
rm -f cron_tmp
160161
touch cron_tmp
161162
echo "30 1 * * * cd $current_path && $docker_compose run --rm misago ./cron" >> cron_tmp
162-
echo "0 1 * * * cd $current_path && ./appctl daily_backup" >> cron_tmp
163+
echo "0 1 * * * cd $current_path && $docker_compose run --rm misago ./.run backup \"auto-\$(date +%Y%m%d%H%M%S)\"" >> cron_tmp
163164
echo "0 1 * * * find $current_path/backups/ -name auto-* -type f -mtime +10 -delete" >> cron_tmp
164165
echo "" >> cron_tmp
165166
crontab cron_tmp
@@ -320,30 +321,8 @@ reset_secret_key() {
320321
# Create new backup
321322
create_new_backup() {
322323
require_setup
323-
if [[ $1 ]]; then
324-
backup_type="$1"
325-
else
326-
backup_type="manual"
327-
fi
328-
# create tmp backup dir
329-
backup_dir="$backup_type-$(date +%Y%m%d%H%M%S)"
330-
mkdir "./backups/$backup_dir"
331-
# backup database
332-
echo "Backing up database..."
333-
$docker_compose run --rm misago ./.run backup_db $backup_dir
334-
# backup media
335-
echo "Backing up media..."
336-
cp -r ./misago/media "./backups/$backup_dir/media"
337-
cd ./backups/
338-
# archive backup dir
339-
backup_archive="$backup_type-$(date +%Y%m%d%H%M%S).tar.gz"
340-
GZIP=-9
341-
tar -zcf $backup_archive "$backup_dir"
342-
# delete backup dir as its no longer required
343-
rm -rf $backup_dir
344-
cd ..
345-
echo "New backup has been created at backups/$backup_archive"
346-
echo
324+
backup_name="manual-$(date +%Y%m%d%H%M%S)"
325+
$docker_compose run --rm misago ./.run backup $backup_name
347326
}
348327

349328
# Restore from backup
@@ -472,6 +451,8 @@ if [[ $1 ]]; then
472451
docker_run $@
473452
elif [[ $1 = "psql" ]]; then
474453
run_psql
454+
elif [[ $1 = "resetcron" ]]; then
455+
set_crontab
475456
else
476457
invalid_argument $1
477458
fi

misago/.run

+35-5
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,38 @@ run_psql() {
5454
psql --username $POSTGRES_USER --host $POSTGRES_HOST $POSTGRES_USER
5555
}
5656

57-
# Backup database
58-
backup_db() {
57+
# Create new backup
58+
create_backup() {
59+
if [[ ! $1 ]]; then
60+
echo -e "Missing argument: ${RED}backup_name${NORMAL}"
61+
echo
62+
exit 1
63+
fi
64+
backup=$1
65+
backup_tmp="/tmp/$backup"
66+
backup_archive="$backup.tar.gz"
67+
wait_for_db
68+
echo "Initializing backup $backup"
69+
mkdir $backup_tmp
70+
echo "Preparing database..."
71+
pg_dump -U $POSTGRES_USER -h $POSTGRES_HOST $POSTGRES_USER -Oxc > "$backup_tmp/database.sql"
72+
echo "Preparing media files..."
73+
cp -r /misago/media "$backup_tmp/media"
74+
echo "Archiving..."
75+
GZIP=-9
76+
cd "/tmp/"
77+
tar -zcf $backup_archive $backup
78+
echo "Cleaning up..."
79+
cd /misago/
80+
mv "/tmp/$backup_archive" "/misago/backups/$backup_archive"
81+
rm -rf $backup_tmp
82+
echo "New backup: $backup.tar.gz"
83+
}
84+
85+
# Dump database to backups for upgrade script
86+
prepare_v1_upgrade() {
5987
wait_for_db
60-
pg_dump -U $POSTGRES_USER -h $POSTGRES_HOST $POSTGRES_USER -Oxc > "/misago/backups/$1/database.sql"
88+
pg_dump -U $POSTGRES_USER -h $POSTGRES_HOST $POSTGRES_USER -Oxc > "/misago/backups/postgresql10.sql"
6189
}
6290

6391
# Command dispatcher
@@ -68,8 +96,10 @@ if [[ $1 ]]; then
6896
wait_for_db
6997
elif [[ $1 = "psql" ]]; then
7098
run_psql
71-
elif [[ $1 = "backup_db" ]]; then
72-
backup_db $2
99+
elif [[ $1 = "backup" ]]; then
100+
create_backup $2
101+
elif [[ $1 = "prepare_v1_upgrade" ]]; then
102+
prepare_v1_upgrade
73103
else
74104
invalid_argument $1
75105
fi

0 commit comments

Comments
 (0)