borg-backup.sh (2336B)
1 #!/usr/bin/env bash 2 3 set -o errexit 4 set -o nounset 5 set -o xtrace 6 7 cd /root/ 8 9 # Install borg backup prerequisites 10 # https://borgbackup.readthedocs.io/en/master/installation.html#debian-ubuntu 11 apt-get install -y python3 python3-dev python3-pip python3-virtualenv \ 12 libacl1-dev libacl1 \ 13 libssl-dev \ 14 liblz4-dev libzstd-dev libxxhash-dev \ 15 libffi-dev \ 16 build-essential \ 17 pkg-config python3-pkgconfig 18 19 # Install latest borg2 with pip 20 # https://borgbackup.readthedocs.io/en/master/installation.html#using-pip 21 virtualenv --python=python3 borg-env 22 source borg-env/bin/activate 23 pip install -U pip setuptools wheel 24 pip install pkgconfig 25 pip install borgbackup==2.0.0b14 26 27 # Symlink borg from venv to global path 28 ln -s /root/borg-env/bin/borg /usr/local/bin/borg 29 30 # Ensure the borg binary is in the path 31 cat <<'EOF' >> /root/.bashrc 32 33 PATH=$PATH:/usr/local/bin 34 EOF 35 36 # Configure borg repo on pcloud 37 mkdir -p /root/.config/borg 38 39 cat <<EOF > /root/.config/borg/config 40 BORG_REPO='$BORG_REPO' 41 BORG_PASSPHRASE='$BORG_PASSPHRASE' 42 PATTERNSFILE='$PATTERNSFILE' 43 EOF 44 45 # Check the encryption/hash bench with `borg benchmark cpu` 46 # You have to initialize the borg repo and rclone before first use 47 # borg repo-create --encryption=repokey-chacha20-poly1305 48 # rclone config 49 50 cat <<EOF > "$PATTERNSFILE" 51 # https://borgbackup.readthedocs.io/en/stable/usage/help.html#borg-patterns 52 # "sh:" pattern style, patternfiles use sh: by default 53 P sh 54 # A root path starts with the prefix R, followed by a path 55 R /host-nfs 56 R /host-srv 57 EOF 58 59 # Install systemd service unit 60 cat <<'EOF' > /etc/systemd/system/borg-backup.service 61 [Unit] 62 Description=Create a backup with borg 63 64 [Service] 65 Type=oneshot 66 EnvironmentFile=/root/.config/borg/config 67 ExecStart=/usr/local/bin/borg create -v --stats --compression lz4 {hostname}-{now:%%Y%%m%%dT%%H%%M} --patterns-from "$PATTERNSFILE" --exclude-caches --chunker-params fixed,4096 68 ExecStart=/usr/local/bin/borg prune -v --list --match-archives '{hostname}-*' --keep-daily=7 --keep-weekly=4 --keep-monthly=12 69 70 [Install] 71 WantedBy=default.target 72 EOF 73 74 # Install systemd timer 75 cat <<'EOF' > /etc/systemd/system/borg-backup.timer 76 [Unit] 77 Description=Create a backup with borg 78 79 [Timer] 80 Unit=borg-backup.service 81 OnCalendar=*-*-* 11:03:00 82 83 [Install] 84 WantedBy=timers.target 85 EOF 86 87 systemctl enable borg-backup.timer 88 systemctl start borg-backup.timer