forge_backup
Backs up user home folders and MySQL databases to an S3 bucket, encrypted with age before they leave the server.
Install
Run this in your project folder. cargo add forge_backup
Forge Backup
forge_backup is a backup application for Linux servers. I developed this for Laravel Forge servers, however it will work with any linux server. It backs up what each user's sites need to be restored (each site's .env and storage/) to an AWS S3 bucket and, with --db, dumps the server's MySQL databases there too. It emails a report of any failures (and of successes, by configuration), through Resend or Mailgun.
With age_recipients in the config, everything is encrypted with age on the server before it is uploaded: the archive or dump is streamed through the encryption into aws s3 cp -, and no unencrypted copy is written to disk. The server holds only public keys, so it can make backups but cannot read them.
Requirements
- Rust 1.74 or newer
libssl-dev(on Debian-based systems) oropenssl-devel(on Amazon Linux)zipanddu(home folders)mysqlandmysqldump(--db)aws-cli- Configured AWS CLI with access keys (
aws configure)
Rust toolchain
To install the rust toolchain, run the following command
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Installation
To install forge_backup, ensure you have the Rust toolchain installed, then run:
cargo install forge_backup
Usage
To use forge_backup, run the following command:
forge_backup [OPTIONS]
Options
- --temp-folder <TEMP_FOLDER>: Specify where to create the temp archives.
- --s3-bucket <S3_BUCKET>: Specify the S3 bucket for backups.
- --s3-folder <S3_FOLDER>: Specify the folder in the S3 bucket.
- --home-dir <HOME_DIR>: Specify the home directory to back up (default: /home).
- --notify-success <NOTIFY_SUCCESS>: Notify on success (possible values: true, false).
- --alert-email <ALERT_EMAIL>: Email to alert in case of failure.
- --exclude-users <EXCLUDE_USERS>: Users to exclude from backup.
- --hostname : Specify the hostname.
- --db: Dump the databases instead of backing up the home folders.
- -h, --help: Print help information.
- -V, --version: Print version information.
Example
forge_backup --s3-bucket my-backup-bucket --s3-folder daily-backups --notify-success true --alert-email [email protected]
forge_backup --db
The exit status is 0 when everything was backed up, 1 when anything failed, and 2 when the config file is there but can't be read (nothing is backed up then). A failure is always emailed; a success only with notify_on_success.
What is kept
From each home folder, only what a restore needs: for every site folder in it (a folder holding a .env or a storage/), that .env and that storage/. exclude_files still applies inside storage/.
Nothing else in the home is kept: not the deployed code (releases/, current), which comes back from git when the site is deployed again; not auth.json; and not keys, shell history or tool caches (.ssh, .aws, .bash_history, .cache, .bun and so on). What is kept is named rather than what is left out, so a new key or cache folder in a home does not find its way into the backups. A home with no site in it is reported as a failure rather than archived empty.
Configuration
You can create a configuration file at ~/.config/forge-backup/forge_backup.toml. The command-line parameters will override the config file values. Following is a template for the config file.
exclude_users = []
exclude_files = [
"**/ai1m-backups/*",
"**/node_modules/*",
"**/.cache/*",
"**/.npm/*",
"**/vendor/*",
]
temp_folder = "~/tmp/backups"
home_dir = "/home"
hostname = "{$HOSTNAME}"
s3_bucket = ""
s3_folder = "daily"
alert_email = ""
notify_on_success = false
# Either a Resend sending key for the sender's domain (and the mailgun_
# settings are ignored), or the three Mailgun settings.
resend_api_key = ""
mailgun_api_base = ""
mailgun_api_key = ""
mailgun_domain = ""
sender_email = ""
# Encryption: age public keys (age1…), one or more. See "Encryption" below.
# age_recipients = ["age1…", "age1…", "age1…"]
# --db only.
# databases = ["asg_phoneus"] # empty or absent: every database but MySQL's own
# db_defaults_file = "/etc/mysql/debian.cnf" # the login mysql and mysqldump use
# db_env_file = "/home/alpha/example.com/.env" # or: log in as this Laravel site does (1.5.0)
# db_keep_days = 10 # days of dumps kept in the bucket, today included
The database login
--db logs in to MySQL with an options file. Without one named, it uses
/etc/mysql/debian.cnf, which Ubuntu's own MySQL packages leave for root.
MySQL Community from Oracle's packages leaves none, and root may not be able to
log in at all. There, set db_env_file to a Laravel site's .env: the dumps
log in with its DB_USERNAME, DB_PASSWORD, DB_HOST and DB_PORT, which can
always read the site's own database and follow it when its password changes.
The login is written to an options file only root can read, in temp_folder,
for as long as the dumps run, and removed after. A password is never put on a
command line. db_defaults_file, when set, is used instead.
Environment Variables
Environment variables can be used in the config file like this:
hostname = "{$HOSTNAME}"
Encryption
age_recipients decides whether backups are encrypted:
- Absent: home folder archives are uploaded unencrypted, as before 1.4.0, and every report says
WARNING: backups are not encrypted.--dbrefuses to run: database dumps are never uploaded unencrypted. - Present: every archive and dump is encrypted to all of the listed keys; any one of the matching private keys decrypts it. If the list is empty, or any entry is not a valid
age1…public key, nothing is uploaded, the error is emailed and the exit status is 1. A private key (AGE-SECRET-KEY-…) in the list is refused the same way.
There is no setting for a private key: decryption never happens on the server.
What goes where
| What | Key in the bucket |
|---|---|
| Home folder (encrypted) | <s3_folder>/<hostname>/YYYY-MMDD/<user>_backup_YYYY-MMDD.zip.age |
| Home folder (unencrypted) | <s3_folder>/<hostname>/YYYY-MMDD/<user>_backup_YYYY-MMDD.zip |
Database dump (--db) |
<s3_folder>/<hostname>/db/YYYY-MMDD/<database>_HHMMSS.sql.gz.age |
Home folder archives are not pruned by forge_backup; use a lifecycle rule on the bucket. Database dumps older than db_keep_days are deleted by --db itself, after a run in which every database dumped successfully.
How a run fails safely
The archive (zip -r - <folder>) or dump (mysqldump … | gzip) is encrypted as it streams into aws s3 cp - <key>. The upload is only allowed to finish once the archive or dump command has exited successfully (zip's exit 12 and 18, files that vanished or could not be read, are accepted and listed in the report, as before) and the encryption is finished. On any failure the aws process is killed before it sees the end of the stream, so it never completes the object, and forge_backup then tries to abort any unfinished multipart upload of that key. That needs s3:ListBucketMultipartUploads and s3:AbortMultipartUpload; without them the parts stay invisible in the bucket until a lifecycle rule (AbortIncompleteMultipartUpload) clears them. If forge_backup itself is killed, the kernel kills aws with it. Even an upload that somehow completed early would not decrypt: an age stream that was never finished fails decryption.
For streams over 50 GB, aws needs to know the size in advance; forge_backup passes --expected-size from du -sb of the folder (or the database's size in information_schema), plus 10%.
Keys
- Generate each key pair offline, on a trusted machine:
age-keygen -o backup-key-1.txt. The file holds the private key; the# public key: age1…line in it is what goes inage_recipients. - Use more than one key (for example three), held separately, so losing one does not lose the backups: for example one in the operator's password manager, one on paper and USB in a safe, and one with a second person.
- Only the public keys go on servers. A private key never goes on a server, in a config, or in this repository.
- To replace a key: add the new public key to
age_recipientseverywhere, then remove the old one. Backups made before the change still need the old private key.
Restoring
You need age (apt install age, brew install age), the AWS CLI with read access to the bucket, and one of the private keys in a file (here identity.txt). Delete that file when you are done.
A home folder
aws s3 cp s3://<bucket>/<s3_folder>/<hostname>/2026-1025/alpha_backup_2026-1025.zip.age .
age -d -i identity.txt alpha_backup_2026-1025.zip.age > alpha_backup_2026-1025.zip
unzip -t alpha_backup_2026-1025.zip # every entry should say OK
unzip alpha_backup_2026-1025.zip -d restore/
The archive holds each site's .env and storage/ under their full paths (home/alpha/<site>/.env). Deploy the site's code as usual, then put those two back.
The archive holds absolute paths without the leading / (home/alpha/...).
A database
aws s3 ls s3://<bucket>/<s3_folder>/<hostname>/db/2026-1025/
aws s3 cp s3://<bucket>/<s3_folder>/<hostname>/db/2026-1025/asg_phoneus_160002.sql.gz.age .
age -d -i identity.txt asg_phoneus_160002.sql.gz.age | gunzip | mysql
The dump was made with -B, so it carries CREATE DATABASE and USE for the original name and restores into it. To restore under another name, or just to look, decrypt to a file first (age -d -i identity.txt x.sql.gz.age | gunzip > x.sql) and edit those lines.
A file that won't decrypt with no identity matched any of the recipients was encrypted to other keys. One that starts decrypting and then fails is damaged or from a run that did not finish: take the day before.
Contribution Guidelines
We welcome contributions. Please follow these guidelines:
- Fork the repository and clone your fork.
- Create new branch for your feature or bugfix.
- Make your changes and test thoroughly.
- Commit your changes with clear and descriptive commit messages.
- Push your branch to your forkl.
- Open a pull request and describe your changes.
License
This project is licensed under the MIT License.
Issues and Feature Requests
If you encounter any issues or have feature requests, please submit them on our GitHub Issues page.
Contact
For any questions or support, please feel free to reach out via GitHub.
Thank you for using forge_backup!