Linux CRON to back up files or folders

I’ve been using Manjaro Linux since the beginning of 2020. Update : Have since switched to Arch Linux, beginning 2021. With both I used CRON to back up files or folders.

So far so good. I’m getting used to the way it works. Linux quirks can be a real pain to learn or get used to. Coming from Windows after using it for over 30 years. Makes it sometimes quite complicated to understand.

Unlike Windows it’s harder to use. To modify or create files, folders in certain places etc can be a real pain, because of Linux’s security. I suppose it’s a good thing, but wrapping you head around how to modify a dopey file or to add a line or just a folder can be more than just a pain in the ass.

In a these Linux posts. I’m trying to write down things that I found really hard to do or learn or understand and / or very time consuming to do. Hoping that they maybe of some use to someone.

This tip is how I backup certain files and folders that I need or want or keep if ever I reinstall Linux or delete by accident. I’m talking about everyday files and folders here. Mostly from My documents folder. Not a system backup, I use a specific program for that called TimeShift.

Backups

I wanted to backup My Document folder and it’s contents from my internal Linux system HD to a second internal HD.
As with Windows and now Linux, if my system crashes I don’t normally bother  about repairing a system. I just wipe the HD and do a fresh install as it’s nearly always quicker. Especially with Linux (no bothering with product keys, and checking with MS to see if it’s a real copy or not). Just download the latest ISO and hop it’s done. Reinstalling Linux is hours quicker than reinstalling Windows.

For my everyday backups I asked a couple of questions on the Telegram group and a IRC chat group that I’m often use. People suggested me a couple of native Linux programs. I downloaded several and tried them but found them all over complicated to use (Linux….), or outdated, or just weren’t very good. Then someone spoke about ‘Cron’ amongst other things…. So of I went. Starting to read up and learn about this Cron thingy.

Cron now what’s that ?

This took a lot of time to get my head around it, and needed to ask many questions on the forums to understand the basics. But finally, it was quite simple…… (remember above : “but wrapping you head around how to modify a dopey file…”) after having tried a few specific programs I found that it was by far the best and easiest for what I needed.
Cron works with just a file (text document) placed in the ‘cron.d’ folder which is in your /etc/ folder.

You can see my Cron files for my different cron jobs
You can see my Cron files for my different cron jobs

It’s just a file (for us Windows guys its just a text file without the .txt extension) that you write and drop it in the Cron.d folder on the system disc.
Linux being Linux. It’s just a tad more complicated that Windows because of permissions that are needed the file ‘needs Root permissions’ as the owner. But when you write a file in Linux the owner is you and not root. So you need to know how to create file with root permissions.

Note : I’m not a Linux geek, and know next to nothing about it, but as I play with it, I’m retired… have time… I sometimes manage to get things working, Cron for instance. It took me yonks to get it to work, reading on the web, the forums the chats. So I hope this post will help you and save some time.

For starters

Don’t forget I’m using Manjaro XFCE. Cron and rsync (cron uses rsync) is installed by default with Manjaro. Maybe your Linux isn’t the same as mine. I was using Manjaro when I wrote this post but since switched to Arch linux XFCE. Cron works perfectly with Arch as it did with Manjaro.

