Find all .htaccess files and make a backup copy in respective directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find all .htaccess files and make a backup copy in respective directories
# 1  
Old 05-29-2011
Find all .htaccess files and make a backup copy in respective directories

Hey guys,

I need to know how to locate all .htaccess files on the server and make a backup of them in the folder they reside before I run a script to modify all of them.

So basically taking dir1/.htaccess and copying it as dir1/.htaccess_bk
dir2/.htaccess copying as dir2/.htaccess_bk
etc...

Code:
find / -name ".htaccess" -exec cp (NOT SURE WHAT GOES HERE) {} \;

Can someone explain the proper cp usage for this example?

Much appreciated.

---------- Post updated at 01:01 PM ---------- Previous update was at 12:48 PM ----------

Nevermind,

I think I figured it out
Code:
find <start_directory> -iname ".htaccess" -exec cp {} {}_bak \;

---------- Post updated at 01:31 PM ---------- Previous update was at 01:01 PM ----------

Okay so that seemed to work as intended.

Now I'm curious... I have a copy of each original .htaccess file named .htaccess_bak sitting along side each original just incase my script to update them all doesnt work as planned and I need to revert to the backups..

How can I accomplish either copying the contents from the backup file to the original file, or renaming the backup files all the .htaccess to overwrite the originals?

Something like...

Code:
find / -name ".htaccess_bak" exec rename {} ?.htaccess?  \;

Can someone explain the proper syntax to accomplish something like this?

Thanks
# 2  
Old 05-29-2011
maybe can give date to bak files.
Code:
# find / -name ".htaccess" -exec cp {} "{}__$(date "+%F %H:%M:%S")__bak" \;

after
Code:
# find /dir1 -name ".htaccess__*__bak"
dir1/.htaccess__2011-01-25 02:38:23__bak
dir1/.htaccess__2011-01-25 02:41:30__bak

Code:
# cp "/dir/.htaccess__2011-01-25 02:38:23__bak" /dir/.htaccess

regards
ygemici
# 3  
Old 05-29-2011
Code:
tar cvf - dir*/.htaccess |gzip > /var/tmp/htaccess.`date +%Y%m%d%H%M%S`.bak.tar.gz

# 4  
Old 05-30-2011
I appreciate the responses, but I don't think that's quite what I'm looking for. Adding a date to the file... really doesn't get me anywhere, nor does a manual file by file copy to replace. Let me try to clarify...

I have approx 3,000 .htaccess files I am planning on targeting with a script to update them all at once removing a specified RE.

/dir1/.htaccess
/dir1/sub1/.htaccess
/dir1/sub2/.htaccess
/dir1/sub2/nest1/.htaccess
/dir2/.htaccess
/dir2/sub1/.htaccess
/dir2/sub2/.htaccess
/dir2/sub2/nest1/.htaccess
etc...

I've used:

Code:
find <start_directory> -iname ".htaccess" -exec cp -v {} {}_bak \;

To create a backup of each .htaccess to sit along side the original in every directory, so it now sits like this:

/dir1/.htaccess
/dir1/.htaccess_bak
/dir1/sub1/.htaccess
/dir1/sub1/.htaccess_bak
/dir1/sub2/.htaccess
/dir1/sub2/.htaccess_bak
/dir1/sub2/nest1/.htaccess
/dir1/sub2/nest1/.htaccess_bak
/dir2/.htaccess
/dir2/.htaccess_bak
/dir2/sub1/.htaccess
/dir2/sub1/.htaccess_bak
/dir2/sub2/.htaccess
/dir2/sub2/.htaccess_bak
/dir2/sub2/nest1/.htaccess
/dir2/sub2/nest1/.htaccess_bak
etc...

Now I'm going to run a script through all the original .htaccess files using find and sed -i to remove a selection of unwanted rewrite rules, while leaving the remaining rules in place. I'm pretty sure it is going to go as planned, but I want to be on the safe side and have backups in place, and a method to restore the original .htaccess files with the backups that reside along side them, if something goes wrong.

I need a method to find all .htaccess_bak files and rename them as .htaccess to overwrite the .htaccess files that are sitting along side them.

So in other words:

/dir1/.htaccess_bak is renamed to /dir1/.htaccess ... overwriting and replacing the .htaccess that is in place.
/dir1/sub1/.htaccess_bak is renamed to /dir1/sub1/.htaccess ... overwriting and replacing the .htaccess that is in place.
/dir1/sub2/.htaccess_bak is renamed to /dir1/sub2/.htaccess ... overwriting and replacing the .htaccess that is in place.

leaving a file structure like the original.

/dir1/.htacces
/dir1/sub1/.htaccess
/dir1/sub2/.htaccess
etc

But I need to do this for 3000 files at one time, not a cp or mv or rename... one by one by one manually..

I think this can be accomplished much along the lines of what I'm already using to create the backups. something like...

Code:
find <start_dir> -iname ".htaccess_bak" -exec rename {} {.htaccess} \;

I'm pretty sure the syntax there isn't correct, but I think it gives you an idea of what I'm trying to accomplish.

Any other thoughts or tips?

