lbu.in 16.9 KB
Newer Older
Natanael Copa's avatar
Natanael Copa committed
1 2 3
#!/bin/sh

# lbu - utility to create local backups.
4
# Copyright (c) 2006-2010 Natanael Copa <ncopa@alpinelinux.org>
Natanael Copa's avatar
Natanael Copa committed
5 6
# May be distributed under GPL2

7
VERSION=@VERSION@
Natanael Copa's avatar
Natanael Copa committed
8
sysconfdir=@sysconfdir@
Natanael Copa's avatar
Natanael Copa committed
9

10 11
if [ ! -f ${libalpine:="./libalpine.sh"} ]; then
	libalpine=/usr/share/lbu/libalpine.sh
12 13 14
	if [ ! -f "$libalpine" ]; then
		libalpine=/lib/libalpine.sh
	fi
15 16
fi
. $libalpine || exit 1
Natanael Copa's avatar
Natanael Copa committed
17

Natanael Copa's avatar
Natanael Copa committed
18 19
EXCLUDE_LIST="$sysconfdir"/exclude
INCLUDE_LIST="$sysconfdir"/include
20 21
LBU_LIST="/etc/apk/protected_paths.d/lbu.list"

Natanael Copa's avatar
Natanael Copa committed
22

Natanael Copa's avatar
Natanael Copa committed
23 24
DEFAULT_CIPHER="aes-256-cbc"

Natanael Copa's avatar
Natanael Copa committed
25
LBU_CONF="$sysconfdir"/lbu.conf
26 27 28
LBU_PREPACKAGE="$sysconfdir"/pre-package.d
LBU_POSTPACKAGE="$sysconfdir"/post-package.d

Natanael Copa's avatar
Natanael Copa committed
29 30
if [ -f "$LBU_CONF" ]; then
	. "$LBU_CONF"
31 32
fi

Natanael Copa's avatar
Natanael Copa committed
33 34
UMOUNT_LIST=

Natanael Copa's avatar
Natanael Copa committed
35 36 37 38 39 40
usage() {
	echo "$PROGRAM $VERSION"
	echo "usage: $PROGRAM <subcommand> [options] [args]

Available subcommands:
  commit (ci)
41
  diff
Natanael Copa's avatar
Natanael Copa committed
42 43
  exclude (ex, delete)
  include (inc, add)
44
  list (ls)
45 46
  list-backup (lb)
  migrate_include_exclude
47
  package (pkg)
Natanael Copa's avatar
Natanael Copa committed
48
  revert
49 50 51
  status (stat, st)


Natanael Copa's avatar
Natanael Copa committed
52 53 54 55 56 57 58 59 60

Common options:
 -h	Show help for subcommand.
 -q	Quiet mode.
 -v	Verbose mode.
"
	exit 1
}

Natanael Copa's avatar
Natanael Copa committed
61 62
cleanup() {
	local i
63 64 65
	for i in $REMOUNT_RO_LIST; do
		mount -o remount,ro $i
	done
Natanael Copa's avatar
Natanael Copa committed
66 67 68 69 70 71 72 73 74 75
	for i in $UMOUNT_LIST; do
		umount $i
	done
}

exit_clean() {
	cleanup
	exit 1
}

76 77 78 79 80
# check if given dir is not a mounted mountpoint
is_unmounted() {
	awk "\$2 == \"$1\" {exit 1}" /proc/mounts
}

Natanael Copa's avatar
Natanael Copa committed
81
mount_once() {
82 83 84 85 86 87 88
	if is_unmounted "$1"; then
		mount $1 && UMOUNT_LIST="$1 $UMOUNT_LIST" || return 1
	fi
}

# check if given dir is read-only
is_ro() {
89
	local tmpfile=$(mktemp -p "$1" 2>/dev/null)
90 91 92 93 94 95 96 97 98 99
	[ -z "$tmpfile" ] && return 0
	rm -f "$tmpfile"
	return 1
}

mount_once_rw() {
	mount_once "$1" || return 1
	if is_ro "$1"; then
		REMOUNT_RO_LIST="$1 $REMOUNT_RO_LIST"
		mount -o remount,rw "$1"
Natanael Copa's avatar
Natanael Copa committed
100 101 102
	fi
}

