سؤال

انا املك زوج من البرامج النصية للباش ، 1 التي تتفق mysql dbs ، والثاني لتطهير النسخ الاحتياطية القديمة.

لقد اعتمدت دائمًا في التاريخ ، لذلك يتم تطهير أي ملفات أقدم من 7 أيام ، وتم إنشاء نسخ احتياطية جديدة يوميًا. وكانت النتيجة مجموعة من النسخ الاحتياطية 7 أيام.

حسنًا الآن بعد أن أصبح لدي 20+ DBS لقد فصلت هذه الوظيفة اليومية إلى وظيفة يوميًا وأسبوعيًا. إذا واصلت استخدام طريقة البحث والتاريخ ، فسوف أخسر الأسبوع الماضي.

سؤال

كيف يمكنني فرز إخراج الاكتشاف حسب التاريخ ، ثم تطهير جميع ما عدا 7. (لذلك حتى لو كان عمره شهرين ، يبقى)

هذا بقدر ما حصلت عليه ، لكنه ليس كما هو متوقع. ربما يكون استخدام LS هو الخيار الأفضل 9find هو بقايا من استخدام التاريخ كمعايير وحيد لتطهيرها)

OLDFILES=`find -regex .*sql.*`

set -- $OLDFILES

if [ -z $1 ]
then
    printf "\tNo files matching purge criteria\n" | tee -a $LOGFILE
else
    printf "\tSQL Files being Delete from $HERE\n" | tee -a $LOGFILE
    printf "\t\t%s\n" $OLDFILES  | tee -a $LOGFILE
