Farmi
Farmi

Reputation: 153

Exclude directories and delete old backup

I am using following simple script script to take backup of all of my websites via tar

TIME=`date +%b-%d-%y`
FILENAME=backup-$TIME.tar.gz

#Parent backup directory
backup_parent_dir="/backup/httpdocs"

#Create backup directory and set permissions
backup_date=`date +%Y_%m_%d_%H_%M`
backup_dir="${backup_parent_dir}/${backup_date}"
echo "Backup directory: ${backup_dir}"
mkdir -p "${backup_dir}"
chmod 755 "${backup_dir}"

SRCDIR=/var/www/sites           #Location of Important Data Directory (Source of backup).

tar -cpzf $backup_dir/$FILENAME $SRCDIR

Now, this is wokring fine but I need 2 things if I can do via same script

  1. Can I exclude some folder within /var/www/sites directory, like if I don't want /var/www/sites/abc.com/logs folder to be backup. Can I define it and some other sub-directories within this script?

  2. This script takes all sites in tar format in specified folder /backup/httpdocs through cronjob which runs daily during night and for old tarballs(older than 7 days) I have to delete them manually, so it there any possibility through same script so when it runs, it checks if there is any backup exists older than 7 days and it deletes it automatically?

EDIT:

Thanks everyone, this is what I am using now which takes backup excluding log files and delete anything older than 7 days

#!/bin/bash
#START
TIME=`date +%b-%d-%y`            # This Command will add date in Backup File Name.
FILENAME=backup-$TIME.tar.gz    # Here i define Backup file name format.

# Parent backup directory
backup_parent_dir="/backup/httpdocs"

# Create backup directory and set permissions
backup_date=`date +%Y_%m_%d_%H_%M`
backup_dir="${backup_parent_dir}/${backup_date}"
echo "Backup directory: ${backup_dir}"
mkdir -p "${backup_dir}"
chmod 755 "${backup_dir}"

SRCDIR=/var/www/vhosts           # Location of Important Data Directory (Source of backup).

tar -cpzf $backup_dir/$FILENAME $SRCDIR --exclude=$SRCDIR/*/logs

find ${backup_parent_dir} -name '*' -type d -mtime +2 -exec rm -rfv "{}" \;

#END

Upvotes: 0

Views: 850

Answers (3)

Ilya Davidov
Ilya Davidov

Reputation: 11

  1. option 1 when not many files/directories to exclude

    tar -cpzf $backup_dir/$FILENAME --exclude=$SRCDIR/dir_ignore --exclude=$SRCDIR/*.log $SRCDIR

or if you have many entries to exclude much better done it in file

tar -cpzf $backup_dir/$FILENAME -X /path/to/exclude.txt $SRCDIR

where /path/to/exclude.txt file looks like

/var/www/dir_to_ignore 
/var/www/*.log

you cannot use variables,but can use wildcards

  1. second question answered very good by both guys before,i personalty love

    find ${backup_parent_dir} -type f -name 'backup-*.tar.gz' -mtime +7 -delete

Upvotes: 1

user2182349
user2182349

Reputation: 9782

  1. Use the --exclude option

    tar -cpzf $backup_dir/$FILENAME $SRCDIR --exclude=$SRCDIR/*/logs

  2. Name your backup files with an identifier that is derived from the day of the week. This will ensure the new file for each day will overwrite any existing file.

 Day:
    a   Day of the week - abbreviated name (Mon)
    A   Day of the week - full name (Monday)
    u   Day of the week - number (Monday = 1)
    d   Day of the month - 2 digits (05)
    e   Day of the month - digit preceded by a space ( 5)
    j   Day of the year - (1-366)
    w   Same as 'u'

From: http://ss64.com/bash/date.html

For example:

TARGET_BASENAME= `date +%u`

Upvotes: 1

Renaud Pacalet
Renaud Pacalet

Reputation: 29040

Exclude from tar:

tar -cpzf $backup_dir/$FILENAME --exclude=/var/www/sites/abc.com/logs $SRCDIR

Find and delete old backups:

find ${backup_parent_dir} -type f -name 'backup-*.tar.gz' -mtime +7 -delete

The filter of find is conservative: selecting names that match backup-*.tar.gz probably renders the -type f (files only) option useless. I added it just in case you also have directories with such names. The -mtime +7 option is to be checked by you because older than 7 days is not accurate enough. Depending on what you have in mind it may be +6, +7 or +8. Please have a look at the find man page and decide by yourself. Note that the selection of backups to delete is not based on their names but on their date of last modification. If you modify them after they are created it may not be what you want. Let us know.

Upvotes: 1

Related Questions