App-FileSync
view release on metacpan or search on metacpan
lib/App/FileSync.pm view on Meta::CPAN
Two machines are involved: the server, which holds the master, and each
client, which holds a copy. Install App::FileSync on both - C<cpanm
App::FileSync>, say - because C<filesync-admin> and C<filesync-server> run on
the server and C<filesync> on the clients.
The example keeps the master in the server account's home directory, so
nothing has to run as root. F</srv/filesync> is the built-in default root;
"A root of /srv/filesync instead" below says what changes if you prefer it.
=head2 On the server
Create the repo as the account the clients log in to. A session over ssh
carries only the repo, the client id and the role, so the client never tells
the server which root to use: name the same C<--root> on every server command.
# on the server, once
filesync-admin --root ~/filesync repo create documents
filesync-admin --root ~/filesync client allow laptop --repo documents
C<repo create> makes the master directory and the metadata beside it, printing
both paths. C<client allow> is optional, because a repo with no clients
admits every client, and it takes the client's id: the host name unless
C<--id> was given.
The server also needs C<--root> when C<filesync-server> starts, and the client
cannot pass one, so wrap it:
# on the server, once
cat > ~/bin/filesync-server-home <<'EOF'
#!/bin/sh
exec /usr/local/bin/filesync-server --root "$HOME/filesync" "$@"
EOF
chmod +x ~/bin/filesync-server-home
Use the path C<filesync-server> was actually installed to, which
C<ssh srv 'command -v filesync-server'> shows: a non-interactive ssh command
can see a shorter PATH than an interactive login shell.
=head2 On each client
Register the directory and name the wrapper as the server program:
# on the client, once
filesync init --repo documents --root /home/foo/Documents \
--server foo@srv \
--server-command /home/foo/bin/filesync-server-home
filesync sync --repo documents --dry-run
filesync sync --repo documents
Nothing needs naming: the client reports its host name, which is the id
C<client allow> lists and the name the server keeps this client's base, its
journal entries and its conflict copies under. Pass C<--id> only when the
host name is not the name you want, and allow that name on the server
instead. An id is not a login - the ssh key and the account do the
authenticating - and it does not divide the repo: every client of a repo
syncs the whole tree.
ssh runs with C<BatchMode=yes>, so the client's key must already be in the
server account's F<authorized_keys>, or loaded in C<ssh-agent>: cron cannot
answer a password prompt.
=head2 From cron
# crontab -e, on each client
* * * * * ionice -c3 /usr/local/bin/filesync sync --repo documents --quiet >/dev/null
The summary line is dropped so cron does not mail on every successful run;
diagnostics still go to standard error, so a failure is mailed. Every attempt,
successful or not, is also appended to F<~/.filesync/logs/documents.jsonl>, and
C<filesync log --repo documents --exit 1> lists the ones that failed.
=head2 On Windows
Windows clients use a scheduled task rather than cron. With the script in
F<C:\filesync> and Strawberry Perl installed, the same job is set up from a
command prompt:
REM on the client, once
C:\Strawberry\perl\bin\perl.exe C:\filesync\bin\filesync init ^
--repo documents --root C:\Users\foo\Documents --server foo@srv ^
--server-command /home/foo/bin/filesync-server-home
REM then create the task; it runs every minute as the current user
schtasks /Create /SC MINUTE /MO 1 /TN FileSync /F ^
/TR "C:\Strawberry\perl\bin\perl.exe C:\filesync\bin\filesync sync --repo documents --quiet"
The task runs only while that user is logged on, which suits a Documents
folder; its run log is still written under F<%USERPROFILE%\.filesync\logs>, so
C<filesync log --repo documents --warnings> works there too.
=head2 A root of /srv/filesync instead
To keep the master outside a home directory, create the default root once for
the account the clients log in to. Then no C<--root> is needed on the server
commands, and no C<--server-command> on the client:
# on the server, as an administrator
sudo mkdir -p /srv/filesync
sudo chown -R foo /srv/filesync
C<filesync-admin repo create documents> works as it is, and the master is
F</srv/filesync/master/documents>.
=head2 Backup and restore
The master directory is an ordinary directory tree, so it can be mirrored to a
backup directory from the server account's crontab. Make the backup directory
once - rsync creates the last component only - then let cron keep it in step:
# on the server, once
mkdir -p /home/foo/backups/documents
# crontab -e, on the server
* * * * * ionice -c3 rsync -a --delete --exclude '.filesync-tmp-*' \
/home/foo/filesync/master/documents/ /home/foo/backups/documents/
Filesync writes each file to a temporary name and renames it into place, so the
mirror never catches a half-written upload; the exclusion keeps the temporary
files themselves out of the backup. C<--delete> makes the copy a mirror: a
deletion that reaches the master also removes the backed-up file.
( run in 1.952 second using v1.01-cache-2.11-cpan-f5f21f1076c )