Thursday, March 9, 2017

Backup files to tarred and zip file

Backup files to tarred and zip file


#!bin/bash

# backs up all files in current directory modified within last 24 hours
# in a tarred and zipped file
# Replace 1 with how many days you want to back up files

BACKUPFILE=backup-`date +"%m-%d-%Y"`
# Embeds date in backup filename

archive=${1:-$BACKUPFILE}
#If no filename specified default to backup-MM-DD-YYYY

find . -mtime -1 -type f -print0 xargs -0 tar rvf "$archive.tar"

#check bellow command
# tar cvf - $(find . -mtime -1 -type f -print) > $archive.tar
# It works But will fail to backup file names contain space
# if there is no files containing spaces it is good

# ALSO USE bellow code but it is slow and not portable
# find . -mtime -1 -type f -exec tar rvf "$archive.tar" {} ;
# use rvf option instead of cvf otherwise only one file will be archived

gzip $archive.tar && echo "Directory $pwd backed up in "$archive.tar.gz" File"

Available link for download