Password Protect Website

There are two files that must be inside your root directory in order to password protect your site.

  • .htaccess — the .htaccess (hypertext access) file is a directory-level configuration file supported by several web servers, that allows for decentralized management of web server configuration.
  • .htpasswd — .htpasswd is a flat-file used to store usernames and password for basic authentication on an Apache HTTP Server.

Step 1

cd into the main directory you want to password protect.

cd /your/root/directory

Step 2

Create the .htaccess file

Replace /your/root/directory.

vi .htaccess
AuthType Basic 
AuthName "restricted area" 
AuthUserFile /your/root/directory/.htpasswd 
require valid-user

Step 3

Create the .htpasswd file

Create new htpasswd file and add user (you will be asked to enter your password).

Replace username.

htpasswd -c .htpasswd username

OR

Add additional users.

htpasswd .htpasswd username

Not only does password protecting your website keep users out, but it also removes your site from search results.

WordPress Backup on CentOS

Now that I’ve moved my website over to CentOS, I’ll be showing you how I do backups.

My Setup

  • VirtualBox – CentOS web server running WordPress
  • iMac – connected Drobo

Goal

  • Run daily mysql backups
  • Run weekly website backups
  • Only keep the latest two backups
  • Run daily rsync’s to my Drobo from my iMac

CentOS — Create a backup directory within your home folder

mkdir -p /home/tommy/backup/itsmetommy.com

CentOS — Backup MySQL Database

How to run it manually.

/usr/bin/mysqldump -u tommy -psupersecretpassword itsmetommy > /home/tommy/backup/itsmetommy.com/itsmetommy_$(date '+%Y%m%d-%H%M%S').sql

CentOS — Backup Website Directory

How to run it manually.

/bin/tar -czvf /home/tommy/backup/itsmetommy.com/itsmetommy_$(date '+%Y%m%d-%H%M%S').tar.gz -C /var/www/ itsmetommy.com

You can see that both files are located in /home/tommy/backup/itsmetommy.

ls -l /home/tommy/backup/itsmetommy.com
total 6731528
-rw-r--r-- 1 root root 9927065 Dec 25 21:55 itsmetommy_20151225-215541.sql
-rw-r--r-- 1 root root 6883148434 Dec 25 22:04 itsmetommy_20151225-215602.tar.gz

CentOS — Setup cron

No one wants to run backups manually, so lets add these commands as a cronjob.

crontab -e
@daily /usr/bin/mysqldump -u tommy -psupersecretpassword itsmetommy > /home/tommy/backup/itsmetommy.com/itsmetommy_$(date '+\%Y\%m\%d-\%H\%M\%S').sql
@weekly /bin/tar -czvf /home/tommy/backup/itsmetommy.com/itsmetommy_$(date '+\%Y\%m\%d-\%H\%M\%S').tar.gz -C /var/www/ itsmetommy.com

If you’re having problems, check your cron logs.

tail -f /var/log/cron

MAC — Setup rsync on my MAC

I first created a backup directory on my Drobo.

mkdir -p /Volumes/Drobo5D02/Backup/itsmetommy.com

How to run it manually.

rsync -hva -e ssh --delete --progress tommy@itsmetommy.com:/home/tommy/backup/itsmetommy.com/ /Volumes/Drobo5D02/Backup/itsmetommy.com

MAC — Setup cron

Note: My iMac is always on, so this isn’t a problem for me. Obviously you may need to revisit the timing or method if your computer is not always on and connected to the internet.

Run everyday at 1am.

crontab -e
0 1 * * * /usr/bin/rsync -hva -e ssh --delete  tommy@itsmetommy.com:/home/tommy/backup/itsmetommy.com/ /Volumes/Drobo5D02/Backup/itsmetommy.com > /dev/null

The above setup works, but I really only want to keep the last two backups of both the .sql and .tar.gz files, otherwise I’d run out of space.

CentOS — only keep the last two backups

How to run it manually.

cd /home/tommy/backup/itsmetommy.com; ls -t | grep sql | tail -n +3 | xargs rm; ls -t | grep tar | tail -n +3 | xargs rm

CentOS — Setup cron

crontab -e
@weekly cd /home/tommy/backup/itsmetommy.com && /bin/ls -t | /bin/grep sql | /usr/bin/tail -n +3 | /usr/bin/xargs /bin/rm
@weekly cd /home/tommy/backup/itsmetommy.com && /bin/ls -t | /bin/grep tar | /usr/bin/tail -n +3 | /usr/bin/xargs /bin/rm

Useful Linux find Commands

General

Find all files and directories starting from the current directory (including all subdirectories).

find .

Find all files starting from the current diretory (including all subdirectories).

find . -type f

Find all directories starting from the current directory (including all subdirectories).

find . -type d

Find all hidden files starting from the current directory (including all subdirectories).

find . -type f -name ".*"

Find all hidden directories starting from the current directory (including all subdirectories).