Natanael Copa's avatar
Natanael Copa committed
103 104 105
# create backupfile
backup_apkovl() {
	local outfile="$1"
106
	local d=$( date -u -r "$outfile" "+%Y%m%d%H%M%S" )
Natanael Copa's avatar
Natanael Copa committed
107 108 109 110
	local backup=$(echo "$outfile" | sed "s/\.apkovl\.tar\.gz/.$d.tar.gz/")
	vecho "Creating backup $backup"
	if [ -z "$DRYRUN" ]; then
		mv "$outfile" "$backup"
Natanael Copa's avatar
Natanael Copa committed
111
		APKOVL_BACKUP="$backup"
Natanael Copa's avatar
Natanael Copa committed
112 113 114
	fi
}

Natanael Copa's avatar
Natanael Copa committed
115 116 117 118 119 120 121 122
restore_apkovl() {
	local outfile="$1"
	if [ -n "$DRYRUN" ] || [ -z "$APKOVL_BACKUP" ]; then
		return 0
	fi
	mv "$APKOVL_BACKUP" "$outfile"
}

Natanael Copa's avatar
Natanael Copa committed
123
# verify we have openssl if we want to encrypt
124 125 126
check_openssl() {
	[ -z "$ENCRYPTION" ] && return 0
	OPENSSL=$(which openssl 2>/dev/null) || die "openssl was not found"
Natanael Copa's avatar
Natanael Copa committed
127

128 129 130
	$OPENSSL list-cipher-commands | grep "^$ENCRYPTION$" > /dev/null \
		|| die "Cipher $ENCRYPTION is not supported"
}
Natanael Copa's avatar
Natanael Copa committed
131

132 133 134 135
# grep and sed has issues with escaping '*' in lists so we rather do
# our own filter functions
list_has() {
	local line=
136
	[ -e "$LBU_LIST" ] || return 1
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	while read line; do
		[ "$line" = "$1" ] && return 0
	done < "$LBU_LIST"
	return 1
}

list_filter_out() {
	local line=
	while read line; do
		if [ "$line" != "$1" ]; then
			echo "$line"
		fi
	done < "$LBU_LIST"
}

# list_add(char prefix, char *listfile, char* file...)
Natanael Copa's avatar
Natanael Copa committed
153
list_add() {
154
	local prefix="$1"
Natanael Copa's avatar
Natanael Copa committed
155
	shift
156
	mkdir -p "${LBU_LIST%/*}"
Natanael Copa's avatar
Natanael Copa committed
157
	while [ $# -gt 0 ] ; do
Natanael Copa's avatar
Natanael Copa committed
158
		filename="$(echo "$1" | sed -E 's:^/+::')"
159 160
		if list_has "${prefix}${filename}"; then
			vecho "$filename is already in $LBU_LIST."
Natanael Copa's avatar
Natanael Copa committed
161
		else
162 163
			vecho "Adding $filename to $LBU_LIST."
			echo "${prefix}${filename}" >> "$LBU_LIST"
Natanael Copa's avatar
Natanael Copa committed
164 165 166 167 168
		fi
		shift
	done
}

169
# list_delete(char prefix, char *listfile, char *file...)
Natanael Copa's avatar
Natanael Copa committed
170
list_delete() {
171 172
	local prefix="$1"
	local tmp="$LBU_LIST.new"
Natanael Copa's avatar
Natanael Copa committed
173
	shift
174
	[ -f "$LBU_LIST" ] || return 1
Natanael Copa's avatar
Natanael Copa committed
175
	while [ $# -gt 0 ] ; do
Natanael Copa's avatar
Natanael Copa committed
176
		filename="$(echo "$1" | sed -E 's:^/+::')"
177 178 179 180 181 182 183
		if list_has "${prefix}${filename}"; then
			vecho "Removing $filename from $LBU_LIST."
			list_filter_out "${prefix}${filename}" > "$tmp" \
				&& mv "$tmp" "$LBU_LIST"
		else
			vecho "$filename is not in $LBU_LIST"
		fi
Natanael Copa's avatar
Natanael Copa committed
184 185 186 187
		shift
	done
}

188 189 190 191
# unpack archive on LBU_MEDIA to given dir
unpack_apkovl() {
	local f="$(hostname).apkovl.tar.gz"
	local dest="$1"
Natanael Copa's avatar
Natanael Copa committed
192
	local mnt="${LBU_BACKUPDIR:-/media/$LBU_MEDIA}"
193 194
	local count=0
	mkdir -p "$dest"
Natanael Copa's avatar
Natanael Copa committed
195 196 197
	if [ -n "$LBU_MEDIA" ]; then
		mount_once "$mnt"
	fi
198 199
	if [ -n "$ENCRYPTION" ]; then
		f="$f.$ENCRYPTION"
200
	fi
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	if [ ! -f "$mnt/$f" ]; then
		return 1
	fi
	if [ -z "$ENCRYPTION" ]; then
		tar -C "$dest" -zxf "$mnt/$f"
		return
	fi
	check_openssl
        while [ $count -lt 3 ]; do
		$OPENSSL enc -d -$ENCRYPTION -in "$mnt/$f" | tar \
			-C "$dest" -zx 2>/dev/null && return 0
		count=$(( $count + 1 ))
	done
	cleanup
	die "Failed to unpack $mnt/$f"
216
}
217

Natanael Copa's avatar
Natanael Copa committed
218 219 220 221 222
#
# lbu_include - add/remove files to include list
#
usage_include() {
	echo "$PROGRAM $VERSION
Natanael Copa's avatar
Natanael Copa committed
223
Add filename(s) to include list ($sysconfdir/include)
224 225 226

usage: $PROGRAM include|inc|add [-rv] <file> ...
       $PROGRAM include|inc|add [-v] -l
Natanael Copa's avatar
Natanael Copa committed
227 228 229 230 231 232 233 234 235 236

Options:
  -l	List contents of include list.
  -r	Remove specified file(s) from include list instead of adding.
  -v	Verbose mode.
"
	exit 1
}

cmd_include() {
237
	cmd_migrate_include_exclude
Natanael Copa's avatar
Natanael Copa committed
238 239 240 241 242
	if [ "$LIST" ] ; then
		[ $# -gt 0 ] && usage_include
		show_include
		return
	fi
243

Natanael Copa's avatar
Natanael Copa committed
244 245
	[ $# -lt 1 ] && usage_include
	if [ "$REMOVE" ] ; then
246
		list_delete + "$@"
Natanael Copa's avatar
Natanael Copa committed
247
	else
248 249
		list_add + "$@"
		list_delete - "$@"
Natanael Copa's avatar
Natanael Copa committed
250 251 252 253
	fi
}

show_include() {
254
	if [ -f "$LBU_LIST" ] ; then
255
		vecho "Include files:"
256
		grep -- '^+' "$LBU_LIST" | sed 's/^+//'
Natanael Copa's avatar
Natanael Copa committed
257 258 259
	fi
}

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
#
# lbu_package - create a package
#
usage_package() {
	echo "$PROGRAM $VERSION
Create backup package.

usage: $PROGRAM package|pkg -v [<dirname>|<filename>]

Options:
  -v	Verbose mode.

If <dirname> is a directory, a package named <hostname>.apkovl.tar.gz will
be created in the specified directory.

If <filename> is specified, and is not a direcotry, a package with the
specified name willbe created.

278
If <dirname> nor <filename> is not specified, a package named
279 280 281 282 283
<hostname>.apkovl.tar.gz will be created in current work directory.
"
	exit 1
}

284
_gen_filelist() {
285
	apk audit --backup --quiet --recursive --check-permissions
286 287
}

288 289 290 291
cmd_package() {
	local pkg="$1"
	local rc=0
	local owd="$PWD"
292
	local suff="apkovl.tar.gz"
293
	local tmpdir tmppkg
294

295
	cmd_migrate_include_exclude
296
	check_openssl
Natanael Copa's avatar
Natanael Copa committed
297
	init_tmpdir tmpdir
298

299
	if [ -d "$LBU_PREPACKAGE" ]; then
300
		run-parts "$LBU_PREPACKAGE" >&2 || return 1
301 302
	fi

303
	[ -n "$ENCRYPTION" ] && suff="$suff.$ENCRYPTION"
304 305 306

	# find filename
	if [ -d "$pkg" ] ; then
307
		pkg="$pkg/$(hostname).$suff"
308
	elif [ -z "$pkg" ]; then
309
		pkg="$PWD/$(hostname).$suff"
310 311
	fi

Natanael Copa's avatar
Natanael Copa committed
312
	tmppkg="$tmpdir/$(basename $pkg)"
313

Natanael Copa's avatar
Natanael Copa committed
314
	local tar_create="tar -c --no-recursion -T -"
Natanael Copa's avatar
Natanael Copa committed
315

316
	cd "${ROOT:-/}"
317
	# remove old package.list
318
	if [ -f etc/lbu/packages.list ] && [ -f var/lib/apk/world ]; then
319 320
		echo "Note: Removing /etc/lbu/packages.list." >&2
		echo "      /var/lib/apk/world will be used." >&2
321 322
		rm -f etc/lbu/packages.list
	fi
323
	# create tar archive
Natanael Copa's avatar
Natanael Copa committed
324 325 326 327
	if [ -n "$VERBOSE" ]; then
		echo "Archiving the following files:" >&2
		# we dont want to mess the tar output with the
		# password prompt. Lets get the tar output first.
Natanael Copa's avatar
Natanael Copa committed
328
		_gen_filelist | $tar_create -v > /dev/null
Natanael Copa's avatar
Natanael Copa committed
329
		rc=$?
Natanael Copa's avatar
Natanael Copa committed
330 331 332
	fi
	if [ $rc -eq 0 ]; then
		if [ -z "$ENCRYPTION" ]; then
Natanael Copa's avatar
Natanael Copa committed
333
			_gen_filelist | $tar_create -z >"$tmppkg"
Natanael Copa's avatar
Natanael Copa committed
334
			rc=$?
Natanael Copa's avatar
Natanael Copa committed
335
		else
Natanael Copa's avatar
Natanael Copa committed
336 337
			set -- enc "-$ENCRYPTION" -salt
			[ -n "$PASSWORD" ] && set -- "$@" -pass pass:"$PASSWORD"
Natanael Copa's avatar
Natanael Copa committed
338
			_gen_filelist | $tar_create -z \
Natanael Copa's avatar
Natanael Copa committed
339 340
				| $OPENSSL "$@" > "$tmppkg"
			rc=$?
341
		fi
342
	fi
Natanael Copa's avatar
Natanael Copa committed
343
	cd "$owd"
Natanael Copa's avatar
Natanael Copa committed
344

Natanael Copa's avatar
Natanael Copa committed
345
	# actually commit unless dryrun mode
Natanael Copa's avatar
Natanael Copa committed
346
	if [ $rc -eq 0 ]; then
347 348 349
		if [ -z "$DRYRUN" ]; then
			if [ "x$pkg" = "x-" ]; then
				cat "$tmppkg"
350 351
			elif [ -b "$pkg" ] || [ -c "$pkg" ]; then
				cat "$tmppkg" > "$pkg"
352
			else
Natanael Copa's avatar
Natanael Copa committed
353 354 355 356 357 358 359
				if cp "$tmppkg" "$pkg.new"; then
					mv "$pkg.new" "$pkg"
					rc=$?
				else
					rm -f "$pkg.new"
					rc=1
				fi
360 361
			fi
		fi
Natanael Copa's avatar
Natanael Copa committed
362
		[ $rc -eq 0 ] && vecho "Created $pkg"
Natanael Copa's avatar
Natanael Copa committed
363
	fi
364
	if [ -d "$LBU_POSTPACKAGE" ]; then
365
		run-parts "$LBU_POSTPACKAGE" >&2
366
	fi
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	return $rc
}

#
# lbu list - list files that would go to archive
#
usage_list() {
	echo "$PROGRAM $VERSION
Lists files that would go to tar package. Same as: 'lbu package -v /dev/null'

usage: $PROGRAM list|ls
"
	exit 1
}

cmd_list() {
383 384
	cmd_migrate_include_exclude
	_gen_filelist
385
}
Natanael Copa's avatar
Natanael Copa committed
386 387 388 389 390 391

#
# lbu_commit - commit config files to writeable media
#
usage_commit() {
	echo "$PROGRAM $VERSION
392 393 394
Create a backup of config to writeable media.

usage: $PROGRAM commit|ci [-nv] [<media>]
Natanael Copa's avatar
Natanael Copa committed
395 396

Options:
Natanael Copa's avatar
Natanael Copa committed
397
  -d	Remove old apk overlay files.
398
  -e	Protect configuration with a password.
Natanael Copa's avatar
Natanael Copa committed
399
  -n	Don't commit, just show what would have been commited.
Natanael Copa's avatar
Natanael Copa committed
400
  -p <password>	Give encryption password on the command-line
Natanael Copa's avatar
Natanael Copa committed
401 402 403
  -v	Verbose mode.

The following values for <media> is supported: floppy usb
Natanael Copa's avatar
Natanael Copa committed
404 405
If <media> is not specified, the environment variable LBU_BACKUPDIR or
LBU_MEDIA will be used. If LBU_BACKUPDIR is set, nothing will be mounted.
Natanael Copa's avatar
Natanael Copa committed
406

407
Password protection will use $DEFAULT_CIPHER encryption. Other ciphers can be
Natanael Copa's avatar
Natanael Copa committed
408 409 410
used by setting the DEFAULT_CIPHER or ENCRYPTION environment variables.
For possible ciphers, try: openssl -v

Natanael Copa's avatar
Natanael Copa committed
411 412
The password used to encrypt the file, can either be specified with the -p
option or using the PASSWORD environment variable.
413

Jeremy Thomerson's avatar
Jeremy Thomerson committed
414
The environment variables can also be set in $LBU_CONF
Natanael Copa's avatar
Natanael Copa committed
415 416 417 418 419
"
	exit 1
}

cmd_commit() {
420
	local media mnt statuslist tmplist
421 422 423 424
	local incl excl outfile ovls lines

	check_openssl

Natanael Copa's avatar
Natanael Copa committed
425 426 427
	# turn on verbose mode if dryrun
	[ -n "$DRYRUN" ] && VERBOSE="-v"

Natanael Copa's avatar
Natanael Copa committed
428 429 430 431 432
	mnt="$LBU_BACKUPDIR"
	if [ -z "$mnt" ]; then
		# find what media to use
		media="${1:-$LBU_MEDIA}"
		[ -z "$media" ] && usage_commit
Natanael Copa's avatar
Natanael Copa committed
433

Natanael Copa's avatar
Natanael Copa committed
434 435 436 437 438
		# mount media unles its already mounted
		mnt=/media/$media
		[ -d "$mnt" ] || usage
		mount_once_rw "$mnt" || die "failed to mount $mnt"
	fi
Natanael Copa's avatar
Natanael Copa committed
439

Natanael Copa's avatar
Natanael Copa committed
440 441 442 443 444 445 446
	# find the outfile
	outfile="$mnt/$(hostname).apkovl.tar.gz"
	if [ -n "$ENCRYPTION" ]; then
		outfile="$outfile.$ENCRYPTION"
	fi

	# remove old config files
447
	if [ -n "$DELETEOLDCONFIGS" ] ; then
Natanael Copa's avatar
Natanael Copa committed
448 449 450 451
		local rmfiles=$(ls "$mnt/"*.apkovl.tar.gz* 2>/dev/null)
		if [ -n "$rmfiles" ] ; then
			if [ -n "$VERBOSE" ]; then
				echo "Removing old apk overlay files:" >&2
452
				echo "$rmfiles"
Natanael Copa's avatar
Natanael Copa committed
453
				echo "" >&2
454
			fi
Natanael Copa's avatar
Natanael Copa committed
455
			[ -z "$DRYRUN" ] && rm "$mnt/"*.apkovl.tar.gz*
456 457
		fi
	else
458
		lines=$(ls -1 "$mnt"/*.apkovl.tar.gz* 2>/dev/null)
Natanael Copa's avatar
Natanael Copa committed
459
		if [ "$lines" = "$outfile" ]; then
Natanael Copa's avatar
Natanael Copa committed
460
			backup_apkovl "$outfile"
Natanael Copa's avatar
Natanael Copa committed
461
		elif [ -n "$lines" ]; then
462
			# More then one apkovl, this is a security concern
Natanael Copa's avatar
Natanael Copa committed
463
			cleanup
Natanael Copa's avatar
Natanael Copa committed
464 465 466
			eecho "The following apkovl file(s) were found:"
			eecho "$lines"
			eecho ""
467
			die "Please use -d to replace."
468
		fi
Natanael Copa's avatar
Natanael Copa committed
469
	fi
470 471 472

	# create package
	if ! cmd_package "$outfile"; then
Natanael Copa's avatar
Natanael Copa committed
473
		restore_apkovl "$outfile"
Natanael Copa's avatar
Natanael Copa committed
474
		cleanup
Natanael Copa's avatar
Natanael Copa committed
475
		die "Problems creating archive. aborting"
476
	fi
Natanael Copa's avatar
Natanael Copa committed
477

478 479
	# delete old backups if needed
	# poor mans 'head -n -N' done with awk.
480
	ls "$mnt"/$(hostname).[0-9][0-9][0-9][0-9]*[0-9].tar.gz 2>/dev/null \
481 482 483 484
		| awk '{ a[++i] = $0; } END {
			print a[0];
			while (i-- > '"${BACKUP_LIMIT:-0}"') {
				print a[++j]
485 486 487
			}
		}' | xargs rm 2>/dev/null

Natanael Copa's avatar
Natanael Copa committed
488
	# remove obsolete file. some older version of alpine needs this
489
	# to be able to upgrade
490 491
	if [ -z "$DRYRUN" ] && [ -f $mnt/packages.list ]; then
		echo "Note: Removing packages.list from $(basename $mnt)."
492
		echo "      /var/lib/apk/world will be used."
493 494
		rm -f $mnt/packages.list
	fi
Natanael Copa's avatar
Natanael Copa committed
495

Natanael Copa's avatar
Natanael Copa committed
496
	# make sure data is written
Natanael Copa's avatar
Natanael Copa committed
497
	sync
Natanael Copa's avatar
Natanael Copa committed
498
	[ "$media" = "floppy" ] && sleep 1
Natanael Copa's avatar
Natanael Copa committed
499 500

	# move current to commited.
501
	vecho "Successfully saved apk overlay files"
Natanael Copa's avatar
Natanael Copa committed
502 503 504 505 506 507 508
}

#---------------------------------------------------------------------------
# lbu_exclude - add remove file(s) from exclude list

usage_exclude() {
	echo "$PROGRAM $VERSION
Natanael Copa's avatar
Natanael Copa committed
509
Add filename(s) to exclude list ($sysconfdir/exclude)
510 511 512

usage: $PROGRAM exclude|ex|delete [-rv] <file> ...
       $PROGRAM exclude|ex|delete [-v] -l
Natanael Copa's avatar
Natanael Copa committed
513 514 515 516 517 518 519 520 521 522

Options:
  -l	List contents of exclude list.
  -r	Remove specified file(s) from exclude list instead of adding.
  -v	Verbose mode.
"
	exit 1
}

cmd_exclude() {
523
	cmd_migrate_include_exclude
Natanael Copa's avatar
Natanael Copa committed
524 525 526 527 528
	if [ "$LIST" ] ; then
		[ $# -gt 0 ] && usage_exclude
		show_exclude
		return
	fi
529

Natanael Copa's avatar
Natanael Copa committed
530 531
	[ $# -lt 1 ] && usage_exclude
	if [ "$REMOVE" ] ; then
532
		list_delete - "$@"
Natanael Copa's avatar
Natanael Copa committed
533
	else
534 535
		list_delete + "$@"
		list_add - "$@"
Natanael Copa's avatar
Natanael Copa committed
536 537 538 539
	fi
}

show_exclude() {
540
	if [ -f "$LBU_LIST" ] ; then
541
		vecho "Exclude files:"
542
		grep -- '^-' "$LBU_LIST" | sed 's/^-//'
543
	fi
Natanael Copa's avatar
Natanael Copa committed
544 545
}

Natanael Copa's avatar
Natanael Copa committed
546
#---------------------------------------------------------------------------
547
# lbu_listbackup - Show old commits
548
usage_listbackup() {
Natanael Copa's avatar
Natanael Copa committed
549 550 551 552
	cat <<EOF
$PROGRAM $VERSION
Show old commits.

553
usage: $PROGRAM list-backup [<media>]
Natanael Copa's avatar
Natanael Copa committed
554 555 556 557 558

EOF
	exit 1
}

559
cmd_listbackup() {
Natanael Copa's avatar
Natanael Copa committed
560
	local media=${1:-"$LBU_MEDIA"}
Natanael Copa's avatar
Natanael Copa committed
561 562
	local mnt="${LBU_BACKUPDIR:-/media/$media}"
	[ -z "$media" ] && [ -z "$LBU_BACKUPDIR" ] && usage_listbackup
Natanael Copa's avatar
Natanael Copa committed
563

Natanael Copa's avatar
Natanael Copa committed
564 565 566
	if [ -n "$media" ]; then
		mount_once "$mnt" || die "failed to mount $mnt"
	fi
Natanael Copa's avatar
Natanael Copa committed
567 568 569 570 571 572
	ls -1 "$mnt"/*.[0-9][0-9]*[0-9][0-9].tar.gz* 2>/dev/null | sed 's:.*/::'
}

#---------------------------------------------------------------------------
# lbu_revert - revert to old config
usage_revert() {
Natanael Copa's avatar
Natanael Copa committed
573 574 575 576 577 578
	cat <<EOF
$PROGRAM $VERSION
Revert to older commit.

usage: $PROGRAM revert <REVISION> [<media>]

Natanael Copa's avatar
Natanael Copa committed
579
The revision should be one of the files listed by 'lbu list-backup'.
Natanael Copa's avatar
Natanael Copa committed
580

Natanael Copa's avatar
Natanael Copa committed
581
EOF
Natanael Copa's avatar
Natanael Copa committed
582 583 584 585
}

cmd_revert() {
	local media=${2:-"$LBU_MEDIA"}
Natanael Copa's avatar
Natanael Copa committed
586 587 588 589 590 591
	[ -z "$media" ] && usage_revert
	local mnt="/media/$media"
	local revertto="$mnt/$1"
	local current="$mnt/$(hostname).apkovl.tar.gz"

	if [ -n "$ENCRYPTION" ]; then
592
		current="$current.$ENCRYPTION"
Natanael Copa's avatar
Natanael Copa committed
593
	fi
594
	mount_once_rw "$mnt" || die "failed to mount $mnt"
Natanael Copa's avatar
Natanael Copa committed
595
	[ -f "$revertto" ] || die "file not found: $revertto"
596
	backup_apkovl "$current"
Natanael Copa's avatar
Natanael Copa committed
597
	vecho "Reverting to $1"
598
	[ -z "$DRYRUN" ] && mv "$revertto" "$current"
Natanael Copa's avatar
Natanael Copa committed
599 600
}

Natanael Copa's avatar
Natanael Copa committed
601 602 603 604 605 606
#---------------------------------------------------------------------------
# lbu_status - check what files have been changed since last save
usage_status() {
	echo "$PROGRAM $VERSION
Check what files have been changed since last commit.

607
usage: $PROGRAM status|st [-av]
Natanael Copa's avatar
Natanael Copa committed
608 609 610 611 612 613 614 615

Options:
  -a	Compare all files, not just since last commit.
  -v	Also show include and exclude lists.
"
	exit 1
}

616 617 618 619 620
cmd_status() {
	if [ -n "$USE_DEFAULT" ]; then
		apk audit --backup
		return 0
	fi
621
	LBU_MEDIA=${1:-"$LBU_MEDIA"}
Natanael Copa's avatar
Natanael Copa committed
622
	[ -z "$LBU_MEDIA" ] && [ -z "$LBU_BACKUPDIR" ] && usage_status
623 624
	local tmp
	init_tmpdir tmp
625
	mkdir -p "$tmp/a" "$tmp/b"
626 627 628 629

	# unpack last commited apkovl to tmpdir/a
	unpack_apkovl "$tmp/a"

630
	# generate new apkovl and extract to tmpdir/b
631 632
	local save_encryption="$ENCRYPTION"
	ENCRYPTION=
633
	cmd_package - | tar -C "$tmp/b" -zx
634
	ENCRYPTION="$save_encryption"
635

636
	# show files that exists in a but not in b as deleted
637 638 639
	local f
	( cd "$tmp"/a && find ) | while read f; do
		f=${f#./}
640 641 642 643 644
		local b="$tmp/b/$f"
		if [ "$f" = "." ] || [ -e "$b" ] || [ -L "$b" ]; then
			continue
		fi
		echo "D $f"
645
	done
646

647
	# compare files in b with files in a
648 649 650 651 652
	( cd "$tmp"/b && find ) | while read f; do
		f=${f#./}
		[ "$f" = "." ] && continue
		local a="$tmp/a/$f"
		local b="$tmp/b/$f"
653
		if [ ! -e "$a" ] && [ ! -L "$a" ]; then
654
			echo "A $f"
Natanael Copa's avatar
Natanael Copa committed
655 656
		elif [ -f "$a" ] && [ -f "$b" ] && [ "$b" -nt "$a" ] \
		     && ! cmp -s "$a" "$b"; then
657 658 659
			echo "U $f"
		fi
	done
660 661
}

Natanael Copa's avatar
Natanael Copa committed
662

663 664 665 666 667 668 669 670 671 672 673 674
#-----------------------------------------------------------
# lbu_diff - run a diff against last commit
usage_diff() {
	echo "$PROGRAM $VERSION
Run a diff against last commit

usage: $PROGRAM diff [<media>]
"
	exit 1
}

cmd_diff() {
675
	local diff_opts=
676
	LBU_MEDIA=${1:-"$LBU_MEDIA"}
Natanael Copa's avatar
Natanael Copa committed
677
	[ -z "$LBU_MEDIA" ] && [ -z "$LBU_BACKUPDIR" ] && usage_diff
678 679 680 681
	local tmp
	init_tmpdir tmp
	mkdir -p "$tmpdir/a" "$tmp/b"
	unpack_apkovl "$tmp/a"
682
	ENCRYPTION=
683
	cmd_package - | tar -C "$tmp/b" -zx
684 685 686 687
	if diff --help 2>&1 | grep -q -- --no-dereference; then
		diff_opts="--no-dereference"
	fi
	cd "$tmp" && diff -ruN $diff_opts a b
688 689
}

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
# migrate
migrate_conf() {
	local pref="$1" conf="$2" line=
	echo "Note: Migrating  $conf to $LBU_LIST" >&2
	echo "# Automatically imported from $conf" >> "$LBU_LIST"
	while read line; do
		if [ "${line#'#'}" != "$line" ]; then
			# dont prefix comments
			echo "$line" >> "$LBU_LIST" || return 1
			continue
		fi
		case "$line" in
			[a-zA-z0-9._/]*) line="$pref$line";;
			*) continue;; # skip files with weird names
		esac
		if ! list_has "$line"; then
			echo "$line" >> "$LBU_LIST" || return 1
		fi
	done < "$conf"
	rm "$conf"
}

#-----------------------------------------------------------
# lbu migrate_config - migrate include/exclude to protected_paths.d
cmd_migrate_include_exclude() {
	if [ -e "$INCLUDE_LIST" ]; then
		migrate_conf + "$INCLUDE_LIST"
	fi
	if [ -e "$EXCLUDE_LIST" ]; then
		migrate_conf - "$EXCLUDE_LIST"
	fi
}
Natanael Copa's avatar
Natanael Copa committed
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738

#-----------------------------------------------------------
# Main

cmd=`echo "$PROGRAM" | cut -s -d_ -f2`
PROGRAM=`echo "$PROGRAM" | cut -d_ -f1`
if [ -z "$cmd" ] ; then
	cmd="$1"
	[ -z "$cmd" ] && usage
	shift
fi

# check for valid sub command
case "$cmd" in
	include|inc|add)	SUBCMD="include";;
	commit|ci)		SUBCMD="commit";;
	exclude|ex|delete)	SUBCMD="exclude";;
739 740
	list|ls)		SUBCMD="list";;
	package|pkg)		SUBCMD="package";;
