Package Git into Ghost Docker Image
Table of Contents
In Ghost on Fly.io for Free, we mentioned that the root environment of fly.io is temporary and will be restored when the app is redeployed or restarted, which was confirmed by the fly.io forum. This article shows how to wrap the Git environment into Docker to avoid the problem of Git environments being destroyed.
In fact, there are many ways to backup Ghost, besides the one we have described in the past using SFTP, there are some others , but these methods are really slow in transferring and backing up the full amount each time, compared to using Git, which is more elegant because it uses incremental backups and is extremely fast at copying! It's a little tedious to set up a Git environment, but it's a one-off.
1. Prepare the deployment files
We need to deploy a local Dockerfile instead of letting fly.io pull the official image automatically.
1.Go to Github and download the official image. Download both of two files:
-
docker-entrypoint.sh
-
Dockerfile
2.Open a terminal and run
mkdir blog
3.Copy the above two files to the blog
folder, which is located under /Users/<ComputerUserName>/blog
, your user folder.
4.Run
ssh-keygen -t rsa -C "<GithubEmail>"
Press enter continuously. Follow the prompts to see where the key is saved. Here is my example.
ssh-keygen -t rsa -C "<GithubEmail>"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/<ComputerUserName>/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/<ComputerUserName>/.ssh/id_rsa
Your public key has been saved in /Users/<ComputerUserName>/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:siVmLMku/3BAem2ruRt44dEgdQsUBrKSAztU6HT1FIs k<GithubEmail>
The key's randomart image is:
+---[RSA 3072]----+
|o.++Bo.o. |
|oB + o+.. |
|X o oE.o |
|.+ = * |
| . O X S |
| = O * |
| o * + |
| + * |
| *+. |
+----[SHA256]-----+
Go to the file /Users/<ComputerUserName>/.ssh/
and copy the id_rsa and id_rsa.pub files to the blog folder.
Open id_rsa.pub with a text editor and add its contents to Github's SSH keys.
5.Continue to run
nano auto_backup.sh
Then, put the following code
#! /bin/sh
cd /var/lib/ghost/content # Switch to the directory
git config --global init.defaultBranch master
git config --global --add safe.directory /var/lib/ghost/content
git config --global pull.rebase false
git pull # Pull the repository
git add . # Add all
git commit -m "auto backup" # Commit
git push --force # Force a commit
Add to it. Press CTRL
+O
to save; press CTRL
+X
to exit the nano editor.
6.Open the Dockerfile and edit a few places. I recommend opening the blog folder with vs code.
Find COPY docker-entrypoint.sh /usr/local/bin
, then, above it, paste the following code.
COPY id_rsa /root/.ssh/
COPY id_rsa.pub /root/.ssh/
COPY auto_backup.sh /usr/local/bin/auto_backup.sh
Find ENV GHOST_VERSION
, then add the following code to the next line.
RUN apt-get update -y && apt-get upgrade -y && apt-get install -y git nano
7.Refer to previous post to prepare the fly.toml file. Note to remove
[build]
image = "ghost:5"
8.This way, several local files that we use for deployment are ready, they are
- auto_backup.sh
- docker-entrypoint.sh
- Dockerfile
- fly.toml
- id_rsa
- id_rsa.pub
2. Deployment
Login:
flyctl auth login --email=<FlyMail> --password=<FlyPassword> --otp=<any letter>
Launch an app:
flyctl launch --image=Decokerfile --name=<AppName> -r hkg --no-deploy
Follow the prompts and press y to continue.
Create a volume:
flyctl volumes create data -r hkg --no-encryption --size 1
Deploy.
flyctl deploy
NOTE: If you have installed Ghost in the standard way, you should simply deploy.
3.Initialize Ghost
Refer to previous post.
4. Backup
1.Verify the github connection
ssh git@github.com
Get the successfully
prompt, that's it.
2.Initialize Git, run
cd /var/lib/ghost/content
git init
git config --global user.name "<GithubUsername>"
git config --global user.email <GithubMail>
git config --global --add safe.directory /var/lib/ghost/content
git remote add origin git@github.com:<GithubUsername>/<YourRepo>.git
git add .
git commit -m "auto backup"
git push -u origin master --force
Check for changes to the contents of the Github repository.
If there is an error, then run
cd /var/lib/ghost/
rm -rf content
It will prompt its BUSY, don't worry about it, it's already cleared.
After that, try to initialize Git again.
5.Restore
Assuming you have a fresh install of Ghost, you need to restore an existing backup in Github.
Unlike a backup, we need to pull rather than push the contents from Github.
rm -rf /var/lib/ghost/content
cd /var/lib/ghost/content
git init
git config --global user.name "<GithubUsername>"
git config --global user.email <GithubMail>
git config --global --add safe.directory /var/lib/ghost/content
git pull --force git@github.com:<GithubUsername>/<YourRepo>.git
If the repository is brand new, i.e. you copied your files directly into it but haven't connected to the remote repository yet, you may be prompted to run
git push --set-upstream origin master
Just execute the action.
6.Daily back up
First, give the automatic backup script execution privileges to, in ssh console run
chmod a+x /usr/local/bin/auto_backup.sh
Then, in local machine, run the following command
flyctl auth login --email=<FlyMail> --password=<FlyPassword> --otp=<any letter>
flyctl ssh console
sh /usr/local/bin/auto_backup.sh
You can write a script to automatically log into console and run it from local machine terminal
nano fly.sh
Paste the following code into it
cd blog
flyctl auth login --email=<FlyMail> --password=<FlyPassword> --otp=<any letter>
flyctl ssh console
Save and exit, then run
chmod a+x fly.sh
Put it into an aspect, like the desktop. To use it, drag it to the terminal, press enter, and it will go directly to the console.