Auto Back up folders Linux

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp

Auto Back up folders Linux




There are some web applications such as Sonarr v2 and Radarr that not have an auto back up wich with some basic bash scripting and Cron can be set up in less than five minutes, that way the next time you format your Pi you can easily restore a back up of your library and save those 15 mins of tedious setup.


1) create your script: create a new file and call it, copy/paste the lines below:

sudo rsync -av --progress --delete --log-file=/home/your-username/Desktop/$(date +%Y%m%d)_rsync.log --exclude "/home/your-username/.folder" /home/data /media/dataBackup_$(date +%Y%m%d_%T)

Meaning of the flags

-av bit: 'a' means archive, or copy everything recursively, preserving things like permissions, ownership and time stamps. 
-'v' is verbose, so it tells you what its doing, either in the terminal, in this case, in the log file.
--progress gives you more specific info about progress.
--delete checks for changes between source and destination, and deletes any files at the destination that you've deleted at the source.
--log-file saves a copy of the rsync result to a date-stamped file on my desktop.
--exclude leaves out any files or directories you don't want copied. In the command above, the .folder directory

/home/data is the directory I want copied. /home/data copies the directory and its contents, /home/data would just copy the contents. 

/media/dataBackup_$(date +%Y%m%d_%T) is the separate drive. Change this to whatever your backup location is. Note that `rsync` will name every sync differently based on day/time of sync

Save in your ~$HOME and make it executable by typing:

sudo chmod +x /home/your-username/Desktop/

You can now double click that .sh file, choose Run in Terminal, it will ask you for your password and run, then leave a log file on your desktop. Or, you can make a cron job to do it for you! and that’s where the auto back up happens.

The cron job

Copy your file to /root by typing:

sudo cp /home/your-username/Desktop/ /root

Then type:

sudo crontab -e

You’ll see a line which reads: minute hour day month year command

Under that, type: 0 22 * * * /root/ > $HOME/readme.log 2>&1

This means:

The hour in military time (24 hour) format (0 to 23)
The day of the month (1 to 31)
The month (1 to 12)
The day of the week(0 or 7 is Sun, or use name)
The command to run
So at 22:00 (10pm) every day root will run the shell script, without prompting you for sudo password (because its running as root already).

Now press Control-X, then type “Y”, then press Enter

In order to delete older back ups, one way of doing this is to create a file with the timestamp of every sync in it. For example add the following command after the command rsync in

date +%Y%m%d_%T >> time.txt

Use the command find to delete backups that matches the timestamp e.g: add this command after the date +%Y%m%d_%T >> time.txt in

find . -type f ! -newer /tmp/timestamp -delete


find . ! -newermt $date ! -type d -delete

This will delete back ups before specific date/time.

More details and sample codes for hourly/daily/monthly backups can be found here

That is how you can set up an easy auto back up of applications that don’t possess one, basic script and Cron job on Linux, this is applicable for most of distros Linux.

Share Post

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp

Related Posts