S
S
Sergey Abramov2011-07-30 04:21:36
PowerShell
Sergey Abramov, 2011-07-30 04:21:36

bash question

there are backups with the following name: backup-YYYMMDD.tar.gz. are done daily. you need to write a script that deletes files that were made earlier, for example, seven days ago. help me please.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
O
ob1, 2011-07-30
@ob1

Here is a small script. Didn't check. Do you drink it yourself?

#!/bin/sh

DAY=7
TDIR=ltmp

mkdir ${TDIR}

while [ ${DAY} -ge 0 ]; do
    mv backup-`date -d "${DAY} day ago" +%Y%m%d`.tar.gz ${TDIR}
    let DAY=${DAY}-1
done

rm -f backup-*.tar.gz
mv ${TDIR}/backup-*.tar.gz .
rmdir ${TDIR}

M
Monca, 2011-07-30
@Monca

Make cyclic backups, there will be no date in the name and the newest one will overwrite the oldest one.

I
Ivan, 2011-07-30
@iSage

find -type f -mtime 7 -execdir rm -f {} \;

For example.
man find

M
Maxim, 2011-07-30
@Maxim_ka

Once I used such a script to clean up the var directory from unnecessary things, look at it, and redo it as you like.
#cleanup
#Для работы программы требуются права root.
LOG_DIR=/var/log
LOG_SQUID_DIR=/var/log/squid
ROOT_UID=0 #Только пользователь с &UID 0 имеет привилегии root.
LINES=10 #Количество сохроняемых строк по-умолчанию.
E_XCD=66 #Невозможно сменить каталог?
E_NOTROOT=67 #Признак отсутствия root-привилегий.
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Для работы сценария требуются права root."
exit $E_NOTROOT
fi
if [ -n "$1" ]
# Полная наличия аргумента командной строки.
then
lines=$1
else
lines=$LINES #Значение по-умолчанию, если число не заданно в командной строке
fi
cd $LOG_DIR
if [ `pwd` != "$LOG_DIR" ]
then
echo "Невозможно перейти в католог $LOG_DIR."
exit $E_XCD
fi # Проверка каталога перед очисткой лог-файлов.
tail -$lines messages > mesg.temp # Сохранить последние строки в log-файле
mv mesg.temp messages
cat /dev/null > wtmp
echo "Лог-файлы очищенны."
cd $LOG_SQUID_DIR
if [ `pwd` != "$LOG_SQUID_DIR" ]
then
echo "Невозможно перейти в катало $LOG_SQUID_DIR."
exit $E_XCD
fi # Проверка каталога перед очисткой лог-файла.
tail -$lines access.log > mesg.temp
mv mesg.temp access.log
tail -$lines cache.log > mesg.temp
mv mesg.temp cache.log
tail -$lines store.log > mesg.temp
mv mesg.temp store.log
rm -rf *.gz
exit 0

R
rssdoor, 2011-11-22
@rssdoor

this topic is not related to uploading to dropbox?

E
Eddy_Em, 2011-11-22
@Eddy_Em

Have you tried logrotate to set up these backups?
He, after all, not only knows how to rotate logs ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question