Wednesday, July 12, 2017

Time and the River

 Zeus, who guided mortals to be wise,has established his fixed law--wisdom comes through suffering.Trouble, with its memories of pain,drips in our hearts as we try to sleep,so men against their willlearn to practice moderation. 
    -- Agamemnon (from the Oresteia), Aeschylus

These lines came to my mind immediately after a quick RMA brought back a fully functional, but completely empty laptop. It had decided to shut down and refuse to charge, and fear was all I knew from that point on. Bottom line; I had refused (by means of laziness) to properly backup my files, I lost them, and willed to never let that happen again.

My current backup solution is simple; a single online copy, hosted on Backblaze's B2 cloud service. It is a fantastic service, with extremely good prices and integrated in many third-party programs, for Linux users like me. I found out about rclone a while ago (written on top of rsync) and decided to give it a try: after battling over encrypted buckets, i managed to upload most of my important files.

The next step was to make backups automatic, otherwise I'd be missing the whole point. So i found out about cron (protip: use corntab to validate your time formatting) and its smart formatting:

00 22 * * * /home/scripts/backup.sh 
(this script runs every day at 22:00).

However, my computer is not turned on 24/7; there must be a way for the computer to run 'lagging' tasks. And here comes anacron, which deals exactly with that. By default anacron runs, and most importantly, runs scripts as root, so I had to create my own local 'anacrontab'.

mkdir ~/.anacron
mkdir ~/.anacron/cron.{hourly,daily,weekly,monthly} ~/.anacron/spool
touch ~/.anacron/anacrontab

Now edit this new 'anacrontab' (replace 'your-user' by the obvious thing):

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/home/aszkid/bin
HOME=/home/your-user
LOGNAME=your-user
# period  delay  job-id  command
1  5   your-user.cron.daily    nice run-parts /home/your-user/.anacron/cron.daily
7  10  your-user.cron.weekly   nice run-parts /home/your-user/.anacron/cron.weekly
@monthly  5  your-user.cron.monthly  nice run-parts /home/your-user/.anacron/cron.monthly

This file will execute all scripts, at their due time, under each of 'cron.daily' etc. Protip: do not write file extensions! For some reason (it might be in the doc), anacron refuses to run files with extensions (e.g. '.sh' files).

And finally we edit our usual user 'crontab' to run anacron through 'crontab -e', and add the following lines:

01 * * * * /usr/sbin/anacron -t /home/your-user/.anacron/anacrontab -S /home/your-user/.anacron/spool
01 * * * * nice run-parts /home/your-user/.anacron/cron.hourly

Reboot (to be safe), and you're good to go.

No comments:

Post a Comment