Score:0

How to run remote crontabs?

tg flag

We want to run a certain backup process via one server, basically like the following:

Server 1 runs a process that copies data from Server 2 to the backup location.

Is there a script that I can write that would do this, whether it is scp, rsync or something else?

We would also want to overwrite the content in the destination location, and replace it with the updated data each time the crontab runs.

cn flag
"How to run remote crontabs?" that is a weird question. crontab executes based on a time/date added to the line INSIDE crontab. So there is no "remote running". There is not even a "runnng" on the local machine.
Score:1
cn flag

That does not require a script to activate. ssh can so what you want.

 ssh user@server2 "/script/backup.sh"

will run /script/backup.sh for "user" on "server2"

And that script can hold all the commands you need to backup, including copying the results over using rsync to another system (or the 1st system).

Score:0
za flag

Make password-less server connections

  • On the local machine generate ssh certificates: ssh-keygen
  • And register that machnie on destination/remote machine: ssh-copy-id username@destination

Backup command:

If you prefer overwriting the contents, the "overwriting" part has to be figured out too; but in a nutshell, need no archive-rotation software, you can use plain tar or scp for moving files, like this semi-oneliner.

(In order to fit that in one line, add ; like this: cd bak-this-dir;. One-liners are needed in crontabs.)

cd backup-this-directory
tar czvf - | ssh username@destination "cd backup-destination; dd | tar xzf -"

Or the other way round - if you want to upload files to your local machine:

cd backup-destination
ssh username@destination "cd backup-this-directory; tar czf -" | dd | tar xzvf -

Variants:

  • you could leave the file compressed, in which case dd could be your reveiving end with dd of=`date '+%F'`-backup.tgz
  • no need for z tar option if source is hard to compress (like videos) or bandwith is not a limitation.

Comments:

  • % characters have to be escaped in chron tab,
  • dd adds you the speed-meter to your transmission,
  • remember to cd into your appropriate directories, or tar will make a "tar-bomb" by possibly littering a homedir with many small files from backup source.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.