Create an automatic backup system on Ubuntu with bash, rsync and crontab
Tags: bash, beginners
Published: 2022-12-25 23:14:35

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:

```bash
#!/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](https://www.howtogeek.com/437958/how-to-use-the-chmod-command-on-linux/)).

On the terminal, write:

```bash
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](https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories).

Within the `makebackup.sh` script, write the following:

```bash
#!/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](https://crontab.guru/#50_9_*_*_FRI) 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](https://crontab.guru/#50_9_*_*_FRI), and type out the full path to your executable script. Something like this:

```bash
# 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:

```bash
# 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!