Community-driven coverage of elementary OS — news, guides & forums
Dark abstract low-poly geometric landscape with layered triangular facets in deep navy, charcoal, and muted teal, illuminated by a soft electric-blue glow near the horizon

News roundups, tutorials, application guides, and forums — built by users, for users of the elegant Linux distribution.

elementary weekly
#20
Latest roundup · 18 Apr 2015
Freya Release
Final
Covered in weekly #19 & #20
Forum Topics
Active
Installation, customization & more

Set Up Automatic Backups with Cron on elementary OS

A reliable backup routine protects your documents, photos, schoolwork and configuration files when a laptop fails or a mistake removes an important folder. On elementary OS, cron provides a lightweight way to run a command at a set time without opening a backup application each day.

This approach works well with an external USB hard drive or SSD. It can also copy files to a mounted network location, although a local disk is usually easier to configure and less dependent on your home network. The example below uses rsync, a well-tested Linux utility that copies only changed files after the first run.

A cron schedule is particularly useful for a desktop that stays powered on overnight. It needs a little extra care on a laptop, because cron does not normally run a missed task when the computer is suspended. A machine that is asleep at 2:30 am may wait until the next scheduled day.

Australian users should also consider the practical conditions around their equipment. A drive left beside a laptop in a Brisbane storm, a Melbourne power outage or a bushfire-affected area is not a complete backup strategy. Automatic local copies are an excellent first layer, while a second copy stored elsewhere provides stronger protection.

Decide What the Backup Should Protect

Start by selecting the folders that contain irreplaceable data. Common choices include Documents, Pictures, Videos, Music, Downloads, and project directories stored directly in your home folder. Browser profiles, application settings and SSH keys may also matter, but copying the entire home directory can include caches and temporary files that waste space.

This guide creates a mirror of selected folders on an external drive. A mirror contains the current version of each file and is convenient when you need to restore a document quickly. It is not the same as versioned backup: if you delete a file and the next run removes it from the mirror, that older copy will no longer be available.

Connect the backup drive and open Files to see where elementary OS mounted it. Removable volumes commonly appear under /media/your-username/DriveName. Linux filesystems such as ext4 are a good choice if the drive will only be used with Linux computers. exFAT is more convenient when the disk must also work with Windows or macOS, but it does not preserve Linux ownership and permissions in the same way.

For example, a drive labelled BackupSSD mounted for a user called alex will usually be available at:

/media/alex/BackupSSD

The exact username and volume label will differ on your computer. Use the path shown by Files rather than copying this example unchanged.

Create a Backup Script

A script is easier to test and maintain than placing a long rsync command directly in the crontab. Create a personal bin directory and open a new file:

mkdir -p "$HOME/bin"
nano "$HOME/bin/elementary-backup.sh"

Paste the following script, replacing BackupSSD if your drive uses another label:

#!/usr/bin/env bash
set -Eeuo pipefail

BACKUP_MOUNT="/media/$USER/BackupSSD"
DEST="$BACKUP_MOUNT/elementary-home"
LOG_DIR="$HOME/.local/state"
LOG_FILE="$LOG_DIR/elementary-backup.log"

mkdir -p "$LOG_DIR"
exec >> "$LOG_FILE" 2>&1

printf '\n%s\n' "Backup started: $(date --iso-8601=seconds)"

if ! /usr/bin/mountpoint -q "$BACKUP_MOUNT"; then
    echo "Backup drive is not mounted: $BACKUP_MOUNT"
    exit 1
fi

mkdir -p "$DEST"

for folder in Documents Pictures Videos Music Downloads; do
    if [ -d "$HOME/$folder" ]; then
        /usr/bin/rsync -a --delete \
            "$HOME/$folder/" \
            "$DEST/$folder/"
    fi
done

echo "Backup completed: $(date --iso-8601=seconds)"

Save the file with Ctrl+O, press Enter, then exit nano with Ctrl+X. The --delete option makes each destination folder match its source. This keeps the backup tidy, but it also means that a deleted source file can disappear from the backup during the next run. Remove --delete if you prefer old files to remain on the destination, or use a versioned backup system when historical copies are important.

Make the script executable and run it manually:

chmod u+x "$HOME/bin/elementary-backup.sh"
"$HOME/bin/elementary-backup.sh"

The first run may take some time because it copies every selected file. Later runs should be faster because rsync compares file size and modification time before transferring data. Check the destination in Files, then inspect the log with:

cat "$HOME/.local/state/elementary-backup.log"

If the drive is not mounted, the script exits without copying anything. That safeguard is important: it prevents a command from accidentally creating a folder on your internal disk when the external drive is unplugged.

Add the Cron Schedule

Open your personal crontab with:

crontab -e

If elementary OS asks you to choose an editor, nano is a straightforward option. Add a line like this, changing alex to your actual username:

30 2 * * * /home/alex/bin/elementary-backup.sh

The five time fields mean minute, hour, day of month, month and day of week. This example runs every day at 2:30 am. The asterisks mean every possible value for the remaining fields. You can use 0 12 * * 0 for a weekly Sunday backup at midday, or 15 18 * * 1-5 for weekdays at 6:15 pm.

