App-FileSync

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

The example keeps the master in the server account's home directory, so
nothing has to run as root.  `/srv/filesync` is the built-in default root;
"A root of /srv/filesync instead" below says what changes if you prefer it.

### 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 `--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
```

`repo create` makes the master directory and the metadata beside it, printing
both paths.  `client allow` is optional, because a repo with no clients
admits every client, and it takes the client's id: the host name unless
`--id` was given.

The server also needs `--root` when `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 `filesync-server` was actually installed to, which
`ssh srv 'command -v filesync-server'` shows: a non-interactive ssh command
can see a shorter PATH than an interactive login shell.

### 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
`client allow` lists and the name the server keeps this client's base, its
journal entries and its conflict copies under.  Pass `--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 `BatchMode=yes`, so the client's key must already be in the
server account's `authorized_keys`, or loaded in `ssh-agent`: cron cannot
answer a password prompt.

### 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 `~/.filesync/logs/documents.jsonl`, and
`filesync log --repo documents --exit 1` lists the ones that failed.

### On Windows

Windows clients use a scheduled task rather than cron.  With the script in
`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 `%USERPROFILE%\.filesync\logs`, so
`filesync log --repo documents --warnings` works there too.

### 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 `--root` is needed on the server
commands, and no `--server-command` on the client:

```
    # on the server, as an administrator
    sudo mkdir -p /srv/filesync
    sudo chown -R foo /srv/filesync
```

`filesync-admin repo create documents` works as it is, and the master is
`/srv/filesync/master/documents`.

### 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



( run in 1.185 second using v1.01-cache-2.11-cpan-f5f21f1076c )