Saturday, 20 July 2013

BACKUP IN TAR FORMAT WITH SCRIPT IN CRONTAB

How to backup files and directories in Linux using tar & cron jobs.


Backup Using TAR

Backing up your files using tar is very simple you just type a little command.

#tar -cvpzf   /BackupDirectory/backupfilename.tar.gz  /ImportantData/directory/path


Let’s suppose i have directory called /imp-data on root  and i want to make backup of this directory including sub directories on  different location like in /mybackupfolder.

#tar -cvpzf /mybackupfolder/backup.tar.gz    /imp-data


Command Explaination:
tar = tape archive
c =  Create
v =  Verbose mode
p = Preserving Files and Directory Permissions.
z = This will tell tar that compress the files further to reduce the size of tar file.
f =  It is allows to tar get file name.

Let’s add tar command in bash script to make this whole backup process automatic.
Here i will show you my self-created simple bash script that i am using for backing up my important data and then we will use cron job to run our whole process in background automatically.

Here is my Super Simple Backup Script :)

#vi /backup.sh



#!/bin/bash
#Purpose = Backup of Important Data
#Created on 20-7-2013
#Author = Piyush Gupta
#Version 1.0
#START
TIME=`date +"%b-%d-%y"`         # This Command will add date in Backup File Name. FILENAME="backup-$TIME.tar.gz"     # Here i define Backup file name format. SRCDIR="/imp-data"                 # Location of Important Data Directory
DESDIR="/mybackupfolder"                  # Destination of backup file.
tar -cpzf $DESDIR/$FILENAME $SRCDIR
#END
:wq

#crontab -e

#Minutes Hours Day of Month  Month  Day of Week     Command 01        13        *     *            1,6    /bin/bash       /backup.sh
:wq

#service crond restart
#chkconfig crond on



That’s All… This Script will run at 01:01:00 at every Monday and Saturday.

No comments:

Post a Comment