Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix + cosmetic changes #4

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
17 changes: 13 additions & 4 deletions README.pod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=head1 NAME

squashfu - an incremental backup solution
squashfu - an incremental backup solution (updated version for CIRCL TR-55)

=head1 SYNOPSIS

Expand Down Expand Up @@ -39,10 +39,9 @@ locations where the target is found will be presented.
Displays usage statistics, including the size of the compressed seed and each incremental
backup with its creation date and bin number.

=item B<-R>
=item B<-R> <number of bins>

Cookies will be persistent and reused for logins. If you specify this option, you must
also provide a cookie file path with the B<-C> option or in the config file.
Rollback specified number of backups and mount union for browsing. The rolled back data will be mounted at $UNION_MOUNT.

=item B<-U>

Expand All @@ -62,6 +61,11 @@ sourced, but options specified in this config will override the defaults. If you
config overrides the location of the backups, ensure that this config is always passed
for any operation.

=item B<-h>

Execute pre and post hooks during operations.
If this is a standard operation, it can be set in the configuration file via `HOOKS=true`

=back

=head1 HOW IT WORKS
Expand Down Expand Up @@ -139,6 +143,8 @@ incremental.

=head1 FURTHER READING

L<https://circl.lu/pub/tr-55/>

L<http://en.wikipedia.org/wiki/Aufs>

L<http://en.wikipedia.org/wiki/UnionFS>
Expand All @@ -158,3 +164,6 @@ B<aufs>(5), B<rsync>(1)

Dave Reisner E<lt>[email protected]<gt>

=head1 CONTRIBUTOR

Sascha Rommelfangen E<lt>[email protected]<gt>
81 changes: 65 additions & 16 deletions squashfu
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,48 @@ die () {
exit 1
}

execute_hooks () {
if [ "$HOOKS" == "true" ]
then
if [ $1 == "pre" ]
then
info "Executing pre-hooks"
for cmd in "${PREHOOKS[@]}"
do
$cmd
done
elif [ $1 == "post" ]
then
info "Executing post-hooks"
for cmd in "${POSTHOOKS[@]}"
do
$cmd
done
else
warn "Hooks called with wrong parameter"
fi
fi
}

