Backup Your /etc Directory With Daily Cron Task

By | 2015/08/07

Here is a simple and easy way to make daily backups of your /etc/ directory!


1. Create backup directory

If needed, create the directory /var/backups

sudo mkdir /var/backups

2. Create backup script

Create the file /usr/local/bin/backupetc with the following content:

#!/usr/bin/env bash
#
# backup the etc directory!
#
cd /var/backups
tar cJPf $(date +%F)-$(hostname)-etc-tar.xz /etc/

# remove older backups
find /var/backups -type f -name '*-etc-tar.xz' -mtime +90 -delete


3. Make the script executable

sudo chmod +x /usr/local/bin/backupetc

If desired, test by running with sudo

sudo /usr/local/bin/backupetc

4. Create crontab entry

Next, setup a daily cron task for this to run the backup daily:

sudo crontab -e

Put in the following entry:

@daily /usr/local/bin/backupetc >/dev/null

Done!


The backup tarballs will appear in /var/backups looking similar as this for example:

2015-08-07-scottbookpro-etc-tar.xz

This could obviously be altered as desired.

Rock on,