find . -type d -name ".*"

Find all files and directories starting from the current directory (including all subdirectories), and exclude hidden files.

$ find . -name '[!.]*'
find . \( ! -regex '.*/\..*' \) | sed 's/^..//'

Find all files and directories starting from the current directory (including all subdirectories) that have the name linkin in it (case insensitive).

  • -iname pattern
    • Like -name, but the match is case insensitive.
find . -iname *linkin*

Find all files and directories starting from the current directory (including all subdirectories) that have the name “linkin park” in it (case insensitive).

find . -iname "*linkin park*"

Find all files and directories starting from the current directory (including all subdirectories) that START with the words “Linkin Park” (case sensitive).

find . -name "Linkin Park*"

Find all files and directories starting from the current directory (including all subdirectories) that END with the words “Linkin Park” (case sensitive).

find . -name "*Linkin Park"

Find all .mp3 files starting from the current directory (including all subdirectories) (case insensitive).

find . -type f -iname *.mp3

Find all .mp3 files starting from the current directory (including all subdirectories) and delete them (case insensitive).

find . -type f -iname *.mp3 -delete

Find all files starting from the current directory (including all subdirectories) ending with .zip which are owned by the user tommye (case insensitive).

find . -type f -user tommye -iname "*.zip"

Find all files starting from the current directory (including all subdirectories) ending with .bak and delete them (case insensitive).

find . -iname "*.bak" -type f -print | xargs /bin/rm -f

Find all .jpg files within the current directory and archive them using tar to a file called jpeg.tgz (case insensitive).

find . -maxdepth 1 -iname "*.jpg" | xargs tar -czvf jpeg.tgz

Find all the files within the current directory with the words “linkin park” in it (case insensitive).

find . -maxdepth 1 -iname "*linkin park*"

Find all files starting from the current directory (including all subdirectories) which are owned by the user tommye.

find . -user tommye

Find all symbolic links starting from the current directory (including all subdirectories) and list the permissions.

find . -type l -ls

Find all files starting from root / that have permissions 777.

find / -type f -perm 0777

Find all directories starting from root / that have permissions 777.

find / -type d -perm 0777

Find a string inside a filename starting from the current directory (including all subdirectories).

Note: xargs can be used to process input from another command.

find . -type f -print | xargs grep -ni "STRING"
  • -n, –line-number
  • -i, –ignore-case

Find Files by Size

Find all files starting from the current directory (including all subdirectories) which are larger than the given size.

find . -size +100M

Find all files starting from the current directory (including all subdirectories) which are smaller than the given size.

find . -size -100M

Find all files starting from the current directory (including all subdirectories) which match the given size.

find . -size 100M

Empty Files & Directories

Find all empty files starting from the current directory (including all subdirectories).

find . -type f -empty

Find all empty directories starting from the current directory (including all subdirectories).

find . -type d -empty

Find all empty files starting from the current directory (including all subdirectories) and delete them.

$ find . -type f -empty -delete
find . -type f -empty -print0 | xargs -0 -I {} /bin/rm "{}"

Find all empty directories starting from the current directory (including all subdirectories) and delete them.

$ find . -type d -empty -delete
find . -type d -empty -print0 | xargs -0 -I {} /bin/rmdir "{}"

Find all empty files within the current directory and delete them.

$ find . -maxdepth 1 -type f -empty -delete
find . -type f -maxdepth 1 -empty -print0 | xargs -0 -I {} /bin/rm "{}"

Find all empty directories within the current directory and delete them.

$ find . -type d -maxdepth 1 -empty -delete<
find . -type d -maxdepth 1 -empty -print0 | xargs -0 -I {} /bin/rmdir "{}"
  • -empty : Only find empty files and make sure it is a regular file or a directory.
  • -type d : Only match directories.
  • -type f : Only match files.
  • -delete : Delete files. Always put -delete option at the end of find command as find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified.

mtime, ctime, atime and amin

“Unix keeps 3 timestamps for each file: mtime, ctime, and atime. Most people seem to understand atime (access time), as when the file was last read. There does seem to be some confusion between mtime and ctime though. ctime is the inode change time while mtime is the file modification time. “Change” and “modification” are pretty much synonymous. There is no clue to be had by pondering those words. Instead you need to focus on what is being changed. mtime changes when you write to the file. It is the age of the data in the file. Whenever mtime changes, so does ctime. But ctime changes a few extra times. For example, it will change if you change the owner or the permissions on the file.” —Source

  • -mtime n //modified n days ago
  • -ctime n //change n days ago
  • -atime n //accessed n days ago
  • -amin n //accessed n minuties ago
  • +n //More than ‘n’ days/minuties ago
  • -n //Less than ‘n’ days/minuties ago
  • n //Exactly ‘n’ days/minuties ago
