You are not logged in.

#1 2008-12-15 04:36:42

bruenig
Member
Registered: 2007-05-20
Posts: 175

So I rm -rf /etc today, but managed to recover, here's how

Today I was trying to rm -rf *.pacsave in /etc but of course put a space between the * and the .pacsave. Now before today if someone had said they deleted /etc, I would say "have fun reinstalling." Reinstalling is obnoxious and so I thought I would try to recover it all, the following is how to do that.

Now my circumstances were ideal, I was using screen, had a root terminal going and irssi going pointed to #archlinux on freenode. The first thing to know if you ever remove /etc, is DO NOT get out of your root terminal. If you get out of it, you are done. There is no switching back into root because there is no root user anymore. The basic strategy I decided to use was to just reinstall everything that ever had a file in /etc with pacman.

To do this, one needs a pacman.conf, this is where #archlinux came in handy, someone was able to upload a pacman.conf that I I could download via the ip address with wget and dump into /etc, some basic editing to it and it was good to go. But, pacman needs to be able to resolve the mirrors in pacman.conf, so I made an /etc/resolv.conf and added the opendns name servers to the file. I tested pacman and lo and behold it worked.

From here I used some bash+pacman cleverness to isolate only packages that had stuff in /etc and had pacman reinstall all of them (113 in total on my system). Now from messing up the first time, the advice I would give to anyone else here is to install filesystem and initscripts first because some packages try to create groups and if you don't have the files in place the installations fail miserably. Also, it is a good idea to reinstall all of them twice so that any errors caused by missing /etc files are gone the second time around.

A fairly hackish one time way to get a list of packages with /etc files.

for file in $(pacman -Q 2>/dev/null | awk '{print $1}'); do pacman -Ql $file 2>/dev/null | grep -F '/etc/'; done | grep -v '^error' | awk '{print $1}' | sort -u | tr '\n' ' ' > thing.list
pacman -S $(<thing.list)

This downloading took considerable time which gave me a chance to create a checklist of what I needed to do before reboot so as to make sure the system still worked. I needed to create a root password, needed to create my user and its groups, I needed to edit rc.conf to add all of my settings, I needed to edit fstab to put in my drives, I needed to edit inittab, sudoers, basically everything that you have ever edited in etc needs to be done again. Prioritize important stuff first like fstab, but eventually you are going to have to get around to all of it. Remember the cardinal rule in all of this, do not get rid of the root terminal until you are certain it will work, if you do, it is game over.

At this point, just reboot and hope you didn't screw up. You probably did somewhere, hopefully it isn't critical and you can just correct it in tty.

Last edited by bruenig (2008-12-15 04:40:13)

Offline

#2 2008-12-15 04:44:53

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: So I rm -rf /etc today, but managed to recover, here's how

Nice effort... Good to see someone doens't give up too easily wink

bruenig wrote:

Remember the cardinal rule in all of this, do not get rid of the root terminal until you are certain it will work, if you do, it is game over.

Just to be absolutely correct... There would be nothing stopping you from booting from the install CD/USB and then chrooting in to your damaged installation.

Personally, I'd just go:

