A Sample Script

The following is a sample script. It backs up selected files from a directory:

#!/bin/bash
# Script name: project1_backup
# Description: Backs up project files into a subdirectory
for file in proj1*
do
    fback=project1/$file.bak
    if [ -f $fback ]
      then
         if [ $fback -nt $file ]
            then
               cp $file $fback
               echo `date` $file backed up >> backup.log
         fi
    else
         cp $file $fback
         echo `date` $file backed up >> backup.log
    fi
done

The script iterates through a list of filenames that begin with proj1. It checks whether the current file is newer than the existing backup file. If so, it copies the current file into the backup directory, giving it a .bak extension. Also, any current file that has not yet been backed up is copied into the backup directory. ...

Get Spring Into Linux® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.