Linux Tips : CRON to back up files or folders

Follow My Blog on WordPress.com

I’ve been using Manjaro Linux since the start of 2020. Update : Have since switched to Arch Linux, beginning 2021. Both using using 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 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 TimeShift.

Backups

A simple way to back up files and folders other than a system backup, or backups that are compressed, all mixed in one big file and complicated to use.

I wanted to backup My Document folder and it’s contents from my internal Linux system HD to another 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 (Linux….), or outdated, or weren’t very good. Then someone talked about ‘Cron’ amongst other things…. So of I went starting read up and learn a little, very little about this Cron thingy.

Cron now what that ?

This took a lot of time to get my head around it, and need 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.

If you have to install it, like on Arch Linux then you need to install Cron which is really called cronie (spot how Linux complicates things. cron is called cronie and you need to install rsync as well. With Arch use pacman. You also need to start cron with terminal, just do ‘systemctl enable cronie.service’ and reboot.

In this folder you need to create a file with root permissions (important root permission, if you do 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 is 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 , which is a shame as a mail telling that it’s run, would be nice, 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 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 backuups=

So there you go, A small text file (no .txt) after the name of the file in the cron.d folder in /etc/cron.d and my files 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.

MailTo and MailFrom

After writing this I’ve got the MailTo and MailFrom to work, will do a another post 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…

Follow My Blog on WordPress.com

Leave a Reply

Your email address will not be published. Required fields are marked *