Replicating Kirby content with rsync and cron
For all of my Kirby projects the content folder usually is excluded from the Git repository, and hence the content does not get deployed with new releases. To create a development preview that always features the latest content from the production site, I recently set up a cron job that replicates the Kirby content directory from its production location to the develop environment.
This is a simple one-liner which makes working with the development site more convenient, because any new features or bugfixes can be previewed and tested with the latest content from the live site.
For the replication to happen, I’m using a simple shell script which then gets called via a cron job at a certain time interval. I think case I’m running the script once a day, but it’s possible to tweak the cron job interval to your own liking and requirements.
To copy the content folder I’m using
rsync (documentation), which is an open-source utility for transferring and synchronizing files.
The simple syntax for the command is:
rsync [OPTION...] SRC... [DEST]
Since cron will be calling the
rsync command as a shell script, the script starts with a hashbang.
To copy the complete
content directory with all of its content, it's required to use the
--recursive to recurse into directories) option.
The complete script then looks like this:
#!/bin/sh rsync -r /production.domain.tld/content /develop.domain.tld/
The last step then is to save the script on the server (e.g. as “replicate-content.sh”), set its permissions to be executable and call it with a cron job. Many hosting plans come with an interface to create and configure cron jobs, otherwise it’s neccessary to create one from scratch.
As a result, the above script will be automatically executed at the set interval (e.g. every day at midnight) and copy the complete content directory and all of its subdirectories to the development server.