Schedule backup in Linux (rsync + cron)

Configure an automatic backup in Linux now and you will thank yourself in the future.

Creating automatic backup will not only save the time you would spend creating the backups manually, but it may save you days, weeks, months or even years of work when something goes wrong and you lose some important data. Everyone knows about this but still a few users schedule an automatic backup in Linux. Even I have postponed configuring scheduled backups in my new KDE Neon install for a few months already. Luckily, nothing wrong happened with my data during this time.

I have just configured my system for automatic daily, weekly, and monthly backups with rsync and I would like to share how I did that. Keep in mind that rsync is a command line tool if you prefer graphical backup programs have a look at LuckyBackup or CloudBerry Backup.

How to schedule rsync

Rsync is installed on all popular Linux systems and you can easily configure it to run on schedule in the background. The most popular and simplest way to do that is to use the cron job scheduler. Cron is installed and configured on all Debian-based systems like Ubuntu, Linux Mint, KDE Neon, etc.

You simply need to add the schedule of the rsync commands you want to run to the crontab file:

00 12 * * * rsync -aAX --delete --exclude '*.Trash-1000' /source/ /backup/daily
00 15 * * 5 rsync -aAX --delete --exclude '*.Trash-1000' /source/ /backup/weekly
00 16 1 * * rsync -aAX --delete --exclude '*.Trash-1000' /source/ /backup/monthly_$(date +%Y%m%d)

Screenshot of my crontab file to schedule automatic backup in Linux with rsync
My crontab file

I am not going to explain the rsync command. I have done it already in this post. Here, I will only explain the schedule format. You can see the schedule format from the comments in the crontab file. The columns abbreviation means the following:

  • m - minute
  • h - hour
  • dom - day_of_month
  • m - month
  • dow - day_of_week
  • command - the command to execute

Hence, my schedule command means that rsync will run at 12:00 daily, at 15:00 on Friday weekly, and at 16:00 on the first day of every month. To configure the time of your automatic backup in Linux, change these time points as you need.

From the above command, you can also see that daily and weekly backups will be overwritten, while all monthly backups will be kept with the date in their names. For example, my backup created on April 1, 2019, has the following name monthly_20190401. If you have a limited amount of space, you can set monthly backups to be overwritten too. Just removed _$(date +%Y%m%d).

I think this is the most reasonable way to automatically backup Linux. You can also compress monthly and weekly backups, but in my experience, compression is not practical. First, it takes a lot of time to run. Second, looking for specific files in a compressed backup is slow.

NOTE that your computer must be on during the scheduled time to execute the rsync commands. Cron will not run missed jobs. To run missed jobs, look at anacron.

Where to backup

You should never store your backup on the same hard drive which you backup from. If it breaks, you will lose all data. I recommend using a separate hard drive either installed in your computer or connected externally through the USB.

I personally have a WD 4 TB hard drive installed on my system that is used for automatic backup, and I also copy monthly backups manually to an external WD 20 TB RAID hard drive. If my computer gets stolen, I will still have a backup of all data on my external hard drive.

You can purchase a hard drive using my Amazon page, where I share the hard drives I use. They all are Linux compatible.

CONCLUSION

Automatic backup in Linux takes only three lines of simple code in your crontab file, but it can be a lifesaver one day. So, do not wait any more, go and configure your automatic backup in Linux.

Read more about backup programs in Linux.

Average Linux User
Average Linux User I am the founder of the Average Linux User project, which is a hobby I work on at night. During the day I am a scientist who uses computers to analyze genetic data.

Comments


graben

https://www.veeam.com/linux-backup-free.html Veeam has a free edition for Linux. I have used the Windows version as well as the enterprise solution Veeam Backup and Replication with VMs and they both are excellent.





Learn how to write in Markdown with this Quick Reference.
Notify me of new comments on this post.
* E-mail is used to display Gravatar.