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...

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

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...

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

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

Thanks

maybe can give date to bak files.

# find / -name ".htaccess" -exec cp {} "{}__$(date "+%F %H:%M:%S")__bak" \;

after

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

regards
ygemici

tar cvf - dir*/.htaccess |gzip > /var/tmp/htaccess.`date +%Y%m%d%H%M%S`.bak.tar.gz

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:

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...

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

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.

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

if you need rollback the name, try this:

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

Thanks for the explanation RDC,

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

#! /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

#! /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