Simple bash script help (to extract and restore specific files)
I need a simple bash for loop to achieve the following sequence of commands
(background of issue is that i have an old server that was defaced, and i dont want to restore everything from backups just the indexes defaced)
Here is what i want to achieve :
(put this as a variable)
cp ../weekly/VARIABLE.tar.gz ./
Now i need to strip the variable from .tar.gz extension
obviously i need the delimiters for each line ; or none .. i also need to know how to set the find results in variables and how to strip the variables... i did some bash search but my box is now offline and need to execute commands rather than testing when its back online.
ps: This box only contains several users defaced, so its much better if i can array the users list
like array(a,b,c,d,e) and just run it for those.
Ok. Try below script at /backup/cpbackup/weekly: (Considering you have cpanel box)
ls /var/cpanel/users/ >> userlist.txt
Then delete first 3 rows from userlist.txt which have entries ./ ../ and space.
Then in a shell script put below code:
for i in `userlist.txt`
tar -xzvf $i.tar.gz
tar -xzvf homedir.tar.gz
cp homedir/public_html/index* /home/$i/public_html/
Hooe this will help you Let me know how it goes in your box.
thanks this looks promising, especially now that the variables are in a text i can edit, i can use that variable for a sequence of commands i need, and i can define which and which i need to do this command for without worrying about looping over un-affected accounts.
all in all ill do it live tonight and ill update this thread
ok well it threw few errors, so i had to search again and i couldnt see why it isnt reading users, it actually threw erros saying line 9 etc :user
each time changes line but still error user
so i figured it might not be reading the user file but rather the loop from user whatever it considers it as a word maybe
So i digged for a while loop after cat file
here is what worked for me modifying your text, and modified the tar commands to just extract what i need
cat user | while read LINE ; do
tar zxvf /two/cpbackup/old/$LINE.tar.gz $LINE/homedir.tar
tar xvf $LINE/homedir.tar ./public_html/index*
mv ./public_html/index* /home/$LINE/public_html/
rm -rf $LINE
rm -rf public_html/
This works well, although to me its not final, since it worked and tested, now ill add extract to subfolders indexes too, and replace all
and ofcourse add a chown command to the end of the file...
That should do it,
Thread solved, thanks for the replies, and thanks avi0 for ur time