Backup your space to Github

Personally I use a private Github repository for backing up my space. This is how I set this up.

Requirements

  • A (free) Github account
  • A private Github repository to keep your space files

Create a token

We will authenticate with Github via HTTPS and a personal access token. This is relatively safe and simple to setup.

Go to your Personal Access Tokens page, and Generate a new token (beta) scoped to your repository.

  • Select a expiry date far in the future
  • Select: “Only selected repositories” and select your private space repostiory.
  • For permissions choose:
    • Contents: Read & Write

Then generate the token. Save the token some place safe, you will need it later in a bit.

Get a shell

If you’re running SilverBullet directly on your machine using Deno, you can just cd into your space folder and use git from there (if it is installed, if not, install git first).

If you’re running inside of a docker container (or fly.io container) you need to get a shell into your container.

Using docker you can do this with docker exec. For instance, if your container is named silverbullet you can run:

docker exec -it silverbullet /bin/bash

If you’re deployed to fly.io, you can get a shell via

fly ssh console

Configure the git repository

In your shell:

# Change to the space folder, in docker this is always /space
cd /space
# Initialize it as a git repo
git init
# Configure your name and email
git config user.name "Your Name"
git config user.email "[email protected]"
# Set rebasing to false (or true if you prefer)
git config pull.rebase false
# Add the Github repository you created as a remote, replace GITHUBTOKEN with the token you received earlier
git remote add origin https://[email protected]/username/repo.git

Empty repository

If you created a fresh Github repository with no content yet, you can do the following:

# Set the branch to main
git branch -m main
# Add all existing files
git add *.md
# Commit the first version
git commit -m "Initial commit"
# Push the first version
git push origin main

Existing content

If your repository already has content, first clean out the auto generated files (index.md and SETTING.md), then checkout the remote branch:

# Fetch all branches from the remote repostiroy
git fetch
# Clean out the auto generated pages
rm index.md SETTINGS.md
# Checkout your main branch from the repository
git checkout main

Setting up the Git plug

Open up SilverBullet and run the Plugs: Add command and enter github:silverbulletmd/silverbullet-git/git.plug.js.

This will give you some useful Git: * commands. Checkout the silverbullet-git README for more information, I specifically recommend you set up auto sync, to make backups automatic.

Enjoy!

4 Likes

I deployed SilverBullet to fly.io then I used the git plug to backup my space to Github. The problem is that I can sync it manually but the autoSync and autoCommit feature seem not working. Is that a limitation of fly.io or did I do something wrong?

I think it works only if you keep your fly machine active. Fly stops machines after a few minutes of inactivity, and then the auto commit stuff also won’t run.

1 Like

I worked around it by using space-script to call git.syncCommand whenever a page is saved

1 Like

I also assume it’s a good idea to include a .gitignore with the below contents. @zef, could you confirm this is correct and if any others are suggested?

.silverbullet.db*
_plug/
Library/

I noticed Library/Personal/* is recommended for storing templates/library edits, so this should be included in git, correct?

1 Like

Yes, this is roughly the .gitignore I use as well, except that I just commit the Library folder as well because… why not :slight_smile: Excluding .silverbullet.db* and _plug is indeed a good idea.

1 Like

I have tried to follow this to the letter but keep getting this everytime I try the first commit

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.