2

I'm using rsync to clone changes from a remote server(MT DV4.0) to a local one(ubuntu 11.10). I'm then using the following copy/paste script to compress then archive the files.

#!/bin/bash
####################################
#
# Backup to NFS mount script with
# grandfather-father-son rotation.
#
####################################

# What to backup. 
backup_files="/home/server-backups/var/www/vhosts /home/server-backups/var/www/vhosts"

# Where to backup to.
dest="/home/bryan/server-tar"

# Setup variables for the archive filename.
day=$(date +%A)
hostname=$(hostname -s)

# Find which week of the month 1-4 it is.
day_num=$(date +%d)
if (( $day_num <= 7 )); then
        week_file="$hostname-week1.tgz"
elif (( $day_num > 7 && $day_num <= 14 )); then
        week_file="$hostname-week2.tgz"
elif (( $day_num > 14 && $day_num <= 21 )); then
        week_file="$hostname-week3.tgz"
elif (( $day_num > 21 && $day_num < 32 )); then
        week_file="$hostname-week4.tgz"
fi

# Find if the Month is odd or even.
month_num=$(date +%m)
month=$(expr $month_num % 2)
if [ $month -eq 0 ]; then
        month_file="$hostname-month2.tgz"
else
        month_file="$hostname-month1.tgz"
fi

# Create archive filename.
if [ $day_num == 1 ]; then
    archive_file=$month_file
elif [ $day != "Saturday" ]; then
        archive_file="$hostname-$day.tgz"
else 
    archive_file=$week_file
fi

# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest/

The daily backups seem to be working great, however I'm only getting 6 daily backups. Saturday is missing (this should be the day the weekly backup runs). The monthly backup is also missing.

I'm a server admin amateur so any help would be greatly appreciated. Thanks!

Bms85smb
  • 153
  • 1
  • 6

2 Answers2

4

Your script seems to work fine, but it's a little hard to follow; I've removed a few minor errors (unportable to use == with test, it's =) and removed the cascading ifs with a simpler solution; it now works with plain sh.

#!/bin/sh
####################################
#
# Backup to NFS mount script with
# grandfather-father-son rotation.
#
####################################

# What to backup. 
backup_files="/home/server-backups/var/www/vhosts /home/server-backups/var/www/vhosts"

# Where to backup to.
dest="/home/bryan/server-tar"

# Setup variables for the archive filename.
day=$(date +%A)
hostname=$(hostname -s)

# Find which week of the month 1-4 it is.
day_num=$(date +%d)
# Integer arithmetic to replace cascading ifs, but now can have 5th week
week_file="$hostname-week$(expr $day_num / 7 + 1).tgz"

# Find if the Month is odd or even.
month_num=$(date +%m)
month_file="$hostname-month$(expr 2 - $month_num % 2).tgz"

# Create archive filename.
if [ $day_num = 1 ]; then
    archive_file=$month_file
elif [ $day != Saturday ]; then
    archive_file="$hostname-$day.tgz"
else
    archive_file=$week_file
fi

# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files

# Print end status message.
echo
echo "Backup finished"
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest/

What's the actual problem? When running it today (Saturday), I find it tries to make a tar file called /home/bryan/server-tar/pegasus-week2.tgz.

Are you surprised that there is no tarfile for Saturday? If you want one there, you can do some linking because it'll be identical for the $hostname-week${x}.tar

Can you provide an ls of /home/bryan/server-tar?

[diff of the changes is at http://www.bayofrum.net/~crees/patches/sf-469087-1.diff]

Chris Rees
  • 275
  • 1
  • 7
1

Sorry, I'm late. Maybe your 'day' variable differently shown in terminal?

elif [ $day != Saturday ]; then

to

elif [ $day != saturday ]; then

This is a small change in date. You can see how it looks date in terminal:

echo day=$(date +%A)