-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup-script.sh
More file actions
246 lines (243 loc) · 8.32 KB
/
backup-script.sh
File metadata and controls
246 lines (243 loc) · 8.32 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash
#
# Backup script for incremental backups to i.e. removable usb drive
# (c) Rickard Armiento, 2005-2025
# Based on the ideas from: http://www.mikerubel.org/computers/rsync_snapshots/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# NOTE:
# 1. Destination has to be on a file system that supports hardlinks,
# i.e. Linux ext2/3/4, it will not work on fat32-preformated
# usb memory.
#
# 2. The script uses hardlinks between unchanged files to
# create what appears as a complete backup for old
# dates, while only storing incremental changes.
#
# 3. If you are running out of disk space, you can clean out your
# old backups manually by just removing directories in the
# backup tree with rm -rf <name>. However, you only earn the disk
# space that corresponds to unique files in that backup. (Read up
# on hardlinks if this statement confuses you.)
#
# 4. Make sure the date on your computer is correct before
# running this script.
#
##################### CONFIG ####################
## BACKUP_SOURCE
## Absolute path to directory to be backed up
BACKUP_SOURCES=/storage/home/*
## BACKUP_DEST
## Absolute directory path to backup to
BACKUP_DEST_MAIN="/storage/backup/home"
## Date format for backup directory names (see manpage for date)
## ISO: "%Y-%m-%d"
## US: "%m.%d.%Y"
DATE_FORMAT="%Y-%m-%d"
#DATE_FORMAT="%m.%d.%Y"
###################################################
#### Max number of days to keep files in different categories
MAX_DAILY=7
MAX_WEEKLY=32
MAX_MONTHLY=366
#### Best practices
set -e
set -u
cd /tmp
unset PATH
LS=/bin/ls
RSYNC=/usr/bin/rsync
CP=/bin/cp
FIND=/usr/bin/find
TAIL=/usr/bin/tail
HEAD=/usr/bin/head
TOUCH=/bin/touch
DATE=/bin/date
MKDIR=/bin/mkdir
RM=/bin/rm
MV=/bin/mv
BASENAME=/usr/bin/basename
if [ ! -x "$LS" -o ! -x "$RSYNC" -o ! -x "$CP" -o ! -x "$FIND" -o ! -x "$TAIL" -o ! -x "$TOUCH" -o ! -x "$DATE" -o ! -x "$MKDIR" -o ! -x "$RM" -o ! -x "$MV" -o ! -x "$BASENAME" ]; then
echo "Some binaries seems to be missing." >&2
exit 1
fi
#### Check and set lockfile.
if [ -e "${BACKUP_DEST_MAIN}/backup_lockfile" ]; then
echo "Lockfile exists: ${BACKUP_DEST_MAIN}/backup_lockfile" >&2
echo "Another backup may already be running, make sure it is not so," >&2
echo "then delete the lockfile." >&2
exit 1
fi
trap "$RM -f \"${BACKUP_DEST_MAIN}/backup_lockfile\"; exit" INT TERM EXIT
$TOUCH "${BACKUP_DEST_MAIN}/backup_lockfile"
for BACKUP_SOURCE in $BACKUP_SOURCES; do
USERNAME=$($BASENAME "$BACKUP_SOURCE")
BACKUP_DEST="$BACKUP_DEST_MAIN/$USERNAME"
#### Check that source and destination directories exist.
if [ ! -d "${BACKUP_SOURCE}" ]; then
echo "Script weirdness: was given $BACKUP_SOURCE to backup, but the directory does not exist." >&2
continue
fi
if [ ! -d "${BACKUP_DEST}" ]; then
$MKDIR -p "${BACKUP_DEST}"
fi
#### Create backup subdirectories if they do not exist.
$MKDIR -p "${BACKUP_DEST}/Daily"
$MKDIR -p "${BACKUP_DEST}/Weekly"
$MKDIR -p "${BACKUP_DEST}/Monthly"
$MKDIR -p "${BACKUP_DEST}/Yearly"
echo "Starting backup script at $($DATE)."
echo "Handle 'Previous' directory."
#### Handle 'Previous' directory
if [ -d "${BACKUP_DEST}/Previous" ]; then
PREVIOUS_DATE=$($DATE -r "${BACKUP_DEST}/Previous" +${DATE_FORMAT})
if [ ! -d "${BACKUP_DEST}/Daily/${PREVIOUS_DATE}" ]; then
$MV "${BACKUP_DEST}/Previous" "${BACKUP_DEST}/Daily/${PREVIOUS_DATE}"
else
$RM -rf "${BACKUP_DEST}/Previous"
fi
fi
echo "Cleaning out daily backups."
#### Clean daily backups
while [ 0 == 0 ]; do
# Find oldest daily backup
OLDEST_DAILY=$($LS "${BACKUP_DEST}/Daily" -1t | $TAIL -1)
# Check if it is older than MAX_DAILY days old
if [ -d "${BACKUP_DEST}/Daily/${OLDEST_DAILY}" ]; then
IS_OLD=$($FIND "${BACKUP_DEST}/Daily/${OLDEST_DAILY}" -maxdepth 0 -mtime +$MAX_DAILY)
if [ ! -z "${IS_OLD}" ]; then
# Find its week number
NEW_WEEKNBR="$((1 + $($DATE -r "${BACKUP_DEST}/Daily/${OLDEST_DAILY}" +'%_d') / 7))"
# Find most recent weekly backup
YOUNGEST_WEEKLY=$($LS "${BACKUP_DEST}/Weekly" -1t | $HEAD -1)
if [ ! -z "${YOUNGEST_WEEKLY}" -a -d "${BACKUP_DEST}/Weekly/${YOUNGEST_WEEKLY}" ]; then
OLD_WEEKNBR="$((1 + $($DATE -r "${BACKUP_DEST}/Weekly/${YOUNGEST_WEEKLY}" +'%_d') / 7))"
if [ "${NEW_WEEKNBR}" -eq "${OLD_WEEKNBR}" ]; then
#We already have a backup from this week
$RM -rf "${BACKUP_DEST}/Daily/${OLDEST_DAILY}"
continue
fi
fi
$MV "${BACKUP_DEST}/Daily/${OLDEST_DAILY}" "${BACKUP_DEST}/Weekly/"
continue
fi
fi
break
done
echo "Cleaning out weekly backups."
#### Clean weekly backups
while [ 0 == 0 ]; do
# Find oldest weekly backup
OLDEST_WEEKLY=$($LS "${BACKUP_DEST}/Weekly" -1t | $TAIL -1)
# Check if it is older than MAX_WEEKLY days old
if [ -d "${BACKUP_DEST}/Weekly/${OLDEST_WEEKLY}" ]; then
IS_OLD=$($FIND "${BACKUP_DEST}/Weekly/${OLDEST_WEEKLY}" -maxdepth 0 -mtime +$MAX_WEEKLY)
if [ ! -z "${IS_OLD}" ]; then
# Find its month number
NEW_MONTHNBR="$($DATE -r "${BACKUP_DEST}/Weekly/${OLDEST_WEEKLY}" +'%_m')"
# Find most recent monthly backup
YOUNGEST_MONTHLY=$($LS "${BACKUP_DEST}/Monthly" -1t | $HEAD -1)
if [ ! -z "${YOUNGEST_MONTHLY}" -a -d "${BACKUP_DEST}/Monthly/${YOUNGEST_MONTHLY}" ]; then
OLD_MONTHNBR="$($DATE -r "${BACKUP_DEST}/Monthly/${YOUNGEST_MONTHLY}" +'%_m')"
if [ "${NEW_MONTHNBR}" -eq "${OLD_MONTHNBR}" ]; then
# We already have a backup from this month
$RM -rf "${BACKUP_DEST}/Weekly/${OLDEST_WEEKLY}"
continue
fi
fi
$MV "${BACKUP_DEST}/Weekly/${OLDEST_WEEKLY}" "${BACKUP_DEST}/Monthly/"
continue
fi
fi
break
done
echo "Cleaning out monthly backups."
#### Clean monthly backups
while [ 0 == 0 ]; do
# Find oldest monthly backup
OLDEST_MONTHLY=$($LS "${BACKUP_DEST}/Monthly" -1t | $TAIL -1)
# Check if it is older than MAX_MONTHLY days old
if [ -d "${BACKUP_DEST}/Monthly/${OLDEST_MONTHLY}" ]; then
IS_OLD=$($FIND "${BACKUP_DEST}/Monthly/${OLDEST_MONTHLY}" -maxdepth 0 -mtime +$MAX_MONTHLY)
if [ ! -z "${IS_OLD}" ]; then
# Find its year number
NEW_YEARNBR="$($DATE -r "${BACKUP_DEST}/Monthly/${OLDEST_MONTHLY}" +'%Y')"
# Find most recent yearly backup
YOUNGEST_YEARLY=$($LS "${BACKUP_DEST}/Yearly" -1t | $HEAD -1)
if [ ! -z "${YOUNGEST_YEARLY}" -a -d "${BACKUP_DEST}/Yearly/${YOUNGEST_YEARLY}" ]; then
OLD_YEARNBR="$($DATE -r "${BACKUP_DEST}/Yearly/${YOUNGEST_YEARLY}" +'%Y')"
if [ "${NEW_YEARNBR}" -eq "${OLD_YEARNBR}" ]; then
# We already have a backup from this year
$RM -rf "${BACKUP_DEST}/Monthly/${OLDEST_MONTHLY}"
continue
fi
fi
$MV "${BACKUP_DEST}/Monthly/${OLDEST_MONTHLY}" "${BACKUP_DEST}/Yearly/"
continue
fi
fi
break
done
if [ -e "${BACKUP_DEST}/Current" ]; then
$MV "${BACKUP_DEST}/Current" "${BACKUP_DEST}/Previous"
fi
if [ ! -e "${BACKUP_DEST}/Current" ]; then
$MKDIR -p "${BACKUP_DEST}/Current"
fi
if [ -d "${BACKUP_DEST}/Previous" ]; then
echo "Backing up changed files."
$RSYNC -a --delete --link-dest="${BACKUP_DEST}/Previous" "${BACKUP_SOURCE}/" "${BACKUP_DEST}/Current" --exclude "/.ARCHIVE"
else
echo "First initial backup."
$RSYNC -a --delete "${BACKUP_SOURCE}/" "${BACKUP_DEST}/Current" --exclude "/.ARCHIVE"
fi
#### Mark current backup with date when backup finished.
$TOUCH "${BACKUP_DEST}/Current"
done
#### Touch backup directory with date all backups finished
$TOUCH "$BACKUP_DEST_MAIN"
echo "Backup finished at $($DATE)."