$ find . type -f -mtime +2
find . type -f -mtime -2
find . type -f -mtime 2
find . type -f -ctime +2
find . type -f -ctime -2
find . type -f -ctime 2
find . type -f -atime +2
find . type -f -atime -2
find . type -f -atime 2
find . type -f -amin +2
find . type -f -amin -2
find . type -f -amin 2

Note: +0 is supposed to work and would mean files more than 0 days old.

IMPORTANT: find’s concept of a “day” has NOTHING TO DO WITH MIDNIGHT. It works in 24 hour increments. 1 = 24 hours.

Find all files and directories that were modified within the last 48 hours.

find . -mtime -2

Delete all files older than X days starting from the current directory (including all subdirectories).

find . -type f -mtime +X -delete

Delete all directories older than X days starting from the current directory (including all subdirectories).

find . -type d -mtime +X -delete

Delete all files newer than X days starting from the current directory (including all subdirectories).

find . -type f -mtime -X -delete

Delete all directories newer than X days starting from the current directory (including all subdirectories).

find . -type d -mtime +X -delete

Find & Copy

Find all files starting from the current directory with the name “linkin park” in it and copy them over to /Users/tommye/Desktop/LP (case insensitive).

  • Does NOT preserve path hierarchy
find . -type f -iname "*linkin park*" -exec cp -r {} /Users/tommye/Desktop/LP \;

Find all files within the current directory with the name “linkin park” in it and copy the files over to /Users/tommye/Desktop/LP (case insensitive).

  • Preserves path hierarchy
find . -type f -iname "*linkin park*" | cpio -pvdmu /Users/tommye/Desktop/LP
  • -p (pass) cpio -p reads the standard input to obtain a list of path names of files.
  • -v Verbose. Print a list of file names.
  • -d Creates directories as needed
  • -m Retain previous file modification time. This option is ineffective on directories that are being copied (mutually exclusive with -a).
  • -u Copy unconditionally (normally, an older file will not replace a newer file with the same name)

Find all directories within the current directory with the words “linkin park” in it and copy them to /Users/tommye/Desktop/LP (case insensitive).

find . -maxdepth 1 -type d -iname "*linkin park*" -exec cp -r {} /Users/tommye/Desktop/LP \;

Find all the files and directories starting from the current directory with the name “linkin park” in it and copy them over to /Users/tommye/Desktop/LP (case insensitive).

  • Does NOT preserve path hierarchy

Note: Keep in mind that Directories with the name “linkin park” in it will be copied over, but will not include its contents (it’s kind of useless in my opinion).

find . -iname "*linkin park*" -exec cp -r {} /Users/tommye/Desktop/LP \;

Find all the files and directories starting from the current directory with the name “linkin park” in it and copy them over to /Users/tommye/Desktop/LP (case insensitive).

  • Preserves path hierarchy
find . -iname "*linkin park*" > temp;rsync -arhv --files-from=temp . /Users/tommye/Desktop/LP;rm temp
  • -a, –archive archive mode
  • -r, –recursive # recurse into directories (meaning, include all sub-folders)
  • -h, –human-readable output numbers in a human-readable format
  • -v, –verbose # increase verbosity

Backup your WordPress Website to Dropbox on a Mac

We learned how to Backup & Restore a MySQL database HERE.
We learned how to tar a directory HERE.

Now lets combine this knowledge and backup our WordPress Website directly to Dropbox.

IMPORTANT: Keep in mind that I am running my website on my MAC (not a Linux distro).

You will need the following information:

  • Web Server Name (hostname)
  • MySQL User Name
  • MySQL Password
  • MySQL Database Name
  • Root Website Directory
  • Dropbox Directory Location

I am going to backup (dump) my MySQL Database into my websites root directory. Then I will zip the whole website directory and place it into my Dropbox folder.

mysqldump -u <USERNAME> -p<PASSWORD> <DATABASENAME> > /<WEBSITEROOTDIRECTORY>/<WEBSITENAME>.sql ; tar cvzf /<DROPBOXDIRECTORY>/<WEBSITENAME>.tar.gz /<WEBSITEROOTDIRECTORY>

Example:

mysqldump -u tommy -psupersecret itsmetommy > /Library/WebServer/Documents/itsmetommy/itsmetommy.sql ; tar cvzf /Users/Tommy/Dropbox/Backup/itsmetommy/itsmetommy.tar.gz /Library/WebServer/Documents/itsmetommy

MySQL – Backup & Restore A Database

Dump all databases

mysqldump -u <USERNAME> -p<PASSWORD> --all-databases > alldatabases.sql

Dump Multiple/Specific Databases

mysqldump -u <USERNAME> -p<PASSWORD> -databases db_name1 [db_name2 ...] > alldatabases.sql

Dump (Backup) A Single Database

mysqldump -u <USERNAME> -p<PASSWORD> <DATABASE_NAME> > <DATABASE_NAME>.sql

Restore A Single Database

mysql -u <USERNAME> -p<PASSWORD> <DATABASE_NAME> < <DATABASE_NAME>.sql