I would like to scan for my /tmp/
directory now and then and check what files have been changed since I last time did it. All files that have been changed should be copied to remote server (as a backup), perserving the directory structure. I tried something like this, but it's not quite working on Ubuntu, throwing Permission denied (publickey).
#!/bin/bash
TARGET=/bkp
[email protected]
SOURCE=/tmp
touch $TARGET/last_sync
rsync \
-ahrv \
--update \
-e \
--files-from=<ssh -i /home/ubuntu/ssh_key.pem $HOST "find $SOURCE -type f -newer $SOURCE/last_sync -exec realpath --relative-to=$SOURCE '{}' \;" \
$HOST:$SOURCE \
$TARGET
rsync -ahv $TARGET/last_sync $HOST:$SOURCE
What I had in plan is to have on a source machine:
ubuntu@source:~/$ tree /tmp
.
├── file1.txt
├── dir1
│ ├── subdir1
│ │ └── file3.txt
│ └── file2.txt
├── file4.txt
and on the first run it should be copied on a remote machine as
ubuntu@remote:~/$ tree /bkp
.
├── file1.txt
├── dir1
│ ├── subdir1
│ │ └── file3.txt
│ └── file2.txt
├── file4.txt
Several hours later, new file /tmp/dir1/file5.txt
was added, and file /tmp/file4.txt
was changed
After the script was executed, it should be synced (added both files) on remote:
ubuntu@remote:~/$ tree /bkp
.
├── file1.txt
├── dir1
│ ├── subdir1
│ │ └── file3.txt
│ └── file2.txt
| | -- file5.txt <-- ADDED
├── file4.txt <-- ADDED (replaced)
I was able to achieve something similar on macos -> ubuntu, but with scp and more "logic" behind it, was checking files changed in the last N hours and running the script every N hours..
/tmp
should not hold any data that warrants making back-ups in the first place. Second: allowing rsync to discover which files it needs to copy to keep source <--> destination synchronised is usually better than trying to be smart about it withfind
. - And all of that is completely redudant to what your error is about.Permission denied (publickey)
is an error you get when public key authentication is not set up correctly. Debug with the ssh client set to more verbose output and check the server side sshd logs for errors/warnings.