create_new_squash () {
# Args: number of bins to be squashed, -1 on initial creation
# Returns: 0 on success, non-zero on failure

# If making first seed, create it directly from source
if [[ $1 -eq -1 ]]; then
info "Creating seed from sources (this may take a while)"
mksquashfs "${INCLUDES[@]}" "$SEED" -b 65536 -e "${EXCLUDES[@]}" >/dev/null
if [ ! -d "${INCLUDES[@]}" ]
then
info "The 'INCLUDES' ${INCLUDES[@]} directory doesn't exist."
info "Make sure the directory exists and is writeable by the user"
die "Cannot recover from missing source directory"
fi
if [ -z "${EXCLUDES[@]}" ]
then
mksquashfs "${INCLUDES[@]}" "$SEED" -b 65536 >/dev/null
else
mksquashfs "${INCLUDES[@]}" "$SEED" -b 65536 -e "${EXCLUDES[@]}" >/dev/null
fi
if [[ $? -eq 0 ]]; then
mount_squash
info "Seed creation finished. It has been mounted at "$SQUASH_MOUNT" if you would like to make sure the proper files are included"
Expand Down Expand Up @@ -74,7 +108,7 @@ create_new_bin () {
# Update binventory with new bin name and timestamp
echo "${1}:$(date +%s)" >> "$BINVENTORY"

# If write to bin list fails, remove diretory and exit
# If write to bin list fails, remove directory and exit
if [[ $? -ne 0 ]]; then
rmdir "$BINS_DIR/${1}"
die "Error writing to '$BINVENTORY'"
Expand Down Expand Up @@ -120,9 +154,8 @@ unmount_union () {
# Returns: return code from umount
info "Unmounting union"
umount "$UNION_MOUNT" 2>/dev/null
local ret=$?
sleep .5
return $ret
return $?
}

unmount_squash () {
Expand Down Expand Up @@ -184,16 +217,16 @@ action_backup () {
mkdir -p "$UNION_MOUNT" "$SQUASH_MOUNT" "$BINS_DIR"
touch "$BINVENTORY"
create_new_squash -1
exit 0
#exit 0
fi

info "Backup requested at $(date --rfc-3339=seconds)"

execute_hooks pre
# Cleanup union, in case user was doing a rollback and forgot to unmount (or error on last run)
mountpoint -q "$UNION_MOUNT" && unmount_union

info "Creating new bin"
# Make a new bin for this incremenetal
# Make a new bin for this incremental
get_next_available_bin
local new_bin=$?
create_new_bin $new_bin
Expand Down Expand Up @@ -223,7 +256,7 @@ action_backup () {
# TODO: Report if requested

unmount_all

execute_hooks post
info "Backup completed at $(date --rfc-3339=seconds)"
}

Expand All @@ -247,14 +280,15 @@ action_remove_bin () {
echo
[[ ! "$reply" = [Yy] ]] && die "Delete operation aborted"
fi

execute_hooks pre
mountpoint -q "$UNION_MOUNT" && { unmount_union || die "Failed to unmount union"; }
info "Deleting bin $1"
sed -i "/^$1:[0-9]*/d" "$BINVENTORY"
rm -rf "$BINS_DIR/$1"
else
die "Bin $1 not found."
fi
execute_hooks post
}

action_rollback () {
Expand All @@ -270,7 +304,7 @@ action_rollback () {
[[ $1 -gt ${#bin_list[@]} ]] && die "Cannot rollback more than ${#bin_list[@]} backups"

local num_to_mount=$(( ${#bin_list[@]} - $1 ))

execute_hooks pre
mountpoint -q "$UNION_MOUNT" && unmount_union
mountpoint -q "$SQUASH_MOUNT" || mount_squash

Expand All @@ -279,9 +313,9 @@ action_rollback () {
else
# voodoo magic: reverse bin_list and print off the top (#bin_list - $1) bins
mount_union_with_bins $(for (( i = ${#bin_list[@]} - $1 - 1; i >= 0; i-- )); do echo ${bin_list[i]}; done)
local rb_timestamp=$(grep "^${bin_list[@]:(-$num_to_mount):1}:" "$BINVENTORY" | cut -d: -f2)
local rb_timestamp=$(grep "^${bin_list[@]:(-$1-1):1}:" "$BINVENTORY" | cut -d: -f2)
fi

execute_hooks post
info "You have rolled back to $(date --rfc-3339=seconds --date="@$rb_timestamp")"
info "Your files can be found at '$UNION_MOUNT'"
}
Expand All @@ -291,11 +325,17 @@ action_report () {
# Enumerate bins, sort date order, print human readable create date and size

pushd "$BINS_DIR" &>/dev/null

if [ ! -f "$BINVENTORY" ]
then
info "No inventory file found at $BINVENTORY."
die "Statistics cannot be generated."
fi

declare -a DATA

# Collect all data into an array to 'preload' it. Index 0 is the entire
# folder. The following indicies correspond to the bin number of that index
# folder. The following indices correspond to the bin number of that index
printf "\n%30s\r" ".: Loading :." >&2
while read size bin; do
case $bin in
Expand Down Expand Up @@ -333,7 +373,9 @@ action_resquash_now () {
if [[ $number_of_bins -le $MIN_BINS ]]; then
die "Nothing to do. Current backups do not exceed MIN_BINS value."
else
execute_hooks pre
create_new_squash $(( $number_of_bins - $MIN_BINS ))
execute_hooks post
fi

exit $?
Expand All @@ -342,7 +384,7 @@ action_resquash_now () {
action_restore () {
[[ -z $1 ]] && die "Missing parameter from restore. Provide a full path to the restore target."
[[ $UID != 0 ]] && die "Must be root to restore."

execute_hooks pre
mount_squash || die "Failed to mount seed"

IFS=$'\n' read -r -d'\0' -a result < <(find "$BINS_DIR"/*/"$1" -maxdepth 0 2>/dev/null)
Expand Down Expand Up @@ -391,11 +433,13 @@ action_restore () {
} && info "Your data has been restored as: '$restore_name'"

unmount_all

execute_hooks post
}

action_unmount () {
execute_hooks pre
unmount_all
execute_hooks post
}

usage () {
Expand Down Expand Up @@ -439,14 +483,17 @@ OPTIONS
the default config. If you specify alternate locations via a supplmenetal config,
you will need to provide the config for all actions

-h
Execute pre and post hooks during operations

-v
Show debugging output.

HELP
exit 1
}

while getopts :BG:CD:QR:Uc:v opt; do
while getopts :BG:CD:QR:Uc:hv opt; do
case $opt in
B)
[[ -n $action ]] && die "only one operation may be used at a time"
Expand All @@ -473,6 +520,8 @@ while getopts :BG:CD:QR:Uc:v opt; do
[[ -f $OPTARG ]] && source $OPTARG ;;
v)
DEBUG=true ;;
h)
HOOKS=true;;
\:)
die "Argument missing from -$OPTARG"
usage ;;
Expand Down
11 changes: 11 additions & 0 deletions squashfu.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
# you need to troubleshoot.
DEBUG=false

# Execute pre and post hooks by default
HOOKS=false

# Use color in informational output
COLOR=true

Expand Down Expand Up @@ -70,4 +73,12 @@ EXCLUDES=(
/var/log
) #end excludes

# Hook commands need to be quoted if they contain spaces
PREHOOKS=(
"sudo /etc/init.d/samba stop"
id
) #end prehooks

POSTHOOKS=(
"sudo /etc/init.d/samba start"
) #end posthooks