Thanks much!
BoxX
# 5  
Old 05-30-2011
The date with time is important, if you run your script two times by accidently, you will lost your updates easily.

tar all .htaccess files out into one file is one of ways to do the backup. You needn't rename it every time. But if you don't like this way, you can continue your way by rename.

Code:
find <start_directory> -iname ".htaccess" -exec cp {} {}_bak \;

if you need rollback the name, try this:

Code:
find <start_directory> -type f -name ".htaccess_bak" |while read file
do
  mv "$file" "${file%_bak}"
done

This User Gave Thanks to rdcwayx For This Post:
# 6  
Old 05-30-2011
Thanks for the explanation RDC,

I have the following two little bash scripts now, both working as hoped. =)

Code:
#! /bin/bash
#create_bak.sh
# find all .htaccess files and create a copy of them in their current dir as .htaccess_bak
# run this to backup .htaccess files prior to making proposed mass update.
find . -iname ".htaccess" -exec cp -v {} {}_bak \;

and

Code:
#! /bin/bash
#restore_bak.sh
# Find all .htaccess_bak files to restore, and change thier name to .htaccess to overwrite existing file. (vebose)
# Use this if the modifcations do not work as intended.
find . -type f -name ".htaccess_bak" |while read file
do
  mv -v "$file" "${file%_bak}"
done


Last edited by boxx; 05-30-2011 at 05:12 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and Copy Directories ONLY

I am trying to copy only the date specific folders/directories using the following command. However, the following copy command is also copying files from the root folder (OriginalFolder). find /OriginalFolder/ -type -d \{ -mtime 1 -o -mtime 2 \ } -exec cp -R {} /CopyTo/'hostname'__CopyTo/ \;... (2 Replies)
Discussion started by: apacheLinux
2 Replies

2. Shell Programming and Scripting

Copy files in respective directories

Hi Guys, I need to copy the files to respective directories based on name of the file. My script is something like below con=$1 for file in `cat $con` do file_tmp=$(ls -t1 $path| grep -i $file | head -n 1) echo $file_tmp if then cp $path$file_tmp $DIR/ap if then... (16 Replies)
Discussion started by: Master_Mind
16 Replies

3. AIX

How to backup a directory (sub-directories/files) files from one server on to other ?

Hello, Server A: /directory1/ Server B: /Backups/ i wanted to backup contents of /directory1 from "server A" on to "Server B" every 1 hour. If there is any change in (only new/differences) contents on serverA (directory1/) supposed to be backeup on next run. I did used rsync command to... (5 Replies)
Discussion started by: System Admin 77
5 Replies

4. Shell Programming and Scripting

Copy directories in make file

LD:=C:/WindRiver/diab/5.9.3.0/WIN32/bin/dld.exe CFILES:=$(wildcard *.c) OBJFILES:=$(subst .c,.o, $(CFILES)) OBJ_PATH:=$(PRJ_PATH)/out/ ADDOBJFILES := $(addprefix $(OBJ_PATH),$(OBJFILES)) FILES:=C:/EB/tresos/workspace/Test_Spi/output/src copyfiles: cp ... (3 Replies)
Discussion started by: ushacy
3 Replies

5. Shell Programming and Scripting

Copy respective path next to last column with awk

Hi to all, I have the short print out sample of the DOS command "dir S/" as showed below. Directory of C:\Program Files\Winamp\Skins\Bento\window 02/12/2010 11:35 p.m. <DIR> . 02/12/2010 11:35 p.m. <DIR> .. 11/12/2009 10:31 a.m. 13,556... (3 Replies)
Discussion started by: cgkmal
3 Replies

6. Shell Programming and Scripting

Replace several numbers with respective tag and make a single file

Dear All, I have a final output files as 736645|0| 13879|1| 495563|10| 127933|14| 4975|16| 49038|6| 53560|7| 135115|8| 178857|9| Now I want to replace second column with respective tag as per the value (4 Replies)
Discussion started by: jojo123
4 Replies

7. UNIX for Dummies Questions & Answers

how can i copy those files into other directories have the same name

how can i copy those files into other directories have the same name but different in the end i have files in directory called test: 10_10_asdadfsdfad.txt 10_10_11_asdawqefwkjasd.txt 10_10_11_12_asdafjjhoqwd.txt i want to put them in exist directory thart i have on my system i have... (1 Reply)
Discussion started by: t17
1 Replies

8. Shell Programming and Scripting

Make python script ignore .htaccess

I just wrote a tiny script with the help of ghostdog74 to search all my files for special content phrases. After a few modifications I now made it work, but one problem is left. The files are located in public_html folder, so there might also be .htaccess files. So I ignored scanning of that... (4 Replies)
Discussion started by: medic
4 Replies

9. UNIX for Dummies Questions & Answers

copy multiple files in different directories

I have a report file that is generated every day by a scheduled process. Each day the file is written to a directory named .../blah_blah/Y07/MM-DD-YY/reportmmddyy.tab I want to copy all of this reports to a separate directory without having to do it one by one. However, if I try cp... (3 Replies)
Discussion started by: ken2834
3 Replies
Login or Register to Ask a Question