aboutsummaryrefslogtreecommitdiff
path: root/rsync-backup.sh
blob: ae094ea5448bb5ffeaf9ce48e78fcf3b515258b6 (plain)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/bin/bash
#
# rsync-backup.sh - a backup script using rsync.
#
# Copyright 2015 - 2018 Einhard Leichtfuß
#
# This file is part of rsync-backup.
#
# rsync-backup 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.
# 
# rsync-backup 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 General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with rsync-backup.  If not, see <http://www.gnu.org/licenses/>.
#

RSYNC=rsync

typeset -a rsync_args
typeset -a rsync_inc_args
typeset -A bakpath
typeset filter_file_all
typeset -A filter_file

# Default arguments for rsync.
rsync_args=(
	--recursive
	--perms --times --owner --group
	#--acls --xattrs
	--links
	--hard-links
	--sparse
	--timeout=60
	-vv
	)

# Supplemental arguments for rsync; only used when an actual incremental
# update is performed, i.e. when --link-dest is used.
rsync_inc_args=(
	--fuzzy --fuzzy
	)

filter_file_all=/etc/rsync-backup/filter

config=/etc/rsync-backup/backup.conf
source "$config"

src=/home/respiranto/txt/bash/rsync-backup/src
dest=/home/respiranto/txt/bash/rsync-backup/dest

typeset -l reply
typeset -i i n

# RSYNC args.
# basic args:  --recursive --perms --times --owner --group --links
# extra args:  --sparse --acls --xattrs --hard-links --timeout=60
#              --fuzzy --fuzzy
#              --filter="merge <file>"
# likely:      --compress
#              --partial{,dir=DIR}
#              --progress
# possibly:    --devices --specials
#              --max-size=SIZE-OF-FILE
#              --one-file-system 
#              --log-file=FILE --log-file-format=FORMAT
#              --human-readable
# unlikely:    --omit-{dir,link}-times --update (!--inplace) --delete
#              --exclude
# great:       --link-dest=DIR (timestamps?)
# testing:     --verbose --dry-run (-vn)
# interesting: --sockopts --itemize-changes --out-format=FORMAT
#              --stats

function main
{
	# In case of a non-matching wildcard, return nothing.
	shopt -s nullglob
	# Include dotfiles (except . and ..) in regex matching.
	shopt -s dotglob

	get_args "$@" || return $?

	check_paths || return $?

	# Change the separation char to $'\n' to avoid confusion with filenames
	# containing spaces (default is $' \t\n').
	IFS=$'\n'

	find_existing_backup_dirs || return $?

	perform_backup || return $?

	refresh_symlinks || return $?
}


function get_args
{
	if [ $# -eq 0 ]
	then
		echo "Usage: $0 <src> [<dest>]"
		return 1
	fi

	dirpath="$(realpath "$1")"

	if [ $# -gt 1 ]
	then
		bakpath="$(realpath $2)"
	elif test -v bakpath[$dirpath]
	then
		bakpath="${bakpath[$dirpath]}"
	else
		echo "Error: Backup path for $dirpath not configured."
		return 1
	fi

	filter_args=()
	if test -v filter_file_all -a -f "$filter_file_all"
	then
		filter_args=("--filter=merge ${filter_file_all}")
	fi
	if test -v filter_file[$dirpath] && test -f "${filter_file[$dirpath]}"
	then
		filter_args+=("--filter=merge ${filter_file[$dirpath]}")
	fi
}


function check_paths
{
	# Test whether the dirs exist already.
	if ! test -d "$dirpath"
	then
		echo "Source (\"$dirpath\") does not exist."
		return 1
	fi

	if ! test -d "$bakpath"
	then
		echo "Target (\"$bakpath\") does not exist."
		echo -n "Do you want do create it [y/N]? "
		read reply
		if [[ "$reply" != "y" && "$reply" != "yes" ]]
		then
			return 1
		fi

		if ! mkdir -p "$bakpath"
		then
			echo "Backup creation has failed."
			echo "\"$bakpath\" could not be created."
			return 1
		fi
	fi
}


function find_existing_backup_dirs
{
	n=0
	for file in `ls "$bakpath"`; do
		if test -d $bakpath/$file \
			&& [[ "$file" =~ ^[0-9]{4}-[0-1][0-9]-[0-3][0-9]_[0-2][0-9]([0-5][0-9]){2}.backup$ ]]
		then
			dirs[n]="$file"
			n+=1
		fi
	done
}


# $1 path
# $2 error message: string
function clear_file
{
	if test -e "$1" -o -L "$1"; then
		echo "It seems like the last backup process failed or you have created" \
		 	"the file \"$1\" manually."
		echo -n "Do you want to delete it [y/N]? "
		read reply
		if [[ "$reply" == "y" || "$reply" == "yes" ]]
		then
			printf "Deleting"
			if rm -rf "$1"; then
				printf " - done\n"
				return 0
			else
				printf '\n%s\n' "$2"
				printf 'The file \"%s\" could not be deleted.\n' "$1"
				return 1
			fi
		else
			printf '%s\n' "$2"
			printf '%s %s\n' "Please remove the above mentioned file manually" \
				"or run this script once more."
			return 1
		fi
	fi
	return 0
}


function perform_backup
{
	bakdate=`date +%Y-%m-%d_%H%M%S.backup`
	finaldir="$bakpath/$bakdate"
	tempdir="$bakpath/new.backup"
	clear_file "$tempdir" "Backup creation has failed." || return 1
	clear_file "$finaldir" "Backup creation has failed." || return 1

	# note: $n == ${#dirs[@]}
	printf 'Starting backup nr. %u.\n' "$n"
	if [ $n -eq 0 ]
	then
		# Perform first backup, not incremental.
		$RSYNC "${rsync_args[@]}" \
			"${filter_args[@]}" \
			"$dirpath"/ "$tempdir" \
			|| return 1
	else
		# Perform incremental backup on the basis of the last.
		$RSYNC "${rsync_args[@]}" \
			"${rsync_inc_args[@]}" --link-dest="${bakpath}/${dirs[n-1]}" \
			"${filter_args[@]}" \
			"$dirpath"/ "$tempdir" \
			|| return 1
	fi

	mv "$tempdir" "$finaldir" || return 1
	printf '\nCreating of backup nr. %u succesfully executed.\n' "$n"
}


function refresh_symlinks
{
	linkdir="$bakpath/latest"
	if test -L "$linkdir"
	then
		rm "$linkdir"
		ln -s "$bakdate" "$linkdir"
	elif clear_file "$linkdir" "Creation of softlink \"$linkdir\" failed."
	then
		ln -s "$bakdate" "$linkdir"
	fi

	# by_number
	numdir="$bakpath/by_number"
	if ! test -d "$numdir"
	then
		if clear_file "$numdir" "Creating directory $numdir failed."
		then
			mkdir "$numdir"
		else
			return 1
		fi
	fi

	for file in "$numdir"/*
	do
		if test -L "$file"
		then
			rm "$file"
		else
			if ! clear_file "$file" "Setting up numbered softlinks failed."
			then
				return 1
			fi
		fi
	done

	dirs[$n]="$bakdate" # Set last dir on current.
	max_n_zeros=$((${#n} - 1))
	i=1
	while [ $i -le $max_n_zeros ]
	do
		zeros[i]=$(printf '%.s0' {$i..$max_n_zeros})
		i+=1
	done

	i=0
	while [ $i -le $n ]
	do
		ln -s "../${dirs[i]}" "$numdir/${zeros[${#i}]}$i"
		i+=1
	done
}


main "$@"