Soldato
Before I give up and move on to other things I thought I'd try you guys first.
I'm more or less finished with a little script I'm writing for learning purposes. But the fine details are taking too much time, and I'm thinking of moving on and learning other stuff.
Essentially the script automates the creation of full and incremental backups.
The question was this
Which I took from here.
My script looks like this:
The kind of output it generates is like this:
paul.2016-12-07_13:56.full.tar.gz
paul.2016-12-07_13:57.incremental.tar.gz
The part I am getting stuck on is:
"In case of an incremental backup, only do this if the full backup is not older than a week."
I found this which finds the most recent full back up and strips it down to the time stamp:
find /var/backups/ -type f -name "*.full.tar.gz" | grep $user | sort -r | head -n1 | awk 'BEGIN { FS="." } { print $2 }'
The output is: 2016-12-07_22:17
I thought I could do a `date +%F_%R` comparison but I need to find out if the backup is older than 7 days.
Note in the script I use the variable $user (`whoami`) on the command line I have to substitute $user for `whoami`
---
Oh and the other part that I need help on is this:
#Check to see if a full backup already exists or not
if [ ! -f /var/backups/$user.*.full.tar.gz ]
then
echo "A Full Backup has never been taken. Creating Full Backup in /var/backups/."
I think the .*. part needs to be done using regular expressions.
I'm more or less finished with a little script I'm writing for learning purposes. But the fine details are taking too much time, and I'm thinking of moving on and learning other stuff.
Essentially the script automates the creation of full and incremental backups.
The question was this
Write a script called homebackup that automates tar so the person executing the script always uses the desired options (cvp) and backup destination directory (/var/backups) to make a backup of his or her home directory. Implement the following features:
Test for the number of arguments. The script should run without arguments. If any arguments are present, exit after printing a usage message.
Determine whether the backups directory has enough free space to hold the backup.
Ask the user whether a full or an incremental backup is wanted. If the user does not have a full backup file yet, print a message that a full backup will be taken. In case of an incremental backup, only do this if the full backup is not older than a week.
Compress the backup using any compression tool. Inform the user that the script is doing this, because it might take some time, during which the user might start worrying if no output appears on the screen.
Print a message informing the user about the size of the compressed backup.
Test for the number of arguments. The script should run without arguments. If any arguments are present, exit after printing a usage message.
Determine whether the backups directory has enough free space to hold the backup.
Ask the user whether a full or an incremental backup is wanted. If the user does not have a full backup file yet, print a message that a full backup will be taken. In case of an incremental backup, only do this if the full backup is not older than a week.
Compress the backup using any compression tool. Inform the user that the script is doing this, because it might take some time, during which the user might start worrying if no output appears on the screen.
Print a message informing the user about the size of the compressed backup.
Which I took from here.
My script looks like this:
#!/bin/bash
#Declared variables
user=`whoami`
date=`date +%F_%R`
spaceavailable=`df /dev/mapper/vg_centos6mysql-lv_root | awk '{ print $3 }' | sed '1d'`
home=`du ~/ | awk '{ print $1 }'`
roothome=`du ~/ | awk '{ print $1 }' | sed '1,4d'`
oldbackup=`find /var/backups/ -type f -name "*.full.tar.gz" | grep $user | sort -r | head -n1 | awk 'BEGIN { FS="." } { print $2 }'`
echo "This script will help you backup your home directory"
echo
echo "Creating backup to /var/backups/"
#Check to make sure no arguments are passed to the script
if [ $# -gt 0 ]; then
echo "ERROR Usage: $0 with no parameters."
exit
fi
#Check to see if a full backup already exists or not
if [ ! -f /var/backups/$user.*.full.tar.gz ]
then
echo "A Full Backup has never been taken. Creating Full Backup in /var/backups/."
#Create the full backup
if [ "$user" = "root" ] && [ "$roothome" -le "$spaceavailable" ]
then
tar --listed-incremental ~/$user.back.snar -cvzf /var/backups/$user.$date.full.tar.gz ~/ 1> /dev/null
elif [ "$user" != "root" ] && [ "$home" -le "$spaceavailable" ]
then
tar --listed-incremental ~/$user.back.snar -cvzf /var/backups/$user.$date.full.tar.gz ~/ 1> /dev/null
else
echo "There is not enough space to create backup. Please delete some files"
fi
exit
fi
#Giving the user a choice of a full or incremental back up
echo "Please choose if you require a Full or an Incremental Backup"
echo "Please select - '1' for Full Backup or '2' for Incremental Backup"
read n
case $n in
1)
if [ "$user" = "root" ] && [ "$roothome" -le "$spaceavailable" ]
then
tar --listed-incremental ~/$user.back.snar -cvzf /var/backups/$user.$date.full.tar.gz ~/ 1> /dev/null
elif [ "$user" != "root" ] && [ "$home" -le "$spaceavailable" ]
then
tar --listed-incremental ~/$user.back.snar -cvzf /var/backups/$user.$date.full.tar.gz ~/ 1> /dev/null
else
echo "There is not enough space to create backup. Please delete some files"
fi
;;
2)
if [ "$user" = "root" ] && [ "$roothome" -le "$spaceavailable" ]
then
tar --listed-incremental ~/$user.back.snar -cvzf /var/backups/$user.$date.incremental.tar.gz ~/ 1> /dev/null
elif [ "$user" != "root" ] && [ "$home" -le "$spaceavailable" ]
then
tar --listed-incremental ~/$user.back.snar -cvzf /var/backups/$user.$date.incremental.tar.gz ~/ 1> /dev/null
else
echo "There is not enough space to create backup. Please delete some files"
fi
;;
*) echo "invalid option please select 1 or 2";;
esac
#Declared variables
user=`whoami`
date=`date +%F_%R`
spaceavailable=`df /dev/mapper/vg_centos6mysql-lv_root | awk '{ print $3 }' | sed '1d'`
home=`du ~/ | awk '{ print $1 }'`
roothome=`du ~/ | awk '{ print $1 }' | sed '1,4d'`
oldbackup=`find /var/backups/ -type f -name "*.full.tar.gz" | grep $user | sort -r | head -n1 | awk 'BEGIN { FS="." } { print $2 }'`
echo "This script will help you backup your home directory"
echo
echo "Creating backup to /var/backups/"
#Check to make sure no arguments are passed to the script
if [ $# -gt 0 ]; then
echo "ERROR Usage: $0 with no parameters."
exit
fi
#Check to see if a full backup already exists or not
if [ ! -f /var/backups/$user.*.full.tar.gz ]
then
echo "A Full Backup has never been taken. Creating Full Backup in /var/backups/."
#Create the full backup
if [ "$user" = "root" ] && [ "$roothome" -le "$spaceavailable" ]
then
tar --listed-incremental ~/$user.back.snar -cvzf /var/backups/$user.$date.full.tar.gz ~/ 1> /dev/null
elif [ "$user" != "root" ] && [ "$home" -le "$spaceavailable" ]
then
tar --listed-incremental ~/$user.back.snar -cvzf /var/backups/$user.$date.full.tar.gz ~/ 1> /dev/null
else
echo "There is not enough space to create backup. Please delete some files"
fi
exit
fi
#Giving the user a choice of a full or incremental back up
echo "Please choose if you require a Full or an Incremental Backup"
echo "Please select - '1' for Full Backup or '2' for Incremental Backup"
read n
case $n in
1)
if [ "$user" = "root" ] && [ "$roothome" -le "$spaceavailable" ]
then
tar --listed-incremental ~/$user.back.snar -cvzf /var/backups/$user.$date.full.tar.gz ~/ 1> /dev/null
elif [ "$user" != "root" ] && [ "$home" -le "$spaceavailable" ]
then
tar --listed-incremental ~/$user.back.snar -cvzf /var/backups/$user.$date.full.tar.gz ~/ 1> /dev/null
else
echo "There is not enough space to create backup. Please delete some files"
fi
;;
2)
if [ "$user" = "root" ] && [ "$roothome" -le "$spaceavailable" ]
then
tar --listed-incremental ~/$user.back.snar -cvzf /var/backups/$user.$date.incremental.tar.gz ~/ 1> /dev/null
elif [ "$user" != "root" ] && [ "$home" -le "$spaceavailable" ]
then
tar --listed-incremental ~/$user.back.snar -cvzf /var/backups/$user.$date.incremental.tar.gz ~/ 1> /dev/null
else
echo "There is not enough space to create backup. Please delete some files"
fi
;;
*) echo "invalid option please select 1 or 2";;
esac
The kind of output it generates is like this:
paul.2016-12-07_13:56.full.tar.gz
paul.2016-12-07_13:57.incremental.tar.gz
The part I am getting stuck on is:
"In case of an incremental backup, only do this if the full backup is not older than a week."
I found this which finds the most recent full back up and strips it down to the time stamp:
find /var/backups/ -type f -name "*.full.tar.gz" | grep $user | sort -r | head -n1 | awk 'BEGIN { FS="." } { print $2 }'
The output is: 2016-12-07_22:17
I thought I could do a `date +%F_%R` comparison but I need to find out if the backup is older than 7 days.
Note in the script I use the variable $user (`whoami`) on the command line I have to substitute $user for `whoami`
---
Oh and the other part that I need help on is this:
#Check to see if a full backup already exists or not
if [ ! -f /var/backups/$user.*.full.tar.gz ]
then
echo "A Full Backup has never been taken. Creating Full Backup in /var/backups/."
I think the .*. part needs to be done using regular expressions.
Last edited: