diff --git a/TempCleaner/Readme.md b/TempCleaner/Readme.md new file mode 100644 index 0000000..4e413a6 --- /dev/null +++ b/TempCleaner/Readme.md @@ -0,0 +1,102 @@ +

Gereksiz Temp Temizleme

+ + +> UYARI : Bunu kullanmak herkesin kendi mesuliyetindedir. + + +

Elle temizleme

+ Kesin çalışır 2-3 günde bir yapsanız yeterli. + +> Depolama Kontrol +```console +df -h +``` +> Temp Silme +```console +rm -rf /tmp/* +``` +> Tekrar Depolama Kontrol +```console +df -h +``` +> Bu kadar. + +

Oto Temizleme

+ +Bu yöntem ile tx botlarının oluşturduğu gereksiz temp dosyalarını silerek depolamamızda yer açıyoruz. Kodu çalıştırdığınızda ve bunu takip eden her 12 saatte kullanılmayan temp dosyaları siliniyor. + +> 80gb dolmuş sunucuda denediğimde 64gb gereksiz dosya silindi. + +

Kurulum

+ +> Sunucuzda npm ve node.js kurulu ise bu kısmı geçebilirsiniz. + +Npm ve node.js kurulumu + +```console +# komutları sırasıyla girelim: +curl -sL https://deb.nodesource.com/setup_20.x -o /tmp/nodesource_setup.sh +sudo bash /tmp/nodesource_setup.sh +sudo apt install nodejs + +curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash +source ~/.bashrc +nvm install v20.10.0 +nvm use v20.10.0 +npm install -g npm@latest +``` +Screen Açalım + +> Screen indirelim. + +```console +apt install screen +``` + +> Screen oluşturalım. + +```console +screen -S tempcleaner +``` + +> Sonra yeni bir klasör oluşturalım. Ardından npm ile proje oluşturalım. Hepsini enter ile geçebilirsiniz. + +```console +cd $HOME +mkdir tempcleaner +cd tempcleaner +npm init +``` + +> tempCleaner.js dosyasını oluşturun. Bu repodaki index.js değiştirmeden yapıştırın. Ctrl+X Y sonra Enter ile kaydedin. + +```console +nano tempCleaner.js +``` + +> package.json dosyasını oluşturun. Ctrl+K ile hepsini sil. Githubdan package.json kopyala yapıştır. Ctrl+X Y sonra Enter ile kaydedin. + +```console +nano package.json +``` + +> En son aşağıdakileri çalıştıralım + +```console +npm install +node tempCleaner.js +``` + + Ctrl+A+D ile çıkabilirsiniz. +> Screen içine girmek için; + +```console +screen -r tempcleaner +``` + +Depolama kontrol kodu + +```console +df -h +``` +> Bu kod ile kullanılan ve boş depolama miktarınızı görebilirsiniz. diff --git a/TempCleaner/package.json b/TempCleaner/package.json new file mode 100644 index 0000000..60215de --- /dev/null +++ b/TempCleaner/package.json @@ -0,0 +1,16 @@ +{ + "name": "temp-cleaner", + "version": "1.0.0", + "description": "A Node.js script to clean unused temp files", + "main": "tempCleaner.js", + "scripts": { + "start": "node tempCleaner.js" + }, + "dependencies": { + "node-cron": "^3.0.0", + "fs-extra": "^10.0.0" + }, + "author": "NuriBartu", + "license": "MIT" + } + \ No newline at end of file diff --git a/TempCleaner/tempCleaner.js b/TempCleaner/tempCleaner.js new file mode 100644 index 0000000..dbf20ee --- /dev/null +++ b/TempCleaner/tempCleaner.js @@ -0,0 +1,50 @@ +const cron = require('node-cron'); +const fs = require('fs'); +const path = require('path'); +const { promisify } = require('util'); +const readdir = promisify(fs.readdir); +const stat = promisify(fs.stat); +const unlink = promisify(fs.unlink); + +const TEMP_DIR = '/tmp'; +const DAYS_UNUSED = 7; +const MILLISECONDS_IN_A_DAY = 24 * 60 * 60 * 1000; + +async function cleanOldTempFiles() { + try { + const files = await readdir(TEMP_DIR); + const now = Date.now(); + + for (const file of files) { + const filePath = path.join(TEMP_DIR, file); + const fileStat = await stat(filePath); + + // Son erişim tarihini kontrol et + const lastAccessTime = new Date(fileStat.atime).getTime(); + const fileAge = (now - lastAccessTime) / MILLISECONDS_IN_A_DAY; + + if (fileAge > DAYS_UNUSED) { + await unlink(filePath); + console.log(`Silindi: ${filePath}`); + } + } + } catch (error) { + console.error(`Hata oluştu: ${error.message}`); + } +} + +// Kod çalıştırıldığında temizlik yap +console.log('Kullanılmayan temp dosyaları temizleniyor...'); +cleanOldTempFiles().then(() => { + console.log('İlk temp dosya temizleme işlemi tamamlandı.'); +}); + +// Her 12 saatte bir çalışacak cron job tanımlaması +cron.schedule('0 */12 * * *', () => { + console.log('Kullanılmayan temp dosyaları temizleniyor...'); + cleanOldTempFiles().then(() => { + console.log('Temp dosya temizleme işlemi tamamlandı.'); + }); +}); + +console.log('Temp dosya temizleyici cron job başlatıldı.'); \ No newline at end of file