Borgbackup: Difference between revisions

Created page with "[https://borgbackup.readthedocs.io/en/stable/ borgbackup] is a deduplicating backup software ==Installation== [https://borgbackup.readthedocs.io/en/stable/installation.html I..."
 
 
(17 intermediate revisions by the same user not shown)
Line 17: Line 17:
# Create a repo without encryption
# Create a repo without encryption
borg init --encryption=none ./testrepo
borg init --encryption=none ./testrepo
# If you want encryption
# If you want encryption
#borg init --encryption=repokey ./testrepo
borg init --encryption=repokey ./testrepo
</pre>
</pre>


Line 26: Line 27:
The following will create a backup of <code>~/src</code> titled <code>Monday</code> to the repo <code>./testrepo</code>.
The following will create a backup of <code>~/src</code> titled <code>Monday</code> to the repo <code>./testrepo</code>.
<pre>
<pre>
borg create --stats ./testrepo::Monday ~/src
borg create --stats --progress ./testrepo::Monday ~/src
</pre>
</pre>


;Notes
;Notes
* Name placeholders: {now}, {utcnow}, {fqdn}, {hostname}, {user}
* Name placeholders: {now}, {utcnow}, {fqdn}, {hostname}, {user}
* Progress shows (O, C, D, N) which correspond to original size, compressed size, deduplicated size, and number of files processed.
** Deduplicated size is the additional size consumed on disk after compression and deduplication.


{{hidden | Examples |
{{hidden | Examples |
<pre>
<pre>
borg create \
borg create \
   --stats --show-rc \
   --stats --progress --show-rc \
   ./testrepo::'{hostname}-daily-{utcnow}' \
   ./testrepo::'{hostname}-daily-{utcnow}' \
   /src
   /src
Line 42: Line 45:


===Pruning backups===
===Pruning backups===
If you're scripting daily, weekly, or monthly backups, you'll probably want to prune old backups.
[https://borgbackup.readthedocs.io/en/stable/usage/prune.html borg prune]
 
If you're scripting daily, weekly, or monthly backups, you'll probably want to prune old backups.
The command below will keep one backup from today, one backup from last week, and one backup from one month.
<pre>
borg prune --keep-daily 1 --keep-weekly 1 --keep-monthly 1 remote:~/my_backup
</pre>
 
===Extract===
See [https://borgbackup.readthedocs.io/en/stable/usage/extract.html extract]
<pre>
borg extract --progress <archive>
</pre>
 
* Note that borgbackup <code>1.1.11</code> included in Ubuntu 20.04 has a memory leak when using <code>--progress</code> [https://github.com/borgbackup/borg/issues/5162 documented here]. Download a newer version of borgbackup.
* You should extract to an empty folder since borg does not delete existing files in the target folder.
 
==Scheduling==
 
===borgmatic===
See [https://torsion.org/borgmatic/ https://torsion.org/borgmatic/]
 
===Bash Script===
Below is a bash script you can call from cron or systemd.
{{hidden | backup.sh |
<syntaxhighlight lang="bash">
#!/bin/bash
REPOSITORY="my_server:~/Backups/my_backup"
ARCHIVE_NAME="{hostname}-{utcnow}"
SOURCE_DIR="/home/david/"
BACKUP_DIRS=(
  "."
)
BORG_CREATE_FLAGS=(
  "--stats"
  "--progress"
  "-x"
  "--show-rc"
)
BORG_PRUNE_FLAGS=(
  "--keep-daily"
  "3"
  "--keep-weekly"
  "1"
  "--keep-monthly"
  "1"
  #  "--stats"
  "--list"
  "--show-rc"
)
EXCLUSIONS=(
  "-e" "System Volume Information"
  "-e" "\$RECYCLE.BIN"
)
BORG_EXEC="borg"
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
LOCKFILE=/tmp/backup_server.lockfile
LOGFILE=/home/david/Documents/server-backup-scripts/my_backup.log
 
exec {lock_fd}>$LOCKFILE || exit 1
flock -n "$lock_fd" || {
  echo "Another instance is running" >&2
  exit 1
}
 
function send_fail_email {
  {
    echo From: my_email@gmail.com
    echo To: my_gmail@gmail.com
    echo Subject: my_backup failed
    echo
    echo my_backup failed
  } | /usr/lib/sendmail -t
  exit
}
 
if [ -d "${SOURCE_DIR}" ]
then
  # shellcheck disable=SC2015
  cd "${SOURCE_DIR}" && \
    $BORG_EXEC create "${BORG_CREATE_FLAGS[@]}" "${EXCLUSIONS[@]}" "${REPOSITORY}"::"${ARCHIVE_NAME}" "${BACKUP_DIRS[@]}" 2>&1 | tee "$LOGFILE" &&
    $BORG_EXEC prune "${BORG_PRUNE_FLAGS[@]}" "${REPOSITORY}" | tee -a "$LOGFILE" ||
    send_fail_email
fi
</syntaxhighlight>
}}
 
==Borg vs Restic==
Restic is a very similar piece of software which supports backing up to S3-compatible (e.g. S3, Minio, Backblaze B2) storage.
 
==Hosted Services==
See https://www.borgbackup.org/support/commercial.html


==Resources==
==Resources==
* [https://practical-admin.com/blog/backups-using-borg/ practical-admin backups using borg]
* [https://practical-admin.com/blog/backups-using-borg/ practical-admin backups using borg]