Skip to content

Instantly share code, notes, and snippets.

@siddolo
Created January 2, 2024 15:00
Show Gist options
  • Save siddolo/01a5e24dd7e2ed6731f169bb4eabb560 to your computer and use it in GitHub Desktop.
Save siddolo/01a5e24dd7e2ed6731f169bb4eabb560 to your computer and use it in GitHub Desktop.
OVH to Borg Backup

Backup

OS: Ubuntu 22.04 LTS

Setup

apt update && apt upgrade
apt install python3 python3-pip borgbackup htop net-tools mariadb-client
pip install --upgrade borgmatic
pip install --upgrade borgweb

generate-borgmatic-config

mkdir /etc/borgweb
touch /etc/borgweb/borgweb.conf

mkdir /root/backup-sources
mkdir /root/backup-sources/foobar.com
mkdir /root/backup-sources/foobar.com/www
mkdir /root/backup-sources/foobar.com/db
mkdir /root/backup-repository
mkdir /var/log/borg

borg init --encryption=repokey /root/backup-repository/

ssh-keygen
ssh-copy-id foobar@ssh.foobar.hosting.ovh.net

Config files

/etc/borgweb/borgweb.conf

HOST = '0.0.0.0'  # use 0.0.0.0 to bind to all interfaces
PORT = 5000  # ports < 1024 need root
DEBUG = False  # if True, enable reloader and debugger

LOG_DIR = '/var/log/borg'
REPOSITORY = 'repo'
NAME = 'backup.log'

BACKUP_CMD = "echo Manual backup disabled"

/etc/borgmatic/config.yaml

# Where to look for files to backup, and where to store those backups.
# See https://borgbackup.readthedocs.io/en/stable/quickstart.html and
# https://borgbackup.readthedocs.io/en/stable/usage/create.html
# for details.
location:
    source_directories:
        - /root/backup-sources/foobar.com

    repositories:
        - /root/backup-repository

storage:
    encryption_passphrase: "REDACTED"

retention:
    keep_daily: 7

    keep_weekly: 4

    keep_monthly: 6

    keep_yearly: 1

hooks:
    before_backup:
        - echo "Starting a backup."

    before_prune:
        - echo "Starting pruning."

    before_compact:
        - echo "Starting compaction."

    before_check:
        - echo "Starting checks."

    before_extract:
        - echo "Starting extracting."

    after_backup:
        - echo "Finished a backup."

    after_compact:
        - echo "Finished compaction."

    after_prune:
        - echo "Finished pruning."

    after_check:
        - echo "Finished checks."

    after_extract:
        - echo "Finished extracting."

    on_error:
        - echo "Error during prune/compact/create/check."

    before_everything:
        - echo "Starting actions."
        - echo "Downloading files from ssh.foobar.hosting.ovh.net..."
        - ssh foobar@ssh.foobar.hosting.ovh.net tar pczf - /homez.37/foobar/www | tar xzf - --no-same-owner -C /root/backup-sources/foobar.com/www/
        - echo "Exporting mysql database foobar_foobar..."
        - ssh mysqldump --no-tablespaces -u foobar_foobar -pREDACTED -h foobar-001.eu.clouddb.ovh.net -P 35233 foobar_foobar | gzip > /root/backup-sources/foobar.com/db/foobar_foobar.sql.gz


    after_everything:
        - echo "Completed actions."

/etc/systemd/system/rc-local.service

[Unit]
 Description=/etc/rc.local Compatibility
 ConditionPathExists=/etc/rc.local

[Service]
 Type=forking
 ExecStart=/etc/rc.local start
 TimeoutSec=0
 StandardOutput=tty
 RemainAfterExit=yes
 SysVStartPriority=99

[Install]
 WantedBy=multi-user.target

/etc/rc.local

#!/usr/bin/env bash
BORGWEB_CONFIG=/etc/borgweb/borgweb.conf /usr/local/bin/borgweb

/etc/logrotate.d/borg

/var/log/borg/*
{
  rotate 12
  monthly
  compress
  missingok
  notifempty
}

Crontab

0 2 * * * /usr/local/bin/borgmatic > /var/log/borg/backup-$(date '+\%Y-\%m-\%d').log 2>&1

Logrotate update conf

service logrotate restart

Borgweb autorun

chmod +x /etc/rc.local
systemctl enable rc-local
reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment