Alan Patrick
Posted on December 25, 2022
There is a lot of cool things you can do with scripts! One thing I always wanted to do is create a backup system that runs automatically at a certain point of the day. Here is how I made the script using rsync
and crontab
commands.
Step 0: It all starts with a BANG
Create a script named makebackup.sh
like this:
#!/bin/bash
echo Initializing backup script.
The #!/bin/bash
comment is a directive that announces which interpreter to use. In this case, the interpreter is bash
, but could be others, like python
or node
. This directive is called a shebang
.
We need an executable script, which means it needs permission to be executed. To change the permissions of a file, you need the chmod
command. This command has two ways of receiving new permission policies: numbers and letters. We will use the letter system, and let all users to be capable of executing the script. (There is a good guide for chmod
here).
On the terminal, write:
chmod a+x makebackup.sh
# a means all users
# + means adding a permission (a minus (-) would be removing it).
# x means permission to execute
Step 1: Where will it come from, where will it go
We will be using rsync
for copying the items, but you could use other tools, like the cp
command. To learn more about rsync
, click here.
Within the makebackup.sh
script, write the following:
#!/bin/bash
echo Initializing backup script
rsync [Sources] /path/to/[Destination]/
# Sources are all files and folders to back up
# Destination is the folder to store the backups
echo Backup finised. Exiting script.
Keep in mind! The [Destination] folder doesn't need to already be there, because rsync
creates it for you, if inexistent. All you need is to name the desired output directory. Do not forget the forward slash at the end, though, to indicate that it is a folder.
Step 2: I got a job for you
Before you set up the cronjob, you have to decide on the schedule. When should things be backed up? Use the Crontab Guru tool for deciding when you want the job done — daily, weekly, monthly, etc.
Then, you set up cronjob, which is really easy! Type crontab -e
on the terminal, and it will open a crontab with some nice information — do skim it, at least. Within the file, go to the bottom, paste the schedule you got from Crontab Guru, and type out the full path to your executable script. Something like this:
# Crontab file
00 10 * * * /path/to/makebackup.sh
Step 3: The End
And this is it! Your backup script is automated.
Some warnings and considerations
The cron
service takes 1 or 2 minutes to reload, so if you want to test immediately, keep the timing in mind: set the execution of the cronjob for at least 3 minutes from saving the crontab file.
Another thing to keep in mind is this: you could schedule a tar
command, instead of creating a custom script. It would look something like this:
# Crontab
# at 10:30 am, everyday, do
30 10 * * * tar cvzf /path/to/archive_name.tar.gz /path/to/input_directory/
Conclusion
I hope this was helpful! The cron
service is really powerful, and so is bash
scripting. You can create all kinds of scheduled tasks and make your ubuntu machine to be a personal butler! If you have any ideas of what else can be done with cron
or suggestions for improving this article, please comment below!
Posted on December 25, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 15, 2024