In this post we'll se something useful about backup your personal data!
Backup data is very important, especially for people that use computer for work. The loss of important data can be a nefarious event.
What we need to run this script is:
- Personal Computer with a distribution of Linux Installed
- About 5 minutes of patience :-)
-------------------------------------------------------------------------------------------------------------------------
UPDATE
The original script has been uploaded by zyphlar on a gitHub repository. The code has been improved in order to perofm security checks. Indeed, when not invoked properly, the script could have deleted data.
https://github.com/zyphlar/backup-rotation-script/blob/master/rotate.sh
Thanks to Zyphlar for the improvements
-------------------------------------------------------------------------------------------------------------------------
Description: This is the script that will makes backup of a specified directory and implements rotation. Rotation in backups means that some old backup files will be deleted automatically and newer will be shifted to replace them. You can set the number of days after which the old backup folders will be deleted. This can be done through changing the NUMDAYS variable. This variable is set up to 7 days by default. Once you have pasted script into a file, save it as "backup_script.sh"
1 - The script must obtain the "x" flag because it's necessary to make possible to eXecute it. So open linux terminal and go to the directory where the script is and type
chmod +x backup_script.sh
2 - To run this script you must use terminal, cd to the directory where the script is and type
./backup_script.sh what where
Where:
- the parameter <what> must defines the ABSOLUTE path of the directory that must be "backupped".
- the parameter <where> must defines the ABSOLUTE path of the directory where the backup will be stored
./backup_script.sh what where > backup_log.log
so in this way the log lines will be stored in backup_log.log file.
3 - We need to execute the last step, the automation of this process. To automate the backup process and schedule it we can use cron. Cron is a daemon that can execute scheduled commands for every user of a machine. You can schedule a command to be executed specifying the minutes of a day when execute it, the hours, the days etc like shown here below.
.---------------- [m]inute (0 - 59)
| .------------- [h]our (0 - 23)
| | .---------- [d]ay [o]f [m]onth (1 - 31)
| | | .------- [mon]th (1 - 12) OR jan,feb,mar,apr...
| | | | .---- [d]ay [o]f [w]eek (0 - 6) (sunday=0 o 7) OR sun,mon,tue,wed,thu,fri,sat
| | | | |
* * * * * comand to be executed
Now we have got to add a scheduled execution of our backup script. Here is an example that you can personalize at your own. For first thing open linux terminal and type
crontab -e
In this way you will edit your cron scheduled commands. (-e option means edit). Now we insert the scheduled execution of our script. To do this, before you can edit the content you must press the key "I" that switches the editor mode into "INSERT" mode. Now it's possible to enter data. Type something like
45 15 * * * /path/to/backup_script.sh > /path/to/backup_log.log
You have to define the absolute path to your script, so replace /path/to/ with the exactly absolute path where the script is placed. Once finished to edit the content press the button "ESC" that switches from "INSERT" mode to "VIEW" mode and type ":wq" and hit return. "wq" means "write changes and quit". Now we have added a scheduled execution of the script to the cron daemon. We can verify this typing
crontab -l
that means to "list" all the cron entries for this user. In the example above we have scheduled the execution of the script every month, every day of the week and the month, at 15.45 (in 24h notation, else 3.45PM in 12h notation). In this way your backups will be automated and the old backups will be automatically deleted and replaced with the shifting logic introduced below.