Skip to content

Instantly share code, notes, and snippets.

@stoyanovgeorge
Created August 30, 2018 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoyanovgeorge/c0b30c6483d3835e65e0ce12da30c472 to your computer and use it in GitHub Desktop.
Save stoyanovgeorge/c0b30c6483d3835e65e0ce12da30c472 to your computer and use it in GitHub Desktop.
Backup script which will backup all the files in a custom directory, will create a tar.xz archive and will delete previous backup files in case the md5sum is the same.
#!/bin/bash
target_dir=""
bkp_dir=""
while [[ ! $bkp_dir =~ [a-zA-Z] && ! -d "$bkp_dir" ]];
do
echo "Please enter a valid directory to backup:"
read -r bkp_dir
done
while [[ ! $target_dir =~ [a-zA-Z] ]];
do
echo "Please enter a valid backup directory name:"
read -r target_dir
done
if [ ! -d "$target_dir" ]; then
echo "Creating the new backup directory: $target_dir"
mkdir -p "$target_dir";
fi
now="$(date +'%Y%m%d')"
tar cfJ "$HOME"/backup/"${now}"_backup.tar.xz "$bkp_dir" &> /dev/null
# Evaluates the md5sum of the most recent backup and compares its md5sum against the other files in the same directory. If the md5sum is the same removes the duplicated file
md5sum_now=$(md5sum "$target_dir"/"${now}"_backup.tar.xz | awk '{ print $1 }')
for filename in $target_dir/*.tar.xz;
do
if [[ "$md5sum_now" == $(md5sum "$filename" | awk '{ print $1 }') ]] && [[ "$filename" != "$target_dir"/"${now}"_backup.tar.xz ]]
then
rm "$filename"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment