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 :-)

Follow the step-by-step guide here below:

1 - Open a text editor like Vi, gedit, emacs or others and create an empty file.

2 - Copy and paste this code:


#!/bin/bash

# Author: Marcello Torchio

# Set the directory name that you wish to backup
DIRTOBACKUP=$1

# Set up the backup destination directory
BACKUPDIRDEST=$2

# Set up the number of days for backup rotation
NUMDAYS=7;

# Start logging
echo "["$(date)"] - Started Backup"
# Check if the destination dir exists. If not create it
if [ ! -d "$BACKUPDIRDEST/"$(date +"%m-%d-%Y")"/" ]; then
    # Creates directory for today's backup
    mkdir $BACKUPDIR/"$(date +"%m-%d-%Y")"
fi

# Echo the log line.
echo "["$(date)"] - Backup directory $DIRTOBACKUP in $BACKUPDIRDEST/"$(date +"%m-%d-%Y")"/"
# Execute the backup command. This backup consist in a recursive copy of the source dir into destination dir
cp -R $DIRTOBACKUP $BACKUPDIRDEST/"$(date +"%m-%d-%Y")"/

TOTMB=$(du $BACKUPDIRDEST/"$(date +"%m-%d-%Y")"/ -hs)
echo "["$(date)"] - Finished Backup - $TOTMB Copied"

# Search for folders older than NUMDAYS days and remove them. This makes possible rotation of backups
for i in `find $BACKUPDIRDEST/ -maxdepth 1 -type d -mtime +$NUMDAYS -print`;
do
        echo -e "["$(date)"] - Found outdated backup folder! Deleting directory $i";
        \rm -rf $i;
done

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"

3 - 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

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

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

Bye

2 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