Monday, August 15, 2022
HomeWordPress DevelopmentAutomated Backups with cron and RClone

Automated Backups with cron and RClone


This text will present you tips on how to use RClone and cron to automated file backups in a Linux Working System.


It’s Friday previous 6 PM, and I wanted to again up some work and college information to Google Drive. Sure, I exploit git for coding initiatives, however this type of work is healthier stored in Google Drive. So I’m wondering if there was a faster, easy, and automatic means of doing backups to Google Drive, and fascinating, I discovered RClone.

On this article you’ll study:

  • Whats is RClone.
  • What’s cron.
  • Primary utilization of RClone and cron.
  • How one can routinely again up information each Friday at 6:30 PM.
  • Cron logging.
  • How one can uninstall RClone.



Whats is RClone?

Mainly, RClone is a command-line laptop program that may handle, switch and sync information out of your laptop to cloud storage, and even between two cloud storages.



Whats is cron?

Cron is a command-line utility software program with the primary goal of scheduling jobs. Jobs are cron command with two components mainly, the primary one is the schedule expression , the final is the script to be executed. Normally, all Linux OS comes with cron put in, if not:

CentOS/RHEL 7/8/5

yum set up cronie
Enter fullscreen mode

Exit fullscreen mode

Ubuntu

apt-get set up cron
Enter fullscreen mode

Exit fullscreen mode

With cron, you may schedule a command or a shell script to run at a particular time, and that’s what we going to do. Schedule an RClone command to run each Friday at 6:30PM.



Organising RClone

To begin, set up RClone.

For Linux, macOS, and BSD, simply run:

curl https://rclone.org/set up.sh | sudo bash
Enter fullscreen mode

Exit fullscreen mode

After putting in RClone, let’s run the RClone configuration command.

rclone config
Enter fullscreen mode

Exit fullscreen mode

Right here you’ll comply with these steps:

  1. Select n for a New distant.
  2. Sort the title of the brand new RClone config, for instance, mygdrive.
  3. Sort 15 to make use of Google Drive as distant storage.
  4. For the following two steps, simply sort enter to proceed.
  5. Now select 1 for full entry.
  6. For the following two selections, simply sort enter to proceed.
  7. Signal-in into your google account within the RClone popup.
    Now you might be able to go.



Constructing a shell script to backup information to Google Drive

We’re going to construct a shell script that incorporates RClone command traces to sync folders from my laptop to the Google Drive storage.

Open your terminal and kind:

mkdir sh ; cd sh
Enter fullscreen mode

Exit fullscreen mode

You don’t have to create a sh folder in your $HOME listing, you may create in one other place and even don’t create, however it’s a good suggestion to arrange your scripts in a single place. If you happen to create in one other place, simply remind your self to vary the trail within the subsequent steps.

Now if you wish to use vim then:

vim backup.sh
Enter fullscreen mode

Exit fullscreen mode

However if you wish to use one other textual content editor, simply create a file named backup.sh within the $HOME/sh folder or every other place, however $HOME/sh will likely be my location for this text.

With the backup.sh file opened, let’s write a line of code that syncs a folder from my laptop to the Google Drive storage.

rclone sync -v --create-empty-src-dirs /$HOME/Desktop/Work mygdrive:/Work
Enter fullscreen mode

Exit fullscreen mode

Now let’s dive into the reasons of the command.

rclone sync
Enter fullscreen mode

Exit fullscreen mode

Sync will copy the information from the supply and ship them to the vacation spot, however is not going to modify the supply, solely the vacation spot.

--create-empty-src-dirs
Enter fullscreen mode

Exit fullscreen mode

By default empty folders within the supply usually are not synced, so to work round, this flag creates empty folders within the vacation spot.

Within the final a part of the command, you have got two paths separated by an area, the primary one is the supply path and the second is the vacation spot path. By now it is best to most likely discover that the second path has a mygdrive: which is similar title because the rclone config title. So mainly I’m syncing knowledge from my laptop to Google Drive, utilizing mygdrive configuration.

You possibly can have a number of RClone configurations and this lets you sync or copy information, not simply out of your laptop to the cloud, however from one cloud to a different. Identical to this:

rclone sync -v --create-empty-src-dirs mygdrive:/Work anothergdrive:/Work
Enter fullscreen mode

Exit fullscreen mode

To see if the backup.sh file works, simply run the command beneath and test if the vacation spot has modified.

supply backup.sh
Enter fullscreen mode

Exit fullscreen mode

After backup.sh working correctly, let’s perceive how we will schedule the backup.sh to run each day.

Remaining script:

