Loading

Friday, July 31, 2009

Linux local content backup and rotation script

### Script by jim ballowe - content.sh can be put into
### /etc/cron.dailly on a RH linux box once complete
### and will backup to TARGET location on local filesystem

### Define variables for naming the final backup file
DATE=`date +%Y%m%d-%H%M` # Datestamp e.g 20080109-2200
DOW=`date +%A` # Day of the week e.g. Monday
DNOW=`date +%u` # Day number of the week 1 to 7 where 1 represents Monday
DOM=`date +%d` # Date of the Month e.g. 27
M=`date +%B` # Month e.g January
W=`date +%V` # Week Number e.g 37
VER=2.5 # Version Number

SOURCE=/var/www/html ### source directory being backed up
TARGET=/var/backup ### target where backups will live
AGE=15 ### number days of backup to keep

### TEST ECHO OF STRINGS
# echo Date = "$DATE"
# echo Source = "$SOURCE"
# echo Target = "$TARGET"
# echo Age = "$AGE"

### Search SOURCE for all files last modified AGE or more days ago
### executes a recursive forced (-rf) remove (rm) on those files
### The "{}" (curly braces) is placeholder for exec to use where it will put filenames
### The "\;" tells exec that's the end of the statement.
### Replace "rm -rf" with "ls -la" to get a list of all the files that would be removed
### If you want to remove files with specific names or extensions use the "-name" argument.

### USE THIS TO REMOVE FILES
find "$TARGET" -mtime +"$AGE" -type f -exec rm -rf {} \;

### USE TO TEST THE FIND LOGIC
# find "$TARGET" -mtime +"$AGE" -type f -exec ls -alh {} \;


# tar up the content in the backup directory
/bin/tar -cvf "$TARGET"/"$DATE"_cvsroot.tar "$SOURCE"

# compress the tar file
/bin/gzip -f "$TARGET"/"$DATE"_cvsroot.tar

### Once it works, create symbolic link to /root/bin/backup.sh in /etc/cron.daily
# ln -s /etc/cron.daily/backup.sh /root/bin/backup.sh

# move the tar file to another location (if necessary)
# mv /var/backup/content/"$DATE"_content.tar.gz /mnt/backup/content/"$DATE"_content.tar.gz

No comments:

Post a Comment