Pages

27.7.12

Automated Rotation Backup Script

Hi guys!

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
This script generates also a log text. In fact if you run it from terminal you'll see a list of log lines talking about the backup operations. If you want to store them into a separate file you can execute the script in this way:

./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.

3 comments:

  1. Cool! Two problems though, firstly $BACKUPDIR doesn't exist so you create a folder in the root directory unnecessarily. Second, if you run this script without a second argument, it will promptly attempt to delete basically your whole hard drive (rm -rf /bin, rm -rf /home, etc, since they're older than 7 days.) I found this out the exciting way.

    I've uploaded a modified version to Github for future collaboration/forking/fun: https://github.com/zyphlar/backup-rotation-script

    ReplyDelete
  2. Hi zyphlar I've found your comment usefull. Sorry for the late reply and great idea for the github!!

    ReplyDelete
  3. You could have added a check if the script was executed with arguments. After editing, I forgot the arguments and it wiped my / partiton :)

    root@mx1:~# ./backup_rotation.sh
    [Sun Sep 25 11:18:03 CEST 2016] - Started Backup
    [Sun Sep 25 11:18:03 CEST 2016] - Backup directory in /09-25-2016/
    [Sun Sep 25 11:18:03 CEST 2016] - Finished Backup - 4.0K /09-25-2016/ Copied
    [Sun Sep 25 11:18:03 CEST 2016] - Found outdated backup folder! Deleting directory /usr
    [Sun Sep 25 11:18:08 CEST 2016] - Found outdated backup folder! Deleting directory /mnt
    [Sun Sep 25 11:18:08 CEST 2016] - Found outdated backup folder! Deleting directory /media
    [Sun Sep 25 11:18:08 CEST 2016] - Found outdated backup folder! Deleting directory /srv
    [Sun Sep 25 11:18:08 CEST 2016] - Found outdated backup folder! Deleting directory /lost+found
    [Sun Sep 25 11:18:08 CEST 2016] - Found outdated backup folder! Deleting directory /backup
    [Sun Sep 25 11:18:08 CEST 2016] - Found outdated backup folder! Deleting directory /home
    [Sun Sep 25 11:18:08 CEST 2016] - Found outdated backup folder! Deleting directory /var
    [Sun Sep 25 11:18:09 CEST 2016] - Found outdated backup folder! Deleting directory /lib

    ReplyDelete