If you have to install it. If like on Arch Linux then you need to install Cron which is really called cronie (See how Linux complicates things. cron is called cronie and you also need to install rsync as well.

With Arch you use pacman to install it (sudo pacman -S cronie) Don’t forget to install rsync as well. After you also need to start cron with terminal, just do ‘systemctl enable cronie.service’ and reboot.

In this folder /etc/cron.d you need to create a file with root permissions (important root permission. If you do it with user permission. It won’t work) and name it. Mine is called ‘Copy_my_documents_folder’. Once this done. You can write the cron commands in the file. Here are mine.

Create a file with root permissions

To create a file with root permission, this is what I do :
All using terminal
cd to the cron.d folder
cd /etc/crond.d
Then :
sudo nano my_file_name_that_I_want_to_use OK

or you can do
sudo nano /etc/cron.d/my_file_name_that_I_want_to_use OK
without doing the cd….. before as the /etc/cron.d/ puts the file there
enter your password

This will open a empty ‘nano’ window (empty because the file doesn’t exist yet) in this rather strange window, similar to a text app. Write in the cron commands text. Or paste it (see just below)
Change the time and paths as needed
Once this nano window is filled in, do a Ctrl o (^o) then OK. Then Ctrl x (^x)
you will now have the file saved as ‘root’ owner in your cron.d folder

My cron commands

#Copy_my documents folder in home to BackUp_1 in the folder named Cron_copies_arch/My_documunts/
MAILTO=myemail@xx.com
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILFROM=””
00 09-22 * * * root rsync -avhW /home/trevor/Documents/* /mnt/BackUp_1/Cron_copies_arch/My_documunts/

Now most of this I haven’t a clue what it means. But it works. I know that the mailto and mailfrom is so that you can send and receive mails when it runs. But you need add more software to your Linux and I tried but didn’t understand it. A shame as a mail telling that it’s run, would be nice. I will try again one day. (Update got that working, will try and explain in a new post)

Line with the # at the beginning

Line_1 #Copy_my documents folder in home to BackUp_1 in the folder named Cron_copies_arch/My_documunts/
(having the # at the start of the line is so that the program doesn’t read it a command, its just a note for yourself)

This part I just copied from examples that I found elsewhere, The first two lines, Shell & path I haven’t a clue what they mean or do.

Line_2 SHELL=/bin/sh
Line_3 PATH=/sbin:/bin:/usr/sbin:/usr/bin
Line_4 MAILTO=””  (“” means the Cron skips this) replace the “” with your email if you want it to send you a mail when the job is done
Line_5 MAILFROM=””  (“” means the Cron skips this) replace the “” with your email if you want it to send you a mail when the job is done

Line_6 00 09-22 * * * root rsync -avhW /home/trevor/Documents/* /mnt/BackUp_1/Cron_copies_arch/My_documunts/
That’s all one long line in my file, but WordPress breaks it into two lines because there’s a space between the /* and /run

6 lines in all. Fairly easy

Cron timing

00 09-22 * * *
This is the cron timing. Here I tell it to run on the hour, every hour from 9am to 10pm. As with everything be careful here., I once added one too many * Nothing worked after……
https://crontab.guru/ Use this site to change, or setup your timing. It’s fairly easy to understand. Basically cron time is ‘5 stars’. Each star meaning something.

root rsync -avhW / rsync is what cron uses for copying and the avhW is to do with copying permissions. So I was told. It works.

From

home/trevor/Documents/*
My home folder ‘Documents’ (it’s the same as the My Document Folder in Windows). The /* after means everything in it. All sub folder and files.

To

/mnt/BackUp_1/Cron_copies_arch/My_documunts/
My BackUp destination (for me its a second internal HD. Just for backups.

So there you go. A small text file (without the .txt) after the name of the file in the cron.d folder in /etc/cron.d and my files and folders are copied easily

Just for fun. Here’s another file for you

Copies my Download folder files once a day at 9pm, again to my 2nd HD

MAILTO=xx@xx.com
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILFROM=””
00 21 * * * root rsync -avhW /home/trevor/Downloads/* /mnt/BackUp_1/Cron_copies_arch/My_downloads/

So there you go using CRON to back up files or folders. Once you have created the file. It’s simple and works well.

To check if all works you can see the logs in terminal running “journalctl -xe -u cronie” (if your running Debian then do sudo journalctl -xe -u cron)

MailTo and MailFrom

After writing this I’ve got the MailTo and MailFrom to work. Below is how to do that as again it’s quite complicated, welcome to Linux 🙂

See : https://bit.ly/3t3AMnt

Once done replace MAILTO=”” with MAILTO=mymail@xx.com

Please don’t hesitate if you have a question.. I not very good a Linux. But I’m learning …… and if I can help…