Bash scripts to take incremental backups of your file and compare the current version of a file with the latest backed up version of the same file.

From: Amit Choudhary

Date: Wed Dec 02 2026 - 02:20:42 EST


--------------------------------------------------------------------------------
Bash scripts to take incremental backups of your file and
compare the current version of a file with the latest
backed up version of the same file.
--------------------------------------------------------------------------------

-------------------
mybackup.sh
-------------------

#!/bin/bash

backup_dir=~/backups

date=`date "+%Y-%m-%d-%H-%M-%S"`

set -x

cp $1 $backup_dir/$1.$date

-------------------------------------------------------------------
compare_file_with_lastest_backed_up_version.sh
-------------------------------------------------------------------

#!/bin/bash

backup_dir=~/backups

ls -tr -1 $backup_dir/$1.*

last=`ls -tr -1 $backup_dir/$1.* | tail -n 1`

echo

set -x

diff -u $last $1

retval="$?"

{ set +x; } 2>/dev/null

if [ "$retval" -eq "0" ]; then
echo
echo -n "Files are SAME: The current file and the latest backed up version"
echo " of the same file are SAME."
echo
fi

if [ "$retval" -eq "1" ]; then
echo
echo -n "Files are DIFFERENT: The current file and the latest backed up"
echo " version of the same file are DIFFERENT."
echo
fi

if [ "$retval" -eq "2" ]; then
echo
echo "ERROR: Some ERROR happened. Please ignore all output."
echo
fi

--------------------------------------------------------------------------------