Is there any way to restore your site from backup in the terminal?

I did a backup of my site before I upgraded to the latest version of the discourse however I can’t even access my site any more even though the upgrade appears to be successful as no error is displayed. Is there anyway that I can restore my site from the backup that I created earlier from terminal app as I can’t access the site?

  1. Is it possible?

  2. If yes, where do discourse save or store the backups that I executed from the admin panel?

  3. Is there any guide on how to restore the site from backup?

Thanks

Yes.

They’re stored in public/backups/default/.

Just run the following command (in the discourse directory)

script/discourse restore <filename.of.the.backup.tar.gz>

Thank you. :slight_smile:

This seems like a handy thing to be able to do since if you screw up configuring SSO you can’t get back into your site. I spent a week one day trying to get SSO configured, and now that it is configured correctly it seems that my account on the SSO (which I don’t control) is gone, so I can’t get in.

So, script/discourse restore fails because thor isn’t installed.

gem install thor fixes that, but then I’m still denied because:

URGENT: FATAL:  Peer authentication failed for user "discourse"

Solving my immediate problem, I suppose I could turn off SSO from the Rails console. . .

edit: to disable SSO from the rails console:

cd /var/discourse
./launcher enter app
rails c
SiteSetting.enable_sso=false
exit
exit

There’s a way to log in: Visit /users/admin-login, which can be used to log in via email :slight_smile:

It worked for me by first getting into the app

./launcher enter app

then

discourse enable_restore
discourse restore <filename.of.the.backup.tar.gz>
discourse disable_restore

Using the script/discourse didn’t work

restoring from a app.yml and an external tar.gz file was mixing up some of the items I found along this thread:

TARBALL_PATH=$(ls local/backups/-*.tar.gz | tail -n 1)
TARBALL_NAME=$(basename ${TARBALL_PATH})
cat ${TARBALL_PATH} | docker exec -i app sh -c "cat - > /var/www/discourse/public/backups/default/${TARBALL_NAME}"
docker exec -i app sh -x << EOF
discourse enable_restore
discourse restore ${TARBALL_NAME}
discourse disable_restore
EOF

I had several pitfalls:

  1. the tarball to restore has to be placed exactly in /var/www/discourse/public/backups/default/
  2. the /var/www/discourse/script/discourse restore tarball-name did not work due to some single sign-on known issue
  3. the version of thor installed by gem install thor was too recent and then incompatible with the rest.
  4. finally one of the tarball I tried to restore from did not contain the “meta.json” file…

Hope this helps other in their restore journey.

Rather than try to restore from app.yml it’s much easier to just ./launcher enter app and restore it from the command line.

The goal was to automate it without any entering container with the launcher.
Imagine your instance got lost for any reason. The only files you stored in git was your containers/app.yml and luckily you have a daily tarball backup.

You can also do it directly to Postgres:

dropdb dbname
createdb dbname
gunzip -c filename.gz | psql dbname

discourse is script/discourse – executed via bundle exec and with rails_env=production. Look at /usr/local/bin/discourse.

Running bundle exec script/discourse takes care of the thor mismatched gem version, too. Or just using the discourse convenience script.

لقد جربت هذا، وبالنسبة للسطر التالي:

discourse restore <filename.of.the.backup.tar.gz>

استخدمت:

discourse restore root@droplet-01:/test/MyBackup.tar.gz

لم أكن متأكدًا مما إذا كانت هذه هي الطريقة الصحيحة للوصول إلى ملف “خارج التطبيق من داخل التطبيق”، لكن يبدو أنه عثر على ملف النسخ الاحتياطي وبدأ عملية الاستعادة. انتهت عملية الاستعادة برسائل غامضة:

تم الانتهاء!
[فشل]
تمت عملية الاستعادة.

حاولت الوصول إلى المنتدى مرة أخرى، لكنه لا يزال لا يعمل، لذا أعتقد أن عملية الاستعادة لم تنجح؟

لا يمكنك ذلك. تحتاج إلى نسخ ملف النسخة الاحتياطية إلى الدليل الصحيح. يرجى اتباع التعليمات في Restore a backup from the command line إذا كنت ترغب في الاستعادة من سطر الأوامر.

إليك البرنامج النصي العامل الذي أستخدمه لاستعادة الإنتاج إلى التطوير والاختبار:

#!/bin/sh
set +x
set -e
# This script restore the latest productive backup to the test/dev environment
CONTAINER_NAME=app-test
LATEST_BACKUP=$(mc ls s3/backup-prod/default | tail -n 1 | cut -d ' ' -f 5)
mc cp s3/backup-prod/default/${LATEST_BACKUP} /tmp
# ensure /var/www/discourse/public/backups/default/ exists with the proper ownership
docker exec -i ${CONTAINER_NAME} sh -c "mkdir -p /var/www/discourse/public/backups/default/ && chown -R discourse:www-data /var/www/discourse/public/backups/default/"
cat /tmp/${LATEST_BACKUP} | docker exec -i ${CONTAINER_NAME} sh -c "cat - > /var/www/discourse/public/backups/default/${LATEST_BACKUP}"
docker exec -i ${CONTAINER_NAME} sh -x << EOF
discourse enable_restore
rails runner "SiteSetting.set('backup_location', 'local')"
discourse restore ${LATEST_BACKUP}
discourse disable_restore
rm -f /var/www/discourse/public/backups/default/${LATEST_BACKUP}
EOF
# rebuild container
cd /var/lib/discourse/discourse_docker
stdbuf -oL -eL ./launcher rebuild ${CONTAINER_NAME} 2>&1 | sed 's/DISCOURSE_google_oauth2_client_secret=[^ ]*/DISCOURSE_google_oauth2_client_secret=***REDACTED***/g'
cd -
rm -f /tmp/${LATEST_BACKUP}

كان rails runner \"SiteSetting.set('backup_location', 'local')\" مفقودًا وكان يمنع الاستعادة من ملف النسخ الاحتياطي المضغوط.

لاحظ أنه كان يجب حذف مخرجات البرنامج النصي للمشغل لأنها ستكشف الأسرار في مخرجاتها، خاصة عند إجرائها عبر وظيفة CI/CD مرئية.

يسعدني أن لديك حلاً. إليك بعض الأشياء التي ستجعل الأمر أسهل لشخص آخر.

إذا استخدمت DISCOURSE_ALLOW_RESTORE: 'true' في ملف app.yml الخاص بك، يمكنك تخطي تمكين الاستعادة. (يمكنك بالمثل وضع مصادقة Google الخاصة بك في متغيرات البيئة (ENV) وإبقائها خارج قاعدة البيانات تمامًا.)

إذا كان لديك كل من بيئة الاختبار والإنتاج يستخدمان نفس مستودع S3، يمكنك استعادة أحدث نسخة احتياطية باستخدام

docker exec ${CONTAINER_NAME} bash -c '$(discourse restore|grep gz|head -1)'

إذا كنت بحاجة إلى قراءتها محليًا، يمكنك بالمثل تجاوز إعدادات الموقع باستخدام متغير بيئة (ENV) وسيقرأ أحدث نسخة احتياطية من المخزن المحلي.