Cron uses the computer's configured local timezone. That matters in Australia because daylight saving changes the clock in New South Wales, Victoria, Tasmania, South Australia and the Australian Capital Territory, while Queensland, Western Australia and the Northern Territory do not observe it. Check the current setting with:

timedatectl

A schedule set for 2:30 am will follow the system timezone, although the clock change can affect the exact behaviour around daylight-saving transitions. Avoid scheduling a large backup at a time when the computer is normally shut down. For a laptop, a time shortly after you usually arrive home may be more practical, provided the external drive is connected.

Confirm that the entry was saved:

crontab -l

On most elementary OS installations, the cron service is already available. If jobs do not run, check its status:

systemctl status cron

You can start it for the current system and enable it at boot with:

sudo systemctl enable --now cron

Use sudo only for managing the service. The personal crontab should remain associated with your normal user account so the script can access your home folders and mounted drive without unnecessary root permissions.

Test the Job and Read Its Logs

Do not wait overnight to find out whether the scheduled task works. Temporarily add a schedule a few minutes ahead, then watch the log after that time passes. For example, if the current time is 10:14, a temporary entry of 16 10 * * * will run at 10:16:

16 10 * * * /home/alex/bin/elementary-backup.sh

Remove the temporary line after testing and keep the daily schedule. You can also run the command through a shell that resembles cron more closely:

env -i HOME="$HOME" USER="$USER" \
    /home/alex/bin/elementary-backup.sh

Cron provides a limited environment. It may not have the same PATH as your terminal, which is why the script uses absolute paths for rsync and mountpoint. It also does not open Files or display a desktop notification. The log file is therefore the main place to look for success messages and errors.

Typical failures include a changed drive label, a disconnected USB cable, a destination with insufficient space, or a source folder that was renamed. Check the mount location with:

findmnt --target "/media/$USER/BackupSSD"
df -h "/media/$USER/BackupSSD"

The first command shows whether the expected path is mounted. The second shows available capacity. If a drive is formatted with a filesystem that does not support Linux permissions properly, rsync may report warnings; for ordinary personal documents, the copy may still be usable, but ext4 is preferable for a Linux-only backup disk.

Cron jobs can also overlap if one backup takes longer than the interval between runs. This is unlikely with a daily schedule, but it can happen after adding many large video files. A flock wrapper prevents two copies from running at the same time:

30 2 * * * /usr/bin/flock -n /tmp/elementary-backup.lock /home/alex/bin/elementary-backup.sh

Protect the Backup Beyond the Local Disk

A second copy in the same room protects against accidental deletion and some hardware failures, but it will not help if the laptop and backup drive are stolen or damaged together. Keep periodic copies at another location, such as a trusted family member's home or a secure workplace. For Australians dealing with flood, cyclone or bushfire risk, physical separation is especially valuable.

Cloud storage can provide an off-site layer, although privacy, subscription cost and Australian data-hosting arrangements deserve attention. Some services store data overseas, while others offer regional storage options. Encrypt sensitive files before uploading them, and keep recovery keys somewhere separate from the computer. A backup that cannot be decrypted after a device failure is not useful.

The script above does not encrypt the external disk. On a portable drive, full-disk encryption is worth considering because the device could be lost in a car, shared house or office. Linux tools such as LUKS can protect the volume, but an encrypted disk must be unlocked before cron can write to it. That makes unattended overnight backups less convenient; an encrypted per-folder archive or a backup application with scheduled unlocking may suit that situation better.

Review the backup occasionally rather than assuming that a successful command proves everything is recoverable. Open several copied files, check the log for errors, and perform a test restore to a temporary folder. Keep an eye on storage capacity, especially when photographs and phone videos are regularly imported. If version history is required, consider a tool designed for snapshots instead of relying on a simple mirror.

A small cron job is most dependable when its scope is clear: choose the important folders, use absolute paths, check that the destination is mounted, write a useful log, and test an actual restore. Connect the external drive before the scheduled time, then keep a separate copy away from the computer so your elementary OS files remain available when you need them.

Browse the News Archive
Latest Updates

From the elementary weekly series

Low-poly faceted abstract render in dark charcoal and electric blue tones, suggesting a news bulletin or announcement
elementary news

elementary weekly #20

The first week with the final Freya release — community reactions, tips, and early impressions gathered in one roundup.

Abstract low-poly geometric scene in midnight blue and soft cyan, conveying a live broadcast or event atmosphere
elementary news

elementary SPECIAL

A live Hangouts event with the elementary OS founders, held on 11 April 2015, discussing the Freya final release.

Low-poly faceted render in deep navy and muted teal with subtle amber highlights, suggesting a tutorial or guide
Tips and Tricks

Timeshift Guide

How to use Timeshift — the intuitive system restore utility for elementary OS — to recover from configuration mishaps.

Explore

Topics & Resources

Dive into guides, application recommendations, and community discussions covering every aspect of elementary OS.