Skip to content

Instantly share code, notes, and snippets.

@NotUser3
Last active January 27, 2023 07:57
Show Gist options
  • Save NotUser3/a7bc89d876c7ee936de4abaeb8f8f300 to your computer and use it in GitHub Desktop.
Save NotUser3/a7bc89d876c7ee936de4abaeb8f8f300 to your computer and use it in GitHub Desktop.
A shell script that automatically creates a backup via BORG and automatically uploads it to an FTP server. Includes password encryption and cronjob.
#!/bin/bash
# Install BORG and lftp
sudo apt-get update
sudo apt-get install -y borgbackup lftp
# Create a password file
read -p "Enter the passphrase for the BORG repository: " passphrase
echo $passphrase > /etc/borg_passphrase
chmod 600 /etc/borg_passphrase
# Create a BORG repository on the FTP server
borg init --encryption=repokey-blake2 ftp://username:password@ftp.example.com/path/to/borg/repo
# Create a script for creating backups and uploading them to FTP
echo "borg create -v --stats --compression lz4 ftp://username:password@ftp.example.com/path/to/borg/repo::'{hostname}-{now:%Y-%m-%d}' /path/to/folder" > /usr/local/bin/backup.sh
echo "borg prune -v --list ftp://username:password@ftp.example.com/path/to/borg/repo --prefix '{hostname}-' --keep-daily 7 --keep-weekly 4 --keep-monthly 6" >> /usr/local/bin/backup.sh
echo "export BORG_PASSPHRASE=$(cat /etc/borg_passphrase) " >> /usr/local/bin/backup.sh
chmod +x /usr/local/bin/backup.sh
# Create a cronjob that runs the backup script every day at 1am
echo "0 1 * * * /usr/local/bin/backup.sh" | crontab -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment