3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation. #
8 # This program is distributed in the hope that it will be useful, but #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
11 # General Public License for more details. #
13 # You should have received a copy of the GNU General Public License #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
16 # Written by and Copyright (C) Francois Fleuret #
17 # Contact <francois@fleuret.org> for comments & bug reports #
18 #########################################################################
23 if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
25 Usage: rsync-luks.sh <source file> <dest file>
27 Mounts both files as LUKS volumes and rsyncs the source to the
28 dest. It first performs a dry-run and then asks for interactive
29 confirmation before synchronizing for real.
31 Comments and bug reports to francois@fleuret.org
37 [[ -f "$1" ]] && [[ -f "$2" ]] || (echo "$0 <source> <dest>" >&2 && exit 1)
39 [[ -e "/dev/mapper/crypt-src" ]] && (echo "/dev/mapper/crypt-src already exists." >&2 && exit 1)
41 [[ -e "/dev/mapper/crypt-dst" ]] && (echo "/dev/mapper/crypt-dst already exists." >&2 && exit 1)
43 function exit_handler () {
45 [[ -n "${VOL_SRC+yes}" ]] && umount "${VOL_SRC}" && rmdir "${VOL_SRC}" && unset VOL_SRC
46 [[ -e "/dev/mapper/crypt-src" ]] && cryptsetup luksClose crypt-src
47 [[ -n "${LOOP_SRC+yes}" ]] && losetup -d "${LOOP_SRC}" && unset LOOP_SRC
49 [[ -n "${VOL_DST+yes}" ]] && umount "${VOL_DST}" && rmdir "${VOL_DST}" && unset VOL_DST
50 [[ -e "/dev/mapper/crypt-dst" ]] && cryptsetup luksClose crypt-dst
51 [[ -n "${LOOP_DST+yes}" ]] && losetup -d "${LOOP_DST}" && unset LOOP_DST
55 trap exit_handler EXIT SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM
57 ######################################################################
60 LOOP_SRC="$(losetup -f)"
61 losetup "${LOOP_SRC}" "$1"
62 cryptsetup luksOpen "${LOOP_SRC}" crypt-src
63 VOL_SRC="$(mktemp -d /tmp/sync-luks.XXXXXX)"
64 mount -o ro /dev/mapper/crypt-src "${VOL_SRC}"
66 LOOP_DST="$(losetup -f)"
67 losetup "${LOOP_DST}" "$2"
68 cryptsetup luksOpen "${LOOP_DST}" crypt-dst
69 VOL_DST="$(mktemp -d /tmp/sync-luks.XXXXXX)"
70 mount /dev/mapper/crypt-dst "${VOL_DST}"
72 ######################################################################
73 # First, show the changes
75 echo "**********************************************************************"
78 rsync -n --itemize-changes --delete --progress -axz "${VOL_SRC}/" "${VOL_DST}/"
80 ######################################################################
81 # Ask for confirmation and synchronize
83 echo "**********************************************************************"
84 echo "* Press 'y' to synchronize, anything else to cancel."
88 if [[ "${KEY}" == "y" ]]; then
89 rsync --itemize-changes --delete --progress -axz "${VOL_SRC}/" "${VOL_DST}/"
91 echo "No synchronization."
94 ######################################################################
96 echo "**********************************************************************"