Nothing worse than losing all your work for one reason or another. Backups are essential. The steps below will create automatic backups of both files and databases. Each time the file runs further files are created. This is good from one point of view, in that backups over a set period of time can be reinstalled should any recent backups be corrupt. A directory flush of the backup directory can be carried out whenever necessary AFTER making sure the backups are either downloaded for safe-keeping or you are certain your file and database systems are working perfectly.
Step 1: Create a directory OUTSIDE of htdocs (or whatever you have called your root directory). Inside this directory create 2 others - one for the backups and another for files (this being one of them). I called mine 'hq' and then inside another directory called 'allbackups'.
Step 2: Create a file inside the 'allbackups' directory. Call it files_databases_bk (or something else)
Step 3: Add the code below to the newly created files_databases_bk file (alter it to suit)
# Backup Files & Database backup script
# For cron to work it needs to navigate to the same directory as this script
# Date and Time
# Compress all website files whilst retaining permissions
sudo tar -zcvpf $today-allfiles.tgz /Applications/Mamp/htdocs/
# ALL Database Backups
sudo /Applications/MAMP/Library/bin/mysqldump -u(hidden user name) -p(hidden password) -C --all-databases | gzip > $today-allsql.gz
# (hidden name of database) Database Backup - create new block of code for each database
sudo /Applications/MAMP/Library/bin/mysqldump -u(hidden user name) -p(hidden password) --opt (hidden name of database) > $today- (hidden name of database).sql
date ## echo the date at start
HIDDEN NAMES ABOVE?
These are your admin and passwords for mysql access. Remember to remove the bracket too.
Once the above directory and file is created you can run a test using the inbuilt mac Terminal (located in Applications Utilities). Navigate to where you have created the files_databases_bk file with something like this ...
If you are successful the Terminal prompt will alter to suit. Mine looks like this ...
You can also run a directory file search by typing this ...
If you in the right directory your newly created file should display. Then type this ...
sudo bash files_databases_bk
You will get your mac password prompt (mac password - not mysql password). Enter it and you should see all your files being backed up. Depending on the size of your websites and all the files you have inside the htdocs directory the file backup should be over in no time at all. Once the file compression and file move is over you will get a warning about using your password on the command line - just let the process run. At the end of the process you will see the date and be back at the prompt. Check the 'allbackups' directory to check if there are new files - 1 for all your files - 1 for ALL databases (compressed) and the rest for single database sql files (as many as you wanted to create). If everything is in order you can move onto creating a cron job - see below.
CRON JOB TO RUN EVERY MORNING AT 3AM (once)
Using the Terminal again carry out these steps
Step 1: stop the bash script asking for your mac password.
Type sudo visudo
Scroll down until you see this line and uncomment it
# %sudo ALL=(ALL) ALL
Alter to this ...
%sudo ALL=(ALL) ALL
Add this code ...
## Added By Me
%admin ALL=NOPASSWD: ALL
Press Shift ZZ to exit
Step 2: type crontab -e
Step 3: press the key i (for insert)
Step 4: enter this line ...
0 3 * * * * /bin/bash /Applications/MAMP/hq/allbackups/files_databases_bk
Step 5: press ESC to close down insert
Step 6: type shift ZZ to exit from cron
And that is it. If you want to check whether the above cron is working then alter the line to this first ...
*/2 * * * curl /Applications/MAMP/hq/files/files_databases_bk
Exit cron and check to see if files are created after just a few mins - 2 as above. If successful alter it back to ...
0 3 * * * * /bin/bash /Applications/MAMP/hq/files/files_databases_bk
Hopefully, if you have everything as it should be, you will have backups created once a day, every day at 3am.
FLUSHING THE ALLBACKUPS DIRECTORY
Create a file within the allbackups directory called flush_backups
Enter this code ...
# Delete gz, tgz and sql files
# sudo bash flush_backups
sudo rm *.gz
sudo rm *.tgz
sudo rm *.sql
You can run the above by navigating to the directory like so ...
And then running ...
sudo bash flush_backups
Only carry out the above after you have saved your previous good backups somewhere safe.