shell script to backup files and directories in Linux using tar & cron jobs

The vi Editor

http://t4test.com/blog/unix-linux-shell-script-introduction-to-shell-scripting/

http://t4test.com/blog/read-making-shell-script-interactive/

http://t4test.com/blog/cpio-a-backup-program/


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.
In above example my command will be.

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

 

Now Create file using vi editor and paste below script.

vi /autobackup.sh
#!/bin/bash
#AutoBackup of Important Data

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 (Source of backup).
DESDIR="/mybackupfolder"            # Destination of backup file.
tar -cpzf $DESDIR/$FILENAME $SRCDIR

This Script will make backup of /imp-data directory and save it into a single compressed file on /mybackupfolder Directory.

 

 

Now i will show you how to schedule our backup process. In Linux we use cron jobs in order to schedule task.
For setting up cron jobs we use crontab -e command in shell, this command basically says we are going to edit our cron jobs file. If you run first time crontab -e command then it will ask you to default text editor, you just select your favorite editor after that it will never ask you again.

crontab -e

Format of Crontab. It has 6 Parts:

Minutes    Hours     Day of Month    Month     Day of Week     Command

0 to 59       0 to 23      1 to 31               1 to 12        0 to 6                Shell Command

Let’s Suppose i want to run this backup process on every Mon and Sat  at 1:pm.
In Above Condition my Crontab file should be like this.

crontab -e
#   Minutes    Hours      Day of Month       Month    Day of Week    Command

      01        13               *             *        1,6          /bin/bash /backup.sh

Now Done…..

To know more about the scheduled job in linux:

Linux Cron Job – Schedule Job in linux: crontab

PHP Cron Job: How to Execute PHP file Using Crontab in Linux

Related Posts

  • 60
    In all Operating system there option to schedule a job/activity that should be run on particular time interval. Here is an introduction to job scheduler in linux, called crontab. Cron allows tasks to run automatically in the background at fixed time or time intervals depending upon the necessity of the…
    Tags: crontab, cron, job, command, month, day, file, jobs, time, linux
  • 41
    There are various method to execute php file via crontab in linux. Here is the details of all the method: Method 1: Run the php script using URL from the crontab If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup…
    Tags: script, crontab, file, linux, job, cron
  • 34
    rsync = Remote Sync This is a remote and local file synchronization tool. It uses an algorithm that minimizes the amount of data copied by only moving the portions of files that have changed. This is a very flexible network-enabled syncing tool. Due to its ubiquity on Linux and Unix-like…
    Tags: directory, files, file, time, linux, command
  • 33
    cp command under a Linux, UNIX-like, and BSD like operating systems is used to copy files and directories . cp is the command entered in a Unix and Linux shell to copy a file from one place to another, possibly on a different filesystem. The original file remains unchanged, and the new…
    Tags: command, linux, file, files, shell
  • 33
    Use the blow command to set the linux date #date 072511242014 where 07= mm (Month) 25=dd (Date) 11=hh (Hour) 24=mm (minute) 2014=yyyy (Year)
    Tags: linux, command, month, time

One thought on “shell script to backup files and directories in Linux using tar & cron jobs

  • February 1, 2014 at 11:42 am
    Permalink

    You can see your active cron with the terminal command:

    crontab -l
    Here are the parameters in order:

    1) min (0 – 59)

    2) hour (0 – 23)

    3) day of month (1 – 31)

    4) month (1 – 12)

    5) day of week (0 – 6) (Sunday=0)

    6) command

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: