开发者

how to delete extra files from backup with shell script?

开发者 https://www.devze.com 2023-03-13 03:38 出处:网络
A backup Shell Script #!/bin/bash backdest=/home/backup date=$(date \"+%F\") backupall=\"$backdest/arch-full-$date.tgz\"

A backup Shell Script

#!/bin/bash

backdest=/home/backup
date=$(date "+%F")

backupall="$backdest/arch-full-$date.tgz"
backuphome="$backdest/jary_p-$date.tgz"

tar -czpvf $back开发者_开发百科upall / --exclude=/home/* --exclude=/mnt/* --exclude=/media/* \
                        --exclude=/proc/* --exclude=/sys/* --exclude=/dev/*   \
                        --exclude=/tmp/* --exclude=/lost+found/*

tar -czpvf $backuphome /home/jary_p

Several(5) times later

there are Serveral(10) files in /home/backup

$ls /home/backup
backup.sh
arch-full-2011-05-13.tgz
arch-full-2011-05-25.tgz
arch-full-2011-06-01.tgz
arch-full-2011-06-09.tgz
arch-full-2011-06-11.tgz
jary_p-2011-05-13.tgz
jary_p-2011-05-25.tgz
jary_p-2011-06-01.tgz
jary_p-2011-06-09.tgz
jary_p-2011-06-11.tgz

How can I just keep the lastest 3 fiels(6) and delete extra files?

thanks

and, apologize my poor English.


ls -t $backdest/jary_p-*.tgz | tac | tail -n +3 | xargs rm

And repeat with $backupall's glob


I have found how to solved it

ls -htr *.tgz|head -n -6 | xargs rm
0

精彩评论

暂无评论...
验证码 换一张
取 消