Home

Backup with anacron and nextcloud

For Linux users who run desktop PCs or laptops—machines that aren’t always powered on—the classic scheduling tool cron often misses critical backup jobs. The solution is Anacron, a utility designed specifically to run jobs that were missed while the system was off.

This guide explains how to use a simple Bash script and anacron to reliably move important files, like configuration files or browser data, to your Nextcloud synchronization directory every week.

Prerequisites

  1. A working Linux system (e.g., Ubuntu, Debian, Fedora, Arch).
  2. Your Nextcloud directory is correctly set up (e.g., /home/wga/Nextcloud).
  3. The necessary permissions to create and edit files in system directories (using sudo).

Step 1: Create the Bash Script

We will use a simple, well-commented Bash script to perform the copy operation. For safety and clarity, we will use the $HOME variable, which automatically points to the current user’s home directory.

File: config_backup.sh

#!/bin/bash

# --- Automated Configuration File Backup Script ---

# 1. Define the Destination Directory
# NOTE: The trailing slash (/) is used to explicitly tell the 'cp' command
# that this variable points to a directory. This prevents accidental renaming.
DEST_DIR="/home/wga/Nextcloud/dotfiles/"

# Create the destination directory if it doesn't exist yet
mkdir -p "$DEST_DIR"

# 2. Define the Source Files (The 'dotfiles' in your home directory)
# The '~' (tilde) is a shorthand for the current user's home directory.
FILES=(
    ~/.bashrc      # Bash shell configuration
    ~/.zshrc       # Zsh shell configuration
    ~/.gitconfig   # Git user settings
    ~/.vimrc       # Vim text editor settings
)

# 3. Loop Through and Copy Each File
echo "Starting configuration file backup to: $DEST_DIR"

# Loop over the list of files defined above
for FILE_PATH in "${FILES[@]}"; do
    
    # Check if the file exists before trying to copy it
    if [ -f "$FILE_PATH" ]; then
        # 'cp' command copies the file.
        # -v stands for 'verbose', which prints the file name being copied (helpful!).
        cp -v "$FILE_PATH" "$DEST_DIR"
    else
        # If the file doesn't exist, report it.
        echo "Warning: File not found, skipping: $FILE_PATH"
    fi
done

echo "---"
echo "Backup complete. Files are now syncing via Nextcloud."

Step 2: Integrate the Script with Anacron

For jobs that should run periodically (daily, weekly, monthly) regardless of when the PC is turned on, Linux uses a system where Anacron manages certain directories.

We will place our script in the system’s weekly job directory.

2.1. Copy the Script to the Weekly Folder

Use the sudo command to copy your script to the system’s designated weekly directory.

# Assuming your script is in your current directory:
sudo cp config_backup.sh /etc/cron.weekly/

# Rename it to something descriptive without the .sh extension
sudo mv /etc/cron.weekly/config_backup.sh /etc/cron.weekly/nextcloud-dotfile-backup

2.2. Make the Script Executable

The script must have execute permissions for Anacron to run it.

sudo chmod +x /etc/cron.weekly/nextcloud-dotfile-backup

Step 3: Verify and Understand the Schedule

The job is now fully set up. You do not need to edit the crontab -e file for this system.

How Anacron Ensures Success

  1. System Check: When your PC boots up, the Anacron utility runs automatically.
  2. Time Check: It looks at its log files (usually in /var/spool/anacron/) to see when the last weekly job was completed.
  3. Run Missed Jobs: If more than 7 days have passed since the last weekly run, Anacron will wait a short randomized delay (to avoid slowing down startup) and then execute every script inside /etc/cron.weekly/—including your nextcloud-dotfile-backup script.

This guarantees that your files will be backed up once per week, even if your computer is shut down for a few days during the scheduled run time.

Tags: Backup, Linux, Bash