#!/usr/bin/env bash
VERSION='v1.4.5'

[ -z $NARADA_SKIP_LOCK ] && exec narada-lock-exclusive $0 "$@"


# compatibility with Narada before 0.9
[ -d config/backup ] || mkdir config/backup || exit 1
[ -f config/backup/exclude ] || cat >config/backup/exclude <<'EOF' || exit 1
./var/.lock.new
./tmp/*
./var/backup/*
./var/patch/.prev/*
EOF
# compatibility with Narada before 1.2.0
perl -i -pe 's{var/patch/prev/}{var/patch/.prev/}' config/backup/exclude


snap='var/backup/snap'
full='var/backup/full.tar'
incr='var/backup/incr.tar'
excl=(); while read; do excl+=( --exclude "$REPLY" ); done <config/backup/exclude


_tar(){
    tmp=/tmp/narada-backup.$$
    TAR=`which gtar tar 2>/dev/null | head -n 1`
    LANG= $TAR "$@" &>$tmp
    status=$?
    grep -v 'socket ignored' $tmp
    rm $tmp
    return $status
}

narada-mysqldump                                    &&
if [ -f $snap -a -f $full ]; then
    mv $full $full.tmp                              &&
    _tar -c -g $snap "${excl[@]}" -f $incr.tmp ./   &&
    mv $incr.tmp $incr                              &&
    _tar -g /dev/null -A $incr -f $full.tmp
else
    rm -f $snap $full $full.tmp $incr $incr.tmp     &&
    _tar -c -g $snap "${excl[@]}" -f $full.tmp ./
fi                                                  &&
mv $full.tmp $full


exit $?
: <<'=cut'

=encoding utf8

=head1 NAME

narada-backup - prepare consistent project backup


=head1 VERSION

This document describes narada-backup version v1.4.5


=head1 USAGE

    narada-backup


=head1 DESCRIPTION

At first run prepare full project backup in 'var/backup/full.tar'.
On next runs prepare incremental backup in 'var/backup/incr.tar' plus
append it to full backup in 'var/backup/full.tar' (using tar -A).

Because of used technique of quick full backup update, full backup will
grow on each run. To keep file with full backup on reasonable size, you
should force full backup from time to time.

To force full backup you should remove 'var/backup/full.tar' before running
narada-backup.

Both full and incremental backups should be unpacked as incremental tar
archives, see http://www.gnu.org/software/automake/manual/tar/Incremental-Dumps.html.

Before preparing backup will run narada-mysqldump.

Files and directories listed in config/backup/exclude (use syntax suitable
for tar's --exclude option) will not be included in backup.

Will exit with non-zero exit status if backup process failed.

Script must be executed only from "project root directory".


=head1 CONFIGURATION AND ENVIRONMENT

Use files in var/backup/.


=head1 SUPPORT

=head2 Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker
at L<https://github.com/powerman/Narada/issues>.
You will be notified automatically of any progress on your issue.

=head2 Source Code

This is open source software. The code repository is available for
public review and contribution under the terms of the license.
Feel free to fork the repository and submit pull requests.

L<https://github.com/powerman/Narada>

    git clone https://github.com/powerman/Narada.git

=head2 Resources

=over

=item * MetaCPAN Search

L<https://metacpan.org/search?q=Narada>

=item * CPAN Ratings

L<http://cpanratings.perl.org/dist/Narada>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Narada>

=item * CPAN Testers Matrix

L<http://matrix.cpantesters.org/?dist=Narada>

=item * CPANTS: A CPAN Testing Service (Kwalitee)

L<http://cpants.cpanauthors.org/dist/Narada>

=back


=head1 AUTHOR

Alex Efros E<lt>powerman@cpan.orgE<gt>


=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2008-2015 by Alex Efros E<lt>powerman@cpan.orgE<gt>.

This is free software, licensed under:

  The MIT (X11) License


=cut