rclone sync -v --create-empty-src-dirs /$HOME/Desktop/Work mygdrive:/Work
rclone sync -v --create-empty-src-dirs /$HOME/Desktop/College mygdrive:/College
Enter fullscreen mode

Exit fullscreen mode



Organising cron to run Shell file

To schedule any job in cron we have to edit the crontab file, and to edit this file, open your terminal and kind:

crontab -e
Enter fullscreen mode

Exit fullscreen mode

If you happen to by no means used crontab, then you’ll have to select a default textual content editor, in my case it’s vim.

It’ll open the crontab file along with your default textual content editor. When the file is open you will note solely feedback.

To schedule in cron you have to a particular sort of expression. Under you will note the construction of the expression:

.---------------- minute (0 - 59)
 |  .------------- hour (0 - 23)
 |  |  .---------- day of month (1 - 31)
 |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
 |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR solar,mon,tue,wed
 |  |  |  |  |
 *  *  *  *  *  command to be executed
Enter fullscreen mode

Exit fullscreen mode

Examples — Expression to schedule a job (my_shell.sh) to run each Monday at 8:49 AM is:

49 8 * * 1 my_shell.sh
Enter fullscreen mode

Exit fullscreen mode

If you wish to run a job (my_shell.sh) from Monday to Friday at 11:26 AM you may write:

26 11 * * 1-5 my_shell.sh
Enter fullscreen mode

Exit fullscreen mode

Extra examples of expressions:

0 * * * *     => Each hour
*/2 * * * *   => Each two minutes
0 9-17 * * *  => At minute 0 previous each hour from 9 by way of 17
Enter fullscreen mode

Exit fullscreen mode

For extra examples or to check in case your expression will return the identical logic as you count on, go to crontab for extra data.

With the crontab file open, sort the road beneath:

30 18 * * 5 /$HOME/sh/backup.sh
Enter fullscreen mode

Exit fullscreen mode

What this command does is, each Friday at 6:30 PM backup.sh will likely be referred to as, and it’ll again up my information to Google Drive.



Suggestions — The place is the cron log file?

By default the cron log file is positioned at:

/var/log/syslog
Enter fullscreen mode

Exit fullscreen mode

And to see the cron log on this file, run the code beneath:

grep CRON /var/log/syslog
Enter fullscreen mode

Exit fullscreen mode

or to repeatedly watch the file:

tail -f /var/log/syslog | grep CRON
Enter fullscreen mode

Exit fullscreen mode

This command will return one thing like this:

Dec 15 07:35:01 rainbowone CRON[242416]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Dec 15 07:45:01 rainbowone CRON[245607]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Dec 15 07:55:01 rainbowone CRON[248793]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Dec 15 08:00:01 rainbowone CRON[249386]: (rainbow) CMD (/$HOME/Desktop/sh/each day.sh)
Enter fullscreen mode

Exit fullscreen mode

These messages solely present which command cron ran, if you wish to see the backup.sh log you have to to personalize the backup.sh to create its personal log file, and write the content material of it.

However assuming you wish to log the cron message to a particular file, luck of yours I’ll present you ways.

To start with, open this file:

/and so forth/rsyslog.d/50-default.conf
Enter fullscreen mode

Exit fullscreen mode

Uncomment the road that begins with #cron.* after which restart syslog with:

sudo service rsyslog restart

Enter fullscreen mode

Exit fullscreen mode

After that you will note cron logs in /var/log/cron.log and /var/log/syslog.



How one can uninstall RClone?

It is extremely easy to uninstall RClone from a Linux working system, to begin with, let’s discover the place the rclone configuration file is, to seek out the file run the command beneath.

rclone config file
Enter fullscreen mode

Exit fullscreen mode

It’ll listing a number of RClone configuration paths.

Configuration file is saved at:
/house/$USER/.config/rclone/rclone.conf
Enter fullscreen mode

Exit fullscreen mode

To take away the configuration file:

sudo rm -rf /house/$USER/.config/rclone/rclone.conf
Enter fullscreen mode

Exit fullscreen mode

And to lastly take away RClone type the system:

sudo rm /usr/bin/rclone
sudo rm /usr/native/share/man/man1/rclone.1
Enter fullscreen mode

Exit fullscreen mode

At this level, you might be most likely in a position to automate file backups with cron and RClone in a Linux working system.

If you happen to like this text you may give a ❤️, and 🔖 for later within the save button.

And in the event you like content material about Git, Linux, Productiveness suggestions, Typescript, and Python please comply with me Marco Antonio Guess


Repository for the How one can use RClone and cron to automate backup in Linux.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments