[shell] deleten van oude backups

Pagina: 1
Acties:

  • Haranaka
  • Registratie: September 2000
  • Laatst online: 08-09-2025
Zojuist een backup script gemaakt dat iedere dag de backup.tar copieerd naar een netwerkshare:
code:
1
cp /backup/dailybackup.tar /mnt/netwerk/dailybackup_$(date +%Y%m%d).tar

Om HD te sparen wil ik backups 7 dagen bewaren op het werkwerk waarna ze van mij weg mogen. Ik heb echter geen idee hoe ik dit aan moet pakken.
Heb net al een half uurtje lopen zoeken hier op GoT maar kon alleen dingen vinden over het maken van de backups zelf.

Ik zat te denken aan zoiets simpels:
code:
1
rm /mnt/netwerk/dailybackup_$(date +%Y%m%d-7).tar

Maar dat werkt dus niet :+ Iemand die een suggestie heeft?

...


  • CyBeR
  • Registratie: September 2001
  • Niet online

CyBeR

💩

Ik doe dat zo:

Bash:
1
2
3
MAXBACKUPS=7
BASEDIR=/backups/foo
find $BASEDIR/daily -xtype d -mtime +$[$MAXBACKUPS-1] -exec rm -rf {} ';'


Overigens gaat dit om directories (-xtype d), met '-xtype f' pakt 'ie files en wellicht wil je -ctime ipv -mtime (creation vs. modification). Ik had dat nodig om dat ik de backups na een dag compress met gzip en gzip nieuwe files maakt waardoor de creation time niet meer klopt. Achteraan het script draai ik nog een speciale 'touch' die de modification time aanpast:

Bash:
1
2
3
4
t=$(date +%H%M)
for i in $(find $BASEDIR/daily -maxdepth 1 -mindepth 1); do
        touch -t ${i//*\/}$t $i/* $i
done

All my posts are provided as-is. They come with NO WARRANTY at all.


  • Spider.007
  • Registratie: December 2000
  • Niet online

Spider.007

* Tetragrammaton

Ik gebruik daar dit script voor; die doet hetzelfde geintje na een maand of jaar :)
incremental from the 1st day of the week) for 1 week, keep weekly backups for a month, keep monthly backups for a year and keep yearly backups forever
Bash:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/sh
# This program will perform a backup of sourcedirectory to targetdirectory by using tar --bzip2
# This program will also remove old backups. It aims to keep a daily backup (incremental from the 1st day of the week) for 1 week, keep weekly backups for a month, keep monthly backups for a year and keep yearly backups forever
# Written by Spider.007@spider007.net

sourceDirectory=$1
targetDirectory=$2
tarOptions=$3

# keep yearly backups
if [ `date '+%j'` != '001' ]
then
        if [ `date '+%d'` = '01' ]
        then
                # remove backups 1 year old if it's the first day of the month
                oldFile=`date --date='-12 months' '+%Y.%m.%d'`.tar.bz2
                echo "Today is the first day of the month, removing backupfile $oldFile"
        else
                if [ `date '+%w'` = '0' ]
                then
                        # remove backups 1 month old if it's the first day of the week + create a new snapshot
                        oldFile=`date --date='-4 weeks' '+%Y.%m.%d'`.tar.bz2
                        tarOptions="$tarOptions --listed-incremental=$targetDirectory/snapshot"
                        echo "Today is the first day of the week, removing backupfile $oldFile"
                else
                        # if it is not the first day of the week / month / year backup using existing snapshot
                        useTempSnapshot='true'
                        tarOptions="$tarOptions --listed-incremental=$targetDirectory/snapshot.tmp"

                        if [ `date '+%d' --date='-1 week'` != '01' ]
                        then
                                # delete files 1 week old if that is not the first backup of the month!
                                oldFile=`date --date='-1 week' '+%Y.%m.%d'`.tar.bz2
                                echo "Today is not the first day of the week / month / year and 1 week ago it wasn't the first day of the month either, removing backupfile $oldFile"
                        fi
                fi
        fi
fi

if [ ! -z $oldFile ] && [ -e $targetDirectory/$oldFile ]
then
        echo Deleting old backup file: $oldFile
        rm $targetDirectory/$oldFile
fi

if [ $useTempSnapshot = 'true' ]; then
        cp $targetDirectory/snapshot $targetDirectory/snapshot.tmp
fi

tar --create --totals --verbose --bzip2 $tarOptions --file=$targetDirectory/`date '+%Y.%m.%d'`.tar.bz2 $sourceDirectory

if [ $useTempSnapshot = 'true' ]; then
        rm $targetDirectory/snapshot.tmp
fi

---
Prozium - The great nepenthe. Opiate of our masses. Glue of our great society. Salve and salvation, it has delivered us from pathos, from sorrow, the deepest chasms of melancholy and hate


  • Haranaka
  • Registratie: September 2000
  • Laatst online: 08-09-2025
Ik ben eruit, het was toch bijna net zomakkelijk als ik me had voorgesteld:

code:
1
rm /mnt/netwerk/dailybackup_$(date +%Y%m%d --date='7 days ago').tar

...