Table of Contents

Bash script to backup

Directory

Bash script to backup regular files in current directory. The default backup directory is archive inside the current directory. Or you may specify a backup directory as the 1st argument to the script.

#!/bin/bash
#
# Backup Regular files to archive/timestamp
#
NOW=$(date +"%Y-%m-%d-%H-%M-%S")
#
#
if [[ -z $1 ]]
  then
  BACKUPDIR=archive/$NOW
else
  BACKUPDIR=$1/$NOW
fi
mkdir -p $BACKUPDIR
#
for file in $(ls -p | grep -v /$)
do
  cp -p $file $BACKUPDIR/
done
#
exit 0

File

#!/bin/bash
#
if [[ ! ("$#" == 1) ]]; then
  echo "File name required!"
  exit 1
fi
filename=$1
#
NOW=$(date +"%Y-%m-%d-%H-%M-%S")
mkdir -p archive
cp -p $filename archive/$filename.${NOW}