Natanael Copa's avatar
Natanael Copa committed
741
	status|stat|st)		SUBCMD="status";;
742
	list-backup|lb)		SUBCMD="listbackup";;
Natanael Copa's avatar
Natanael Copa committed
743
	revert)			SUBCMD="revert";;
744
	diff)			SUBCMD="diff";;
745
	migrate_include_exclude) SUBCMD="migrate_include_exclude";;
Natanael Copa's avatar
Natanael Copa committed
746 747 748 749
	*)			usage;;
esac

# parse common args
750
while getopts "adehlM:np:qrv" opt ; do
Natanael Copa's avatar
Natanael Copa committed
751
	case "$opt" in
752
		a)	[ $SUBCMD = status ] || usage_$SUBCMD
Natanael Copa's avatar
Natanael Copa committed
753 754
			USE_DEFAULT="-a"
			;;
755 756
		d)	DELETEOLDCONFIGS="yes"
			;;
757
		e)	[ -z "$ENCRYPTION" ] && ENCRYPTION="$DEFAULT_CIPHER"
758
			;;
759
		h)	usage_$SUBCMD
Natanael Copa's avatar
Natanael Copa committed
760 761 762
			;;
		l)	LIST="-l"
			;;
763
		n)	[ $SUBCMD = commit ] || usage_$SUBCMD
Natanael Copa's avatar
Natanael Copa committed
764 765
			DRYRUN="-n"
			;;
766 767
		p)	PASSWORD="$OPTARG"
			;;
Natanael Copa's avatar
Natanael Copa committed
768 769 770 771
		q)	QUIET="$QUIET -q"
			;;
		r)	REMOVE="-r"
			;;
772
		v)	VERBOSE="$VERBOSE -v"
Natanael Copa's avatar
Natanael Copa committed
773 774 775 776
			;;
	esac
done
shift `expr $OPTIND - 1`
777

Natanael Copa's avatar
Natanael Copa committed
778
trap exit_clean SIGINT SIGTERM
779
cmd_$SUBCMD "$@"
Natanael Copa's avatar
Natanael Copa committed
780 781 782
retcode=$?

cleanup
Natanael Copa's avatar
Natanael Copa committed
783
exit $retcode