mount -t nfs server:/srv/BackupData/servers/ /mnt/backup
cd /
tar xvzf /mnt/backup/hostname-LastNightsDate.tar.gz /etc/*
*wait while we travel back in time*
umount /mnt/backup

tongue

Last edited by fukawi2 (2008-12-15 04:45:12)

Offline

#3 2008-12-15 04:54:02

Ranguvar
Member
Registered: 2008-08-12
Posts: 2,549

Re: So I rm -rf /etc today, but managed to recover, here's how

Personally, I would have STOPPED, powered off (power button, don't risk writing to the hard drive), and used SystemRescueCd to try and recover. A simple delete is very, very often 100% recoverable _if_ you don't write to the disk after. The space is only marked as empty, but it's not until some app needs a place to write and it comes across that space.

Offline

#4 2008-12-15 05:24:13

louipc
Member
Registered: 2006-10-09
Posts: 85

Re: So I rm -rf /etc today, but managed to recover, here's how

Hah hah. Nice adventure.
Personally I would just run `git reset --hard`

Offline

#5 2008-12-15 05:30:48

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: So I rm -rf /etc today, but managed to recover, here's how

What is this world coming to bruenig? A helpful post from you? tongue

It's nice to see you made it though, but I didn't doubt that you'd succeed wink.

fukawi2 wrote:

Nice effort... Good to see someone doens't give up too easily wink
Personally, I'd just go:

mount -t nfs server:/srv/BackupData/servers/ /mnt/backup
cd /
tar xvzf /mnt/backup/hostname-LastNightsDate.tar.gz /etc/*
*wait while we travel back in time*
umount /mnt/backup

tongue

The thing is, he had no backup tongue. I'm not sure whether he learnt anything from this experience other than some more awk and grep magic tongue.


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#6 2008-12-15 10:41:03

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: So I rm -rf /etc today, but managed to recover, here's how

I think this is the way everyone learns about backups wink

Offline

#7 2008-12-15 15:34:09

bruenig
Member
Registered: 2007-05-20
Posts: 175

Re: So I rm -rf /etc today, but managed to recover, here's how

B is just a troll, can't imagine how he became a forum moderator, amirite or amirite?

pmw

Offline

#8 2008-12-16 03:50:06

sabooky
Member
Registered: 2006-11-02
Posts: 89

Re: So I rm -rf /etc today, but managed to recover, here's how

fukawi2 wrote:

I think this is the way everyone learns about backups wink

Sigh.. after losing a big chunk of /etc once. (wasn't my fault package manager blew things up).
And completely frying my ~ directory with a lot of work on there (that I never recovered.. not even sure what all was on there..)

You'd think that I'd learn to backup.. but nope.. here I am saying I'll set it up "tomorrow"... sigh sad

PS: I've though about making a git repo out of /etc and syncing that to my server.. maybe one day (read: "tomorrow" aka never) I'll do this..

Offline

#9 2008-12-16 05:24:06

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: So I rm -rf /etc today, but managed to recover, here's how

I know some Arch users do use version control for their /etc and even ~ smile. Don't have any names though...


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#10 2008-12-16 09:01:30

Svenstaro
Administrator
From: Germany
Registered: 2008-11-19
Posts: 388

Re: So I rm -rf /etc today, but managed to recover, here's how

Ranguvar wrote:

Personally, I would have STOPPED, powered off (power button, don't risk writing to the hard drive), and used SystemRescueCd to try and recover. A simple delete is very, very often 100% recoverable _if_ you don't write to the disk after. The space is only marked as empty, but it's not until some app needs a place to write and it comes across that space.

Assuming it was EXT3, how would you do that?

Offline

#11 2008-12-16 09:38:11

ninian
Member
From: United Kingdom
Registered: 2008-02-24
Posts: 726
Website

Re: So I rm -rf /etc today, but managed to recover, here's how

Svenstaro wrote:

Assuming it was EXT3, how would you do that?

+1, would you use TestDisk from the SystemRescue CD?

Offline

#12 2008-12-16 15:47:42

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: So I rm -rf /etc today, but managed to recover, here's how

B wrote:

I know some Arch users do use version control for their /etc and even ~ smile. Don't have any names though...

Me! my /etc is in svn, which works fine. My ~ is also in svn, I should migrate that go git once.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#13 2008-12-17 01:26:03

Ranguvar
Member
Registered: 2008-08-12
Posts: 2,549

Re: So I rm -rf /etc today, but managed to recover, here's how

Likely PhotoRec (misnomer) or TestDisc, yeah. Note that I've never used them.

Offline

#14 2008-12-17 02:38:28

sokuban
Member
Registered: 2006-11-11
Posts: 412

Re: So I rm -rf /etc today, but managed to recover, here's how

Nice recovery. I always find reading these stories interesting.

Though I wish I saw that hackish way to install all the packages in /etc. The other day I did a rm -r /usr/share (I don't have -i aliased so I don't need to put -f to use the deadly command.) which isn't nearly as bad as /etc, but I still went and reinstalled every package. Would have shaved a couple of min (and have been cooler) if I had only done so for the packages in /usr/share.

That was the first accidental rm -rf in my life. I learned to "cd before you rm -rf" the hard way :P

Last edited by sokuban (2008-12-17 02:45:39)

Offline

#15 2008-12-17 02:43:12

lldmer
Member
From: Amsterdam
Registered: 2008-05-17
Posts: 119

Re: So I rm -rf /etc today, but managed to recover, here's how

Dieter@be wrote:
B wrote:

I know some Arch users do use version control for their /etc and even ~ smile. Don't have any names though...

Me! my /etc is in svn, which works fine. My ~ is also in svn, I should migrate that go git once.

I have my config files in git as well. Copies of them though, because having your /etc/ in git wouldn't save you when you remove /etc/.git as well:P


For lack of better words: chair, never, toothbrush, really. Ohw, and fish!

Offline

#16 2008-12-18 05:47:49

vogt
Member
From: Toronto, Canada
Registered: 2006-11-25
Posts: 389

Re: So I rm -rf /etc today, but managed to recover, here's how

lldmer wrote:

I have my config files in git as well. Copies of them though, because having your /etc/ in git wouldn't save you when you remove /etc/.git as well:P

You forgot that the etc repo is probably going to be mirrored somwhere, so you're probably only a `git clone' away from getting a recent backup of /etc.

Then again, I don't really see what advantages a revision control system has over regular backup systems (ex rdiff-backup) for plain old config files. To me it just seems like its one less interface to learn.

Offline

#17 2008-12-19 01:40:53

Maki
Member
From: Skopje, Macedonia
Registered: 2007-10-16
Posts: 353
Website

Re: So I rm -rf /etc today, but managed to recover, here's how

So the lesson here is why use alias when all the fun is repairing stuff smile


If it ain't broke, broke it then fix it.

Offline

Board footer

Powered by FluxBB