Langsung ke konten utama

How to recover deleted file held open by Apache

If a file has been deleted but is still open, that means the file still exists in the filesystem (it has an inode) but has a hard link count of 0. Since there is no link to the file, you cannot open it by name. There is no facility to open a file by inode either.
Linux exposes open files through special symbolic links under /proc. These links are called /proc/12345/fd/42 where 12345 is the PID of a process and 42 is the number of a file descriptor in that process. A program running as the same user as that process can access the file (the read/write/execute permissions are the same you had as when the file was deleted).
The name under which the file was opened is still visible in the target of the symbolic link: if the file was /var/log/apache/foo.log, then the target of the link is /var/log/apache/foo.log (deleted).
Thus you can recover the content of an open deleted file given the PID of a process that has it open and the descriptor that it's opened on like this:
recover_open_deleted_file () {
  old_name=$(readlink "$1")
  case "$old_name" in
    *' (deleted)')
      old_name=${old_name%' (deleted)'}
      if [ -e "$old_name" ]; then
        new_name=$(TMPDIR=${old_name%/*} mktemp)
        echo "$oldname has been replaced, recovering content to $new_name"
      else
        new_name="$old_name"
      fi
      cat <"$1" >"$new_name";;
    *) echo "File is not deleted, doing nothing";;
  esac
}
recover_open_deleted_file "/proc/$pid/fd/$fd"
If you only know the process ID but not the descriptor, you can recover all files with:
for x in /proc/$pid/fd/* ; do
  recover_open_deleted_file "$x"
done
If you don't know the process ID either, you can search among all processes:
for x in /proc/[1-9]*/fd/* ; do
  case $(readlink "$x") in
    /var/log/apache/*) recover_open_deleted_file "$x";;
  esac
done
You can also obtain this list by parsing the output of lsof, but it isn't simpler nor more reliable nor more portable (this is Linux-specific anyhow).

Komentar

Postingan populer dari blog ini

Konfigurasi Proxy Server Squid Lusca + Mikrotik menggunakan Ubuntu 14.04

Kali ini aku akan membagikan dokumentasi yang akan aku terapkan pada pekerjaan kecilku di sebuah Internet Cafe di daerah jogja. karena permintaan dari client untuk menggunakan proxy sebagai web cache, aku akan mencoba memasang menggunakan komputer Jogloabang (http://jogloabang.com). sebelumnya kita akan mengenal yang namanya Proxy Server . Proxy Server adalah server yang bertindak sebagai perantara permintaan dari klien mencari sumber daya dari server lain. Seorang klien terhubung ke server proxy , meminta beberapa layanan, seperti file, koneksi, halaman web, atau sumber daya lain yang tersedia dari server yang berbeda dan server proxy mengevaluasi permintaan sebagai cara untuk menyederhanakan dan mengontrol kompleksitasnya. Proxy diciptakan untuk menambah struktur dan enkapsulasi untuk sistem terdistribusi. Saat ini, kebanyakan proxy adalah proxy web, memfasilitasi akses ke konten di World Wide Web dan menyediakan anonimitas. ( Sumber ). lalu apa itu Squid? Squid adalah sebuah daemo...

install php5.3 ubuntu 14.04, 16.04, 18.04 with cgi not fcgi

Halo, kali ini saya akan membagikan tutorial mudah untuk compile dan install php5.3 di ubuntu 14.04, btw.. di 18.04 juga bisa sebenernya, asalakan perinsipnya tahu.  kenapa saya pake cgi tidak fcgi? ya karena lebih enteng dari fcgi, dan fpm. nah biasanya, kita sering dihadapkan dengan kondisi dimana diharuskan memasang aplikasi lama, di server yang OS nya ke-kinian. oke.. bisa dipahami? untuk install php5.3 diubuntu, diperlukan keahlian dalam mengatasi error dan mencarinya di google. yuk mulai 1. Unduh file compile PHP wget -O php-5.3.29.tar.gz http://de1.php.net/get/php-5.3.29.tar.gz/from/this/mirror 2. install depedensi yang dibutuhkan: apt-get install apache2 apt-get install build-essential apt-get install libxml2-dev apt-get install libcurl4-openssl-dev pkg-config apt-get install libbz2-dev apt-get install libjpeg-turbo8-dev apt-get install libpng-dev apt-get install libfreetype6-dev apt-get install libmcrypt-dev libreadline-dev apt-get install libmysqlclient1...

Mengatasi Error DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled

Jika di mysql mendapatkan error seperti dibawah ini: 1418 (HY000) at line 10185: This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable) dapat diselesaikan dengan cara: 1. jalan kan di cmd/terminal: SET GLOBAL log_bin_trust_function_creators = 1; 2. Masukan di konfgurasi my.cnf : log_bin_trust_function_creators = 1;