Back to Overview

Bricksync 24/7: Autostart & Failover

In this advanced tutorial, we replace the simple screen method with a real system service (Systemd) combined with tmux. Bricksync will start completely automatically after a server reboot and recover from crashes on its own.

Community Contribution

The idea and base scripts for this setup come directly from the community. A huge thank you for this brilliant optimization goes to:
Sabine Schmidt (Discord: BieneBiene#CentBricks)
Feel free to support her and visit her store: CentBricks on BrickLink

1 Preparation (Install tmux)

This tutorial builds directly on our Basic Installation Guide. We assume that Bricksync is already located in the directory of the bricksync user.

First, we need to install the program tmux on the server. Log into PuTTY as root and execute the following command:

sudo apt update && sudo apt install tmux -y

2 Create the Startup Script

Now switch to the user bricksync. We'll create a script that neatly opens the tmux session in the background.

su - bricksync
nano /home/bricksync/start_bricksync.sh

Paste the following code into the editor, save with CTRL + O (then Enter), and exit with CTRL + X:

#!/bin/bash
# ==============================================================================
# Script: start_bricksync.sh
# Description: Starts Bricksync in a detached tmux session.
#              Checks if a session already exists to avoid duplicate
#              execution.
# ==============================================================================

SESSION="bricksync"
APP="/home/bricksync/bricksync-linux64/bricksync"

if ! tmux has-session -t "$SESSION" 2>/dev/null; then
    tmux new-session -d -s "$SESSION" "$APP"
    echo "Bricksync started in tmux session '$SESSION'."
else
    echo "The tmux session '$SESSION' is already running."
fi

Next, make the script executable:

chmod +x /home/bricksync/start_bricksync.sh

3 Set up the Systemd Service

So the server knows to treat this script like a system service, we'll create a service file. Note: You must be logged in as root for this step.

exit
nano /etc/systemd/system/bricksync.service

Paste this block into the file and save again with CTRL + O and CTRL + X:

[Unit]
Description=Bricksync 24/7 Service (tmux)
After=network.target

[Service]
Type=forking
User=bricksync
Group=bricksync
WorkingDirectory=/home/bricksync/bricksync-linux64
ExecStart=/home/bricksync/start_bricksync.sh
ExecStop=/usr/bin/tmux kill-session -t bricksync
Restart=always
RestartSec=5
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Environment=HOME=/home/bricksync

[Install]
WantedBy=multi-user.target

Tell the system that there are new services and enable the autostart:

sudo systemctl daemon-reload
sudo systemctl enable bricksync.service
sudo systemctl start bricksync.service

4 Usage & tmux Control

Bricksync is now running safely in the background. If you want to enter commands (such as blmaster), you need to attach to the running tmux session. Log in as user bricksync to do this.

su - bricksync
tmux attach -t bricksync
Very Important When Exiting:
When you are done, you must not simply close the window with the X or press CTRL+C!
To keep the session running in the background, press sequentially:

CTRL + B (release both) and then D (for "detach").

5 Save Local Backup

Before we automatically delete old files on the server in the next step, you should back them up locally on your computer. This ensures you have all order files (.bsx) and backups at hand if you ever need to investigate discrepancies ("Lost Parts") in your shop.

For Mac / Linux Users:
Open the terminal on your local computer (not the server) and use this command to pull the entire data folder to your desktop:

rsync -avz -e ssh root@<YOUR_SERVER_IP>:/home/bricksync/bricksync-linux64/data/ ~/Desktop/Bricksync-Backup/

(Replace <YOUR_SERVER_IP> with your actual server IP address.)

For Windows Users:
If you are not using Windows Subsystem for Linux (WSL), simply download the free software WinSCP or FileZilla. Connect with your server credentials and simply drag and drop the backups and orders folders to your desktop.

6 Bonus: Automated Cleanup

To prevent Bricksync backups and order files from filling up your server's hard drive over the months, you can use this cleanup script.

nano /home/bricksync/cleanup_bricksync.sh
#!/bin/bash
BASE_DIR="/home/bricksync/bricksync-linux64/data"
BACKUPS_DIR="$BASE_DIR/backups"
ORDERS_DIR="$BASE_DIR/orders"

# Delete files/folders older than 7 days.
DAYS_TO_KEEP=7

echo "=== Bricksync Cleanup started: $(date) ==="

# 1. Delete old backup folders
find "$BACKUPS_DIR" -mindepth 1 -maxdepth 1 -type d -mtime +$DAYS_TO_KEEP -exec rm -rf {} \;

# 2. Delete old order files (.bsx)
find "$ORDERS_DIR" -type f -name "*.bsx" -mtime +$DAYS_TO_KEEP -exec rm -f {} \;

echo "Disk space after cleanup:"
df -h "$BASE_DIR"
echo "=== Bricksync Cleanup finished ==="

Make script executable and test it:

chmod +x /home/bricksync/cleanup_bricksync.sh
./cleanup_bricksync.sh

Tip: You can run this script manually after you have pulled your local backup from step 5. Alternatively, you can put it in a cronjob if you automatically secure your backup locally every week anyway.