Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temp Cleaner #43

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions TempCleaner/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<h1 align="center">Gereksiz Temp Temizleme</h1>


> UYARI : Bunu kullanmak herkesin kendi mesuliyetindedir.


<h1 align="center">Elle temizleme</h1>
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.

<h1 align="center">Oto Temizleme</h1>

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.

<h2 align="center">Kurulum</h2>

> 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.
16 changes: 16 additions & 0 deletions TempCleaner/package.json
Original file line number Diff line number Diff line change
@@ -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"
}

50 changes: 50 additions & 0 deletions TempCleaner/tempCleaner.js
Original file line number Diff line number Diff line change
@@ -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ı.');