forked from eike-welk/borg-backup-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathborg-backup-create.sh
executable file
·133 lines (115 loc) · 4.86 KB
/
borg-backup-create.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
# Show debugging output
#set -x
# ----------------------------------------------------------------------------
# Create Backups with **Borg**
# ----------------------------------------------------------------------------
# This script creates backups on a local had disk. It keeps a number of past
# backups, and deletes backups that are too old. It uses the backup program
# *Borg*.
#
# The original version of this script was taken from the documentation of Borg.
#
# The Github project of this script:
# https://github.com/eike-welk/borg-backup-scripts
#
# Documentation and source code for the *Borg* program itself:
# https://borgbackup.readthedocs.io/en/stable/index.html
# https://github.com/borgbackup/borg/
#
# ----------------------------------------------------------------------------
# Create new backup repositories with the following command:
#
# borg init --encryption=repokey /backup/borg-backup/lixie-backup-1.borg
#
# ----------------------------------------------------------------------------
# List the repository's contents:
#
# borg list /backup/borg-backup/lixie-backup-1.borg
#
# Restore an archive: The restored files are created in the current working
# directory.
#
# borg extract /backup/borg-backup/lixie-backup-1.borg/::lixie-2018-04-13T17:11:46
#
# ----------------------------------------------------------------------------
# Copy the backup repository to an other (removable) disk with *Rsync*. Option
# `--delete` deletes file which are no longer in the source directory.
#
# rsync --verbose --archive --delete \
# /backup/borg-backup/lixie-backup-1.borg \
# /run/media/root/back-ext-4/borg-backup
#
# ----------------------------------------------------------------------------
# The backup configuration file:
#
# /etc/borg-backup/repo-secrets.sh
#
# `repo-secrets.sh` must contain the following lines:
#
# # The location of the backup repository.
# BORG_REPO='/backup/borg-backup/lixie-backup-1.borg'
# # The repository's passphrase:
# BORG_PASSPHRASE='xxxxxxxxxxx'
#
# ----------------------------------------------------------------------------
# Set the repository location and passphrase. --------------------------------
source /etc/borg-backup/repo-secrets.sh
export BORG_REPO
export BORG_PASSPHRASE
# Print messages to standard output with date and time.
info() { printf "\n%s %s\n\n" "$( date --rfc-3339=seconds )" "$*" >&2; }
# Exit the whole script when Ctrl-C is pressed.
trap 'info "Backup interrupted."; exit 2' INT TERM
info "Starting backup - $BORG_REPO"
# Create the backup ----------------------------------------------------------
# The command line is for the older *Borg* version 1.0.10
# The archive name consists of the hostname, the current date and time.
# Further cache directories that may be excluded from the archive:
# --exclude '/var/cache/*' \
# --exclude '/var/tmp/*' \
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz4 \
--exclude-caches \
--exclude '/home/*/.cache/*' \
\
::'{hostname}-{utcnow:%Y-%m-%dT%H:%M:%S}' \
\
'/home' \
'/usr/local' \
backup_exit=$?
info "Pruning repository"
# Delete old archives --------------------------------------------------------
# Use the `prune` subcommand to delete old backups, but keep a number of mostly
# recent backups. The retention rules are: Keep all backups for 2 days. Keep 10
# daily, 10 weekly, 10 monthly and unlimited yearly backups.
# Only backups of THIS machine are affected. The '{hostname}-' prefix limits
# the prune command to backups of the current machine. Therefore the archive
# could hold backups of multiple computers.
borg prune \
--list \
--prefix '{hostname}-' \
--show-rc \
--keep-within 2d \
--keep-daily 10 \
--keep-weekly 10 \
--keep-monthly 10 \
--keep-yearly -1 \
prune_exit=$?
# Error handling -------------------------------------------------------------
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 1 ];
then
info "Backup and/or Prune finished with a warning"
fi
if [ ${global_exit} -gt 1 ];
then
info "Backup and/or Prune finished with an error"
fi
exit ${global_exit}