---
title: Comprehensive Guide to Automating Daily Kubernetes Directory Backups to a NAS using Rsync and Cron Jobs
created: 2025-09-29
updated: 2025-09-29
type: summary
tags: ["backup", "cron", "notes", "rsync", "telegram"]
external: http://go/ui/posts/1323/
---

Of course. You can set this up by adding a single line to your server's crontab.

The cron job you need to add is:

```bash
5 2 * * * rsync -avh --delete /mnt/k8s/ rsync://junv@192.168.1.4/backups/192.168.1.2 >> /var/log/rsync_k8s_backup.log 2>&1
```

-----

### Step-by-Step Guide

Here’s how to get it working and what the command does.

#### 1\. Test the Command Manually

Before automating, it's crucial to run the `rsync` command manually from your server's terminal to ensure it works as expected.

First, perform a "dry run" which shows you what files *would* be synced without actually changing anything.

```bash
rsync -avhn --delete --progress /mnt/k8s/ rsync://junv@192.168.1.4/backups/192.168.1.2
```

  * The `-n` (or `--dry-run`) flag is the key here.
  * `--progress` shows you the transfer progress, which is helpful for manual runs.

If the output looks correct, run it for real without the `-n` and `--progress` flags:

```bash
rsync -avh --delete /mnt/k8s/ rsync://junv@192.168.1.4/backups/192.168.1.2
```

This will likely prompt you for the password for the `junv` user on your NAS. For a cron job to work, you'll need to set up passwordless authentication (see "Important Considerations" below).

-----

### 2\. Set Up the Cron Job

Once the command works manually, add it to your crontab.

1.  Open the crontab editor:

    ```bash
    crontab -e
    ```

    If it's your first time, you may be asked to choose a text editor (like `nano`).

2.  Add the line to the file. This example will run the job every day at 2:05 AM.

    ```bash
    5 2 * * * rsync -avh --delete /mnt/k8s/ rsync://junv@192.168.1.4/backups/192.168.1.2 >> /var/log/rsync_k8s_backup.log 2>&1
    ```

3.  Save the file and exit the editor. The cron job is now active.

-----

### Command Breakdown

Here's a breakdown of the cron line so you know exactly what it's doing.

  * `5 2 * * *`: This is the schedule. It means **run at minute 5 of hour 2 (2:05 AM) every day, every month, every day of the week.**
  * `rsync -avh --delete`: This is the sync command and its options.
      * `-a` (archive): A magic flag that preserves permissions, ownership, timestamps, and syncs recursively.
      * `-v` (verbose): Provides more detailed output, which is useful for the log file.
      * `-h` (human-readable): Shows file sizes in a friendly format (e.g., KB, MB, GB).
      * `--delete`: **This is important.** It deletes any files on the destination NAS that no longer exist in the source (`/mnt/k8s/`). This makes the destination a true mirror.
  * `/mnt/k8s/`: The source directory. The trailing slash `/` is significant—it means "copy the **contents** of this directory," not the directory itself.
  * `rsync://junv@192.168.1.4/...`: Your destination NAS.
  * `>> /var/log/rsync_k8s_backup.log 2>&1`: This handles logging. It appends all output (both standard and error messages) to the specified log file, so you can check later if the backup ran successfully. 📜

-----

### Important Considerations

  * **Passwordless Authentication**: Cron jobs run in the background and can't ask for a password. You must set up a way for your server to authenticate with the NAS automatically. A common method is to use the `RSYNC_PASSWORD` environment variable. You can create a small script that sets the variable and then calls `rsync`, and have cron call that script instead.

    For example, create a script named `/usr/local/bin/backup_script.sh`:

    ```bash
    #!/bin/bash
    export RSYNC_PASSWORD="your_nas_password"
    rsync -avh --delete /mnt/k8s/ rsync://junv@192.168.1.4/backups/192.168.1.2
    ```

    Make it executable (`chmod +x /usr/local/bin/backup_script.sh`), and then your cron line would be:

    ```bash
    5 2 * * * /usr/local/bin/backup_script.sh >> /var/log/rsync_k8s_backup.log 2>&1
    ```

  * **Log Rotation**: Your log file will grow indefinitely. You should configure `logrotate` to manage it for you, preventing it from filling up your disk.
