auto-remove old backup script.

SupermanInNY

Verified User
Joined
Sep 28, 2004
Messages
428
Hi All,

I have a simply script that removes old backups.
For whatever reason,I can't get the script to run properly and I don't know what I'm missing:


find /backup/weekly/* -daystart -type d -ctime +5 -maxdepth 0 | xargs rm -rf


I modified the name of the current backup so that I won't loose it. (from 09-21-08 to 09-21a-08).
I created a test directory called 09-14-08 and touched 3 files:

touch test1.tar.gz
touch test2.tar.gz
touch test3.tar.gz

So, running the script now, should delete this directory and it's content.

host.server.com:/backup/weekly # ll
total 8
drwxr-xr-x 2 root root 4096 Sep 21 14:06 09-14-08
drw-r----- 6 root root 4096 Sep 21 04:18 09-21a-08

host.server.com:/backup/weekly # find /backup/weekly/* -daystart -type d -ctime +1

host.server.com:/backup/weekly # find /backup/weekly/* -daystart -type d -ctime -1
/backup/weekly/09-14-08
/backup/weekly/09-21a-08
/backup/weekly/09-21a-08/mysql
/backup/weekly/09-21a-08/apache
/backup/weekly/09-21a-08/custom
/backup/weekly/09-21a-08/custom/etc
/backup/weekly/09-21a-08/custom/etc/ssh
/backup/weekly/09-21a-08/custom/etc/httpd
/backup/weekly/09-21a-08/custom/etc/httpd/conf
/backup/weekly/09-21a-08/custom/var
/backup/weekly/09-21a-08/custom/var/spool
/backup/weekly/09-21a-08/custom/usr
/backup/weekly/09-21a-08/custom/usr/lib
/backup/weekly/09-21a-08/custom/usr/share
/backup/weekly/09-21a-08/custom/usr/local
/backup/weekly/09-21a-08/custom/usr/local/directadmin
/backup/weekly/09-21a-08/custom/usr/local/directadmin/conf
/backup/weekly/09-21a-08/custom/home
/backup/weekly/09-21a-08/bind


host.server.com:/backup/weekly # find /backup/weekly/* -daystart -type d -ctime +5 -maxdepth 0 | xargs rm -rf

host.server.com:/backup/weekly # ll
total 8
drwxr-xr-x 2 root root 4096 Sep 21 14:06 09-14-08
drw-r----- 6 root root 4096 Sep 21 04:18 09-21a-08

host.server.com:/backup/weekly #



What am I missing??

Thanks for any pointers.

-Alon.
 
man find

You need to use the "-exec rm {}" function of find.
 
Sorry,. I need spelled out solution.

find /backup/weekly/* -daystart -type d -ctime +5 -maxdepth 0 | xargs rm -rf

Used to work on other servers.
Now, it does nothing.
If you can rewrite this syntax correctly,. that will be great.

Thanks,

-Alon.
 
sorry, it's

find /backup/weekly/* -daystart -type d -ctime +5 -maxdepth 0 -exec rm {} \;


host.server.com:/backup/weekly # ll
total 16
drwxr-xr-x 2 root root 4096 Sep 21 14:06 09-14-08
drw-r----- 6 root root 4096 Sep 21 04:18 09-21a-08
-rwxr-xr-x 1 root root 77 Sep 24 12:42 alon.sh
-rwxr-xr-x 1 root root 79 Sep 24 11:01 old.sh

host.server.com:/backup/weekly # find /backup/weekly/* -daystart -type d -ctime +5 -maxdepth 0 -exec rm {} \;

host.server.com:/backup/weekly # ll
total 16
drwxr-xr-x 2 root root 4096 Sep 21 14:06 09-14-08
drw-r----- 6 root root 4096 Sep 21 04:18 09-21a-08
-rwxr-xr-x 1 root root 77 Sep 24 12:42 alon.sh
-rwxr-xr-x 1 root root 79 Sep 24 11:01 old.sh

host.server.com:/backup/weekly #


NO LUCK :(
 
host.server.com:/backup/weekly # ll
total 16
drwxr-xr-x 2 root root 4096 Sep 21 14:06 09-14-08
drw-r----- 6 root root 4096 Sep 21 04:18 09-21a-08

NO LUCK :(


your find command won't search for 5 days ago folder names but for folders changed 5 days ago.
Your folders were changed 21 sept so it won't delete them.
It doesn't matter if your folder is 09-14-08 or myfolder it's the same thing.
 
your find command won't search for 5 days ago folder names but for folders changed 5 days ago.
Your folders were changed 21 sept so it won't delete them.
It doesn't matter if your folder is 09-14-08 or myfolder it's the same thing.

I was wondering why we were being shown folders that were less than 5 days old. He thinks the file name has something to do with the date.

If you want to use the file/folder name as the date then you need a different approach altogether.
 
I was wondering why we were being shown folders that were less than 5 days old. He thinks the file name has something to do with the date.

If you want to use the file/folder name as the date then you need a different approach altogether.

OK.. I now understand the concept,. and yes, I was thinking it was researching the NAME of the folder rather than the timestamp on it.
I need a date check as part of the NAME of the folders to make this work.

find /backup/weekly/* -daystart -type d -ctime +2 -maxdepth 0 -exec rm -Rf {} \;

Works great when using the timestamp,. but I need a NAME of folder clean up.
Any pointers on that?

Thanks,

-Alon.
 
You could use something like this
Code:
OLDDATE=`date --date="5 days ago" +%m-%d-%Y`

for i in `ls /backup/weekly/`; do
  if [ $i -lt $OLDDATE ]; then
    `rm -rf /backup/weekly/${i}`;
  fi
done;

It is more than a single line command but if you put it inside of a script and have it run through cron, should work fine.
 
You could use something like this
Code:
OLDDATE=`date --date="5 days ago" +%m-%d-%Y`

for i in `ls /backup/weekly/`; do
  if [ $i -lt $OLDDATE ]; then
    `rm -rf /backup/weekly/${i}`;
  fi
done;

It is more than a single line command but if you put it inside of a script and have it run through cron, should work fine.


I'm having errors with this script, and rethinking this, it is actually not needed as the script itself does check and need to check for timestamp.
My problem was actually testing wise, where I didn't know how to change the timestamp, so I figured the naming of the directory would help.

Now that I know of touch capabilities, I can test with any date.

# touch -t 200809201201 alon.sh

host.server.biz:/backup/weekly # date
Fri Oct 3 06:51:45 IDT 2008

host.server.biz:/backup/weekly # ll
total 4
drw-r----- 6 root root 4096 Sep 20 12:01 09-28-08

host.server.biz:/backup/weekly # find /backup/weekly/* -daystart -type d -ctime +5 -maxdepth 0 -exec rm -Rf {} \;

host.server.biz:/backup/weekly # ll
total 4
drw-r----- 6 root root 4096 Sep 20 12:01 09-28-08

host.server.biz:/backup/weekly #

I'm still stuck at the starting point.

Anyone?

-Alon.
 
Code:
#!/bin/sh

backup_directory="/wwwroot/backup" # No trailing /
backup_time="7" # Remove backups how many days old? Number variable only!

#### Commands ####

find_files=`find $backup_directory -maxdepth 2 -mtime +$backup_time`

#### Begin Script ####

ver="Directory cleaner v1.0 by Andrew ([email protected])"

##

echo "Backup File Cleaner"
echo ""
echo "Looking for old backups... Older then $backup_time days old."
echo "----"
if [ -z "$find_files" ]; then                 
echo "None Found!"
else
echo "$find_files"
fi
echo "----"
echo ""
echo "If any folders were found they have been deleted."
find $backup_directory -mtime +$backup_time -delete
echo ""
echo "Done."
echo ""
 
Back
Top