fi
# $RETAIN is the # of recent to keep
# so I want to grab a subset from 0 to (total - retain), and purge them
TOBURN=$(( $# - $RETAIN ))
printf " grab first %s files. %s  - %s" $TOBURN $# $RETAIN
if [ $TOBURN -gt 0 ]
then
    TOPURGE=("$@:0:$TOBURN")
    printf "\n\nREMOVING..\n\t\t%s\n" $TOPURGE
fi

الملفات الحالية

            ./webbmaster_bellarose_joomla_01-09-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-25-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-10-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-04-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-25-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-29-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-30-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-28-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-06-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-13-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-24-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-21-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-24-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-27-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-26-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-17-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-03-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-21-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-20-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-16-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-31-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-11-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-05-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-14-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-22-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-12-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-17-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-18-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-20-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-08-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-22-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-27-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-23-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-07-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-26-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-19-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-29-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-15-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-28-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-01-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-18-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-02-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-23-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-19-2009.sql.gz

أهداف للتطهير (والتي أريد تعيينها في متغير لـ RM -F $ var)

            ./webbmaster_bellarose_joomla_01-09-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-25-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-10-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-04-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-30-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-28-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-06-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-13-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-21-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-24-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-27-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-17-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-03-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-21-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-20-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-16-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-31-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-11-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-05-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-14-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-22-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-12-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-17-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-18-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-20-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-08-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-22-2009.sql.gz
            ./webbmaster_bellarose_joomla_12-23-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-07-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-26-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-19-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-29-2009.sql.gz
            ./webbmaster_bellarose_joomla_01-15-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-01-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-18-2010.sql.gz
            ./webbmaster_bellarose_joomla_01-02-2010.sql.gz
            ./webbmaster_bellarose_joomla_12-19-2009.sql.gz

(التواريخ 1/23 - 1/29 تركت سليمة)

هل كانت مفيدة؟

المحلول

يرجى القيام بنفسك (وكل شخص آخر) معروفًا كبيرًا وابدأ في الكتابة في التواريخ ISO-8601 تنسيق (أي yyyy-mm-dd).

على سبيل المثال بدلا من

./webbmaster_bellarose_joomla_12-29-2009.sql.gz

سيكون لديك

./webbmaster_bellarose_joomla_2009-12-29.sql.gz

القيام بذلك العديد من الفوائد, الأهم من ذلك هنا أن الترتيب الزمني والأرجية يصبح متطابقًا. لا حاجة للعثور ، فقط اذكر الملفات ، والترتيب العكسي ، وحذف الأسطر السبعة الأولى وحذف أي ملفات متبقية:

ls *sql* | tac | sed 1,7d | tr '\012' '\000' | xargs -0 --no-run-if-empty rm

تحديث: ماذا تقصد بـ "المضي قدمًا"؟ كما يمكنني أن أقول إن حلي يحل مشكلتك 100 ٪:

/tmp/so>cat existing_files
./webbmaster_bellarose_joomla-2009-12-17.sql.gz
./webbmaster_bellarose_joomla-2009-12-18.sql.gz
./webbmaster_bellarose_joomla-2009-12-19.sql.gz
./webbmaster_bellarose_joomla-2009-12-20.sql.gz
./webbmaster_bellarose_joomla-2009-12-21.sql.gz
./webbmaster_bellarose_joomla-2009-12-22.sql.gz
./webbmaster_bellarose_joomla-2009-12-23.sql.gz
./webbmaster_bellarose_joomla-2009-12-24.sql.gz
./webbmaster_bellarose_joomla-2009-12-25.sql.gz
./webbmaster_bellarose_joomla-2009-12-26.sql.gz
./webbmaster_bellarose_joomla-2009-12-27.sql.gz
./webbmaster_bellarose_joomla-2009-12-28.sql.gz
./webbmaster_bellarose_joomla-2009-12-29.sql.gz
./webbmaster_bellarose_joomla-2009-12-30.sql.gz
./webbmaster_bellarose_joomla-2009-12-31.sql.gz
./webbmaster_bellarose_joomla-2010-01-01.sql.gz
./webbmaster_bellarose_joomla-2010-01-02.sql.gz
./webbmaster_bellarose_joomla-2010-01-03.sql.gz
./webbmaster_bellarose_joomla-2010-01-04.sql.gz
./webbmaster_bellarose_joomla-2010-01-05.sql.gz
./webbmaster_bellarose_joomla-2010-01-06.sql.gz
./webbmaster_bellarose_joomla-2010-01-07.sql.gz
./webbmaster_bellarose_joomla-2010-01-08.sql.gz
./webbmaster_bellarose_joomla-2010-01-09.sql.gz
./webbmaster_bellarose_joomla-2010-01-10.sql.gz
./webbmaster_bellarose_joomla-2010-01-11.sql.gz
./webbmaster_bellarose_joomla-2010-01-12.sql.gz
./webbmaster_bellarose_joomla-2010-01-13.sql.gz
./webbmaster_bellarose_joomla-2010-01-14.sql.gz
./webbmaster_bellarose_joomla-2010-01-15.sql.gz
./webbmaster_bellarose_joomla-2010-01-16.sql.gz
./webbmaster_bellarose_joomla-2010-01-17.sql.gz
./webbmaster_bellarose_joomla-2010-01-18.sql.gz
./webbmaster_bellarose_joomla-2010-01-19.sql.gz
./webbmaster_bellarose_joomla-2010-01-20.sql.gz
./webbmaster_bellarose_joomla-2010-01-21.sql.gz
./webbmaster_bellarose_joomla-2010-01-22.sql.gz
./webbmaster_bellarose_joomla-2010-01-23.sql.gz
./webbmaster_bellarose_joomla-2010-01-24.sql.gz
./webbmaster_bellarose_joomla-2010-01-25.sql.gz
./webbmaster_bellarose_joomla-2010-01-26.sql.gz
./webbmaster_bellarose_joomla-2010-01-27.sql.gz
./webbmaster_bellarose_joomla-2010-01-28.sql.gz
./webbmaster_bellarose_joomla-2010-01-29.sql.gz
/tmp/so>cat targets_for_purging
./webbmaster_bellarose_joomla-2009-12-17.sql.gz
./webbmaster_bellarose_joomla-2009-12-18.sql.gz
./webbmaster_bellarose_joomla-2009-12-19.sql.gz
./webbmaster_bellarose_joomla-2009-12-20.sql.gz
./webbmaster_bellarose_joomla-2009-12-21.sql.gz
./webbmaster_bellarose_joomla-2009-12-22.sql.gz
./webbmaster_bellarose_joomla-2009-12-23.sql.gz
./webbmaster_bellarose_joomla-2009-12-24.sql.gz
./webbmaster_bellarose_joomla-2009-12-25.sql.gz
./webbmaster_bellarose_joomla-2009-12-26.sql.gz
./webbmaster_bellarose_joomla-2009-12-27.sql.gz
./webbmaster_bellarose_joomla-2009-12-28.sql.gz
./webbmaster_bellarose_joomla-2009-12-29.sql.gz
./webbmaster_bellarose_joomla-2009-12-30.sql.gz
./webbmaster_bellarose_joomla-2009-12-31.sql.gz
./webbmaster_bellarose_joomla-2010-01-01.sql.gz
./webbmaster_bellarose_joomla-2010-01-02.sql.gz
./webbmaster_bellarose_joomla-2010-01-03.sql.gz
./webbmaster_bellarose_joomla-2010-01-04.sql.gz
./webbmaster_bellarose_joomla-2010-01-05.sql.gz
./webbmaster_bellarose_joomla-2010-01-06.sql.gz
./webbmaster_bellarose_joomla-2010-01-07.sql.gz
./webbmaster_bellarose_joomla-2010-01-08.sql.gz
./webbmaster_bellarose_joomla-2010-01-09.sql.gz
./webbmaster_bellarose_joomla-2010-01-10.sql.gz
./webbmaster_bellarose_joomla-2010-01-11.sql.gz
./webbmaster_bellarose_joomla-2010-01-12.sql.gz
./webbmaster_bellarose_joomla-2010-01-13.sql.gz
./webbmaster_bellarose_joomla-2010-01-14.sql.gz
./webbmaster_bellarose_joomla-2010-01-15.sql.gz
./webbmaster_bellarose_joomla-2010-01-16.sql.gz
./webbmaster_bellarose_joomla-2010-01-17.sql.gz
./webbmaster_bellarose_joomla-2010-01-18.sql.gz
./webbmaster_bellarose_joomla-2010-01-19.sql.gz
./webbmaster_bellarose_joomla-2010-01-20.sql.gz
./webbmaster_bellarose_joomla-2010-01-21.sql.gz
./webbmaster_bellarose_joomla-2010-01-22.sql.gz
/tmp/so>sed 's/\(.\{29\}\)_\(..-..\)-\(....\)/\1-\3-\2/' existing_files | sort > existing_files.8601
/tmp/so>sed 's/\(.\{29\}\)_\(..-..\)-\(....\)/\1-\3-\2/' targets_for_purging | sort > targets_for_purging.8601
/tmp/so>mkdir files
/tmp/so>cd files
/tmp/so/files>xargs touch < ../existing_files.8601
/tmp/so/files>ls -1
webbmaster_bellarose_joomla-2009-12-17.sql.gz
webbmaster_bellarose_joomla-2009-12-18.sql.gz
webbmaster_bellarose_joomla-2009-12-19.sql.gz
webbmaster_bellarose_joomla-2009-12-20.sql.gz
webbmaster_bellarose_joomla-2009-12-21.sql.gz
webbmaster_bellarose_joomla-2009-12-22.sql.gz
webbmaster_bellarose_joomla-2009-12-23.sql.gz
webbmaster_bellarose_joomla-2009-12-24.sql.gz
webbmaster_bellarose_joomla-2009-12-25.sql.gz
webbmaster_bellarose_joomla-2009-12-26.sql.gz
webbmaster_bellarose_joomla-2009-12-27.sql.gz
webbmaster_bellarose_joomla-2009-12-28.sql.gz
webbmaster_bellarose_joomla-2009-12-29.sql.gz
webbmaster_bellarose_joomla-2009-12-30.sql.gz
webbmaster_bellarose_joomla-2009-12-31.sql.gz
webbmaster_bellarose_joomla-2010-01-01.sql.gz
webbmaster_bellarose_joomla-2010-01-02.sql.gz
webbmaster_bellarose_joomla-2010-01-03.sql.gz
webbmaster_bellarose_joomla-2010-01-04.sql.gz
webbmaster_bellarose_joomla-2010-01-05.sql.gz
webbmaster_bellarose_joomla-2010-01-06.sql.gz
webbmaster_bellarose_joomla-2010-01-07.sql.gz
webbmaster_bellarose_joomla-2010-01-08.sql.gz
webbmaster_bellarose_joomla-2010-01-09.sql.gz
webbmaster_bellarose_joomla-2010-01-10.sql.gz
webbmaster_bellarose_joomla-2010-01-11.sql.gz
webbmaster_bellarose_joomla-2010-01-12.sql.gz
webbmaster_bellarose_joomla-2010-01-13.sql.gz
webbmaster_bellarose_joomla-2010-01-14.sql.gz
webbmaster_bellarose_joomla-2010-01-15.sql.gz
webbmaster_bellarose_joomla-2010-01-16.sql.gz
webbmaster_bellarose_joomla-2010-01-17.sql.gz
webbmaster_bellarose_joomla-2010-01-18.sql.gz
webbmaster_bellarose_joomla-2010-01-19.sql.gz
webbmaster_bellarose_joomla-2010-01-20.sql.gz
webbmaster_bellarose_joomla-2010-01-21.sql.gz
webbmaster_bellarose_joomla-2010-01-22.sql.gz
webbmaster_bellarose_joomla-2010-01-23.sql.gz
webbmaster_bellarose_joomla-2010-01-24.sql.gz
webbmaster_bellarose_joomla-2010-01-25.sql.gz
webbmaster_bellarose_joomla-2010-01-26.sql.gz
webbmaster_bellarose_joomla-2010-01-27.sql.gz
webbmaster_bellarose_joomla-2010-01-28.sql.gz
webbmaster_bellarose_joomla-2010-01-29.sql.gz
/tmp/so/files>ls *sql* | tac | sed 1,7d | tr '\012' '\000' | xargs -0 --no-run-if-empty rm
/tmp/so/files>ls -1 ./*
./webbmaster_bellarose_joomla-2010-01-23.sql.gz
./webbmaster_bellarose_joomla-2010-01-24.sql.gz
./webbmaster_bellarose_joomla-2010-01-25.sql.gz
./webbmaster_bellarose_joomla-2010-01-26.sql.gz
./webbmaster_bellarose_joomla-2010-01-27.sql.gz
./webbmaster_bellarose_joomla-2010-01-28.sql.gz
./webbmaster_bellarose_joomla-2010-01-29.sql.gz
/tmp/so/files>comm -3 ../existing_files.8601 ../targets_for_purging.8601
./webbmaster_bellarose_joomla-2010-01-23.sql.gz
./webbmaster_bellarose_joomla-2010-01-24.sql.gz
./webbmaster_bellarose_joomla-2010-01-25.sql.gz
./webbmaster_bellarose_joomla-2010-01-26.sql.gz
./webbmaster_bellarose_joomla-2010-01-27.sql.gz
./webbmaster_bellarose_joomla-2010-01-28.sql.gz
./webbmaster_bellarose_joomla-2010-01-29.sql.gz
/tmp/so/files>

نصائح أخرى

إذا كانت الملفات جميعها في دليل واحد (أي أنك لا تحتاج إلى استخدام Find للتكرار على الرغم من الدلائل الفرعية) ، فيمكنك استخدام الوسيطة -T إلى LS (1) لطلب قائمة الملفات حسب الوقت ثم إزالة الإدخالات السبعة الأولى مع ذيل (1).

rm -f $(ls -t *sql* | tail -n +8)

تحرير: لقد أضفت -F في حالة قيام الاستبدال باستبدال قائمة فارغة

أو في حلقة ...

ls -t *sql* | tail -n +8 | while read file
    echo "Purging: $file"
    rm "$file"
done

الوسيطة إلى الذيل هو +8 لأنك تريد أن تبدأ في التخلص من السطر الثامن فصاعدًا (أي إسقاط الـ 7 الأولى).

بعد إزالة الملفات ، يمكنك فقط حرق ما تبقى.

OLDIFS="$IFS"
IFS=$'\n'
topurge=($(find -mindepth 1 -printf "%T@ %P\n" | sort -n -r | cut -d' ' -f 2- | tail -n +7))
IFS="$OLDIFS"

هنا فكرة واحدة:

oldest_to_keep=`find . -name \*.sql | xargs \ls -1rc | tail -7 | head -1`
find . -name \*.sql -not -samefile $oldest_to_keep -not -newer $oldest_to_keep

حيث يجب أن يكون الاكتشاف الثاني -exec rm \{\} ; إذا كنت تقنع نفسك هذا عاقل. قون

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top