Click to See Complete Forum and Search --> : Help - How to delete old backup files script?
gb_direwolf
10-05-2002, 06:06 AM
I use the below script to backup some info on my Red Hat 7.2 box and place it to a mounted dir on my Win2k box.
The files are named with the creation date at the start like this:
2002_1005_030000_docs.tar.gz
What I want to be able to do is get the script to check the folder with the backups in it and delete any previous copies that are older than x days.
How do I do this?
#!/bin/bash
# archives important files
clear
DATE=`date +%Y_%m%d_%H%M%S`
BACKUP_DOCS_NAME=$DATE"_docs.tar.gz"
echo Operation begun at $DATE
DOCS_DIR="/home/server/documents/"
ARCHIVE_DEST_DIR=/home/qoc-s/backup/
echo Files will be stored in $ARCHIVE_DEST_DIR
echo ===============================
echo
echo "tarring and gziping the files to $ARCHIVE_DEST_DIR$BACKUP from $DOCS_DIR"
tar -czf $ARCHIVE_DEST_DIR$BACKUP_DOCS_NAME $DOCS_DIR
So i assume you know the directory you are checking.
#!/bin/bash
#
# Variable
B_dir="/your/backup/dir"
# List all the files older than one day and echo them
find $VAR_DIR/* -maxdepth 0 -daystart -ctime +1 |xargs echo "$1"
Try that and if it seems to work, then change the 'xargs echo "$1"' into 'xargs rm -f "$1"'. You can give more specific kind of files by changing the find command. More info on how that work, read "man find". Be sure to test it first, you you don't accidentaly delete anything you need, i did first time i tried :).
gb_direwolf
10-05-2002, 12:48 PM
that did the trick.
Thanks!
:)
gb_direwolf
10-08-2002, 08:47 PM
Well this script was working fine but now I get this error message. The backup files are created however, they are not given the date as part of their file name.
What is causing this?
/home/backup: DATE: command not found
It might help if you use "" around the variables.
#!/bin/bash
# archives important files
clear
DATE=`date +%Y_%m%d_%H%M%S`
BACKUP_DOCS_NAME="$DATE""_docs.tar.gz"
echo "Operation begun at $DATE"
DOCS_DIR="/home/server/documents"
ARCHIVE_DEST_DIR="/home/qoc-s/backup"
echo "Files will be stored in $ARCHIVE_DEST_DIR"
echo "==============================="
echo
echo "tarring and gziping the files to $ARCHIVE_DEST_DIR$BACKUP from $DOCS_DIR"
tar -czf "$ARCHIVE_DEST_DIR/$BACKUP_DOCS_NAME" "$DOCS_DIR/*"
gb_direwolf
10-09-2002, 02:57 AM
I still get the DATE: command not found
when I run the script.
It did work before and now the DATE comand seems to be broken?
Originally posted by gb_direwolf
I still get the DATE: command not found
when I run the script.
Well only thing is that your date command shouldn't be capsed. Since that is the error (it can't find command "DATE" which is "date"). But other than this i can't figure out.
gb_direwolf
10-09-2002, 06:28 AM
hmmm
the thing is
DATE=`date +%Y_%m%d_%H%M%S`
the DATE part of the code is a variable which is assigned to the command =`date +%Y_%m%d_%H%M%S`
Why is my script now trying to execute the variable DATE as a command?
even if I put
GOATSNADS =`date +%Y_%m%d_%H%M%S`
I get the error
GOATSNADS: command not found
bwkaz
10-09-2002, 01:05 PM
You can't have spaces around your = signs.
gb_direwolf
10-10-2002, 12:10 AM
DOH!
yup, that was it. A space before the =! I am used to php where white space is ignored.
Thanks for the help.
:)