This is some code from my weekly backup script that shows how I handle the <last backup directory> and create <new backup directory> with date and time information.
The actual backup code, tar and gzip and final move I leave as an exercise as it will be location dependent.
Note that this code contains echo statements, so when run from a cron job an email is generated to the user who launched the cron job.
Code:
#!/bin/bash
# Script to backup files to local directory from LAN PCs
#
# Intended to be run as a weekly cron job.
# Can also be run manually with a space separated list of
# machine names added to just backup from those machines.
# Where to backup to
LOCAL_DEST="/home/<username>/Desktop/Backups/"
# File to maintain the last backup date and time
# The file date and time can be changed with 'touch -t MMDDhhmm /var/bakup/lastPCFilesbakup'
LAST_BACKUP="/var/bakup/lastPCFilesbakup"
# Default list of machines to backup
MACHINES=( M540499 \
M540500 \
)
#***--- Create directory to contain backup
backup_begin() {
# Directory under which to create backup
BACKUP_DIR_PREFIX=$LOCAL_DEST"/"$(date +%F)
if [ ! -d $BACKUP_DIR_PREFIX ]; then
mkdir -p $BACKUP_DIR_PREFIX
fi
}
#***--- Get from PC M540499
backup_M540499() {
# Code to perform backup
...
}
#***--- Get from PC M540500
backup_M540500() {
# Code to perform backup
...
}
...
backup_begin
if [ $1 ]; then
# Assume that additional parameters are machine names
for PC in $@; do
echo "Started backup of "$PC" at "$(date)
backup_$PC
done;
else
# No additional parameters, so use default list of machine names
for PC in ${MACHINES[@]}; do
echo "Started backup of "$PC" at "$(date)
backup_$PC
done;
fi
# Remember this backup date and time
touch $LAST_BACKUP