2020-05-17 14:52:30 +02:00
# Cron
2023-10-02 14:10:51 +02:00
Often it is useful to trigger backups automatically. For this, we can specify a `cron` attribute to each location.
2020-05-17 14:52:30 +02:00
```yaml | .autorestic.yml
locations:
my-location:
from: /data
to: my-backend
cron: '0 3 * * 0' # Every Sunday at 3:00
```
2023-10-02 14:10:51 +02:00
Here is an awesome website with [some examples ](https://crontab.guru/examples.html ) and an [explorer ](https://crontab.guru/ ).
2020-05-17 14:52:30 +02:00
2020-05-17 18:56:14 +02:00
## Installing the cron
2023-10-02 14:10:51 +02:00
**This has to be done only once, regardless of how many cron jobs you have in your config file.**
2020-06-28 22:48:00 +02:00
2021-04-21 09:34:01 +02:00
To actually enable cron jobs you need something to call `autorestic cron` on a timed schedule.
Note that the schedule has nothing to do with the `cron` attribute in each location.
2023-10-02 14:10:51 +02:00
My advice would be to trigger the command every 5min, but if you have a cronjob that runs only once a week, it's probably enough to schedule it once a day.
2020-05-17 18:56:14 +02:00
### Crontab
Here is an example using crontab, but systemd would do too.
First, open your crontab in edit mode
```bash
crontab -e
```
2021-04-27 01:22:32 +02:00
Then paste this at the bottom of the file and save it. Note that in this specific example the config file is located at one of the default locations (e.g. `~/.autorestic.yml` ). If your config is somewhere else you'll need to specify it using the `-c` option.
2020-05-17 18:56:14 +02:00
```bash
2020-06-28 22:33:15 +02:00
# This is required, as it otherwise cannot find restic as a command.
PATH="/usr/local/bin:/usr/bin:/bin"
2020-06-28 22:36:44 +02:00
# Example running every 5 minutes
2021-10-25 17:07:29 +02:00
*/5 * * * * autorestic -c /path/to/my/.autorestic.yml --ci cron
2020-05-17 18:56:14 +02:00
```
2020-11-13 15:48:20 +01:00
> The `--ci` option is not required, but recommended
2021-10-25 17:07:29 +02:00
To debug a cron job you can use
```bash
*/5 * * * * autorestic -c /path/to/my/.autorestic.yml --ci cron > /tmp/autorestic.log 2>& 1
```
2020-11-13 15:48:20 +01:00
Now you can add as many `cron` attributes as you wish in the config file ⏱
2020-05-17 18:56:14 +02:00
2023-10-02 14:10:51 +02:00
> Also note that manually triggered backups with `autorestic backup` will not influence the cron timeline, they are intentionally not linked.