I recently replaced Notion entirely with a self-hosted AFFiNE instance. It’s become my daily driver for notes, and over time, it’s accumulated data I absolutely cannot afford to lose. This led me down the rabbit hole of building a backup system that actually works.

Here’s what I built and how you can use it too.

The Problem Link to heading

I needed a solution that:

  • Automatically backs up my data every day
  • Stores backups in the cloud
  • Encrypts everything end-to-end
  • Can restore my entire AFFiNE setup in minutes

The Solution: rclone + restic Link to heading

I settled on two tools: rclone for cloud provider abstraction, and restic for incremental backups. Together, they’re powerful, reliable, and surprisingly straightforward to set up.

Part 1: Setting Up Google Drive with rclone Link to heading

Create a Google Cloud Project Link to heading

rclone needs permission to access your Google Drive. Here’s how to grant it:

  1. Head to the Google Cloud Console and create a new project
  2. Search for “Google Drive API” and enable it
  3. Go to the Credentials tab and configure the OAuth consent screen as “External”
  4. Add these scopes manually:
    https://www.googleapis.com/auth/docs
    https://www.googleapis.com/auth/drive
    https://www.googleapis.com/auth/drive.metadata.readonly
    
  5. Add yourself as a test user
  6. Create an OAuth 2.0 credential of type “Desktop Application”
  7. Save your client ID and secret
  8. Publish your app (it only needs to be available to you)

Configure rclone Link to heading

Now install rclone and run the interactive setup:

$ rclone config

Select “New remote” and follow these prompts:

  • Name: gdrive (or whatever you want to call it)
  • Storage type: drive (Google Drive)
  • Client ID/Secret: Paste the values from Google Cloud Console
  • Scope: Choose based on your needs
  • Web browser authentication: Answer n if you’re on a headless server, then follow the link on a machine with a GUI to get your auth token

Once done, you’ll have a configured remote ready to use.

Security Aspects Link to heading

By default, rclone stores your config in plain text at ~/.config/rclone/rclone.conf. For sensitive data, encrypt it:

$ rclone config
# Select: s) Set configuration password

Save that rclone password somewhere safe - you’ll need it to restore.

For extra security, create a second remote of type crypt that layers encryption on top of your Google Drive remote. This gives you end-to-end encryption in the cloud.

Part 2: Setting Up restic Link to heading

Initialize a restic Repository Link to heading

restic supports rclone as a backend natively. Set these environment variables:

export RESTIC_REPOSITORY="rclone:gdrive:/"
export RESTIC_PASSWORD="<your-restic-password>"
export RCLONE_CONFIG_PASS="<your-rclone-password>"

Then initialize the repository:

restic init

Save that restic password somewhere safe - you’ll need it to restore.

Take Your First Backup Link to heading

restic backup /path/to/your/affine/data

restic uses incremental backups, so it only stores what’s changed since the last backup. This saves a huge amount of space.

Part 3: Automating Everything Link to heading

Manual backups are great until you forget to run them. I automated mine with a bash script, a systemd service, and a systemd timer.

The script:

  1. Copies the data I want to back up to a temporary directory
  2. Runs restic to create an incremental backup
  3. Cleans up temporary files
  4. Respects a retention policy (e.g., keep daily backups for 30 days, then switch to weekly, etc.)

All configuration comes from a .env file, so it’s easy to customize.

You can find the full systemd configuration in this GitHub repository.

Part 4: The Recovery Plan Link to heading

Here’s where it all pays off. If your server goes down, you can restore everything in minutes.

Step 1: Restore your rclone config Link to heading

I keep my rclone configuration (encrypted) in Bitwarden along with passwords and step-by-step instructions. Copy it to ~/.config/rclone/rclone.conf:

cat > ~/.config/rclone/rclone.conf << 'EOF'
# Paste your encrypted config here
EOF

Step 2: Restore files from restic Link to heading

export RCLONE_CONFIG_PASS="<rclone-password>"
export RESTIC_REPOSITORY="rclone:gdrive:/"
export RESTIC_PASSWORD="<restic-password>"

# Verify your backups exist
restic snapshots

# Restore the latest backup
cd /tmp
restic restore latest --target=.

Step 3: Restore your AFFiNE database and files Link to heading

# Start only the database service first
docker compose create
docker compose start postgres

# Copy the database backup into the container
docker cp ./affine.backup affine_postgres:/affine.backup

# Restore it
docker exec affine_postgres pg_restore \
  --format c \
  --dbname affine \
  --username affine \
  -v /affine.backup

# Restore your data blobs and config
mkdir -p ./storage
cp /tmp/storage/blobs ./storage/

mkdir -p ./config
cp /tmp/config/private.key ./config/

# Start everything
docker compose up -d

You’re back online.

Lessons Learned Link to heading

Self-hosting means you own your data and you’re responsible for keeping it safe. I switched to AFFiNE specifically for privacy. But privacy only matters if your backups are actually secure. End-to-end encryption is non-negotiable: your cloud backups need to be encrypted so that only you can access them.

After months of notes and work accumulated in AFFiNE, losing everything would be catastrophic. So I built a backup system. But here’s what matters: you have to test it. Actually restore your data. Verify nothing is missing. Check that you have all the passwords and configuration files you need.

One thing I’m adding: multiple restic repositories. Storing backups in one place means one point of failure. Spreading them across different cloud providers gives you redundancy. If one repository breaks, you still have others.

Self-hosting requires discipline. But with proper backups and regular testing, you get something centralized services can’t offer: complete control and certainty that your data is yours and recoverable.