Sunday, March 28th, 2004
Automagically backing up your website with OS X
I cobbled this together from several different sources, most notably the help desk from my server and an article from Mac OSX Hints.
The first thing you need is to be able to access your server space via ssh. You might need to contact your server help desk to get shell access.
Logging in
- open up Terminal.app (Applications/Utilities/…)
- run the command ’ssh-keygen -t rsa’
- You will be asked what file you want to save the key in, press return to accept the default.
- You will be asked for a passphrase. Hit return twice to have no passphrase.
- type ‘cd .ssh’ to enter the root ssh directory.
- type ‘cat id_rsa.pub >> authorized_keys2′ to add your public key to the list of authorized keys.
- type ‘chmod 600 id_rsa’ to change the permissions on that file.
- type ‘cd ..’ to return to your home directory
- type ’scp -r .ssh user@yourdomainname.com:~/’ to copy the ssh keys you just created to the webserver.
- ssh into the .ssh folder on the server and chmod 300 authorized_keys2.
- test it out by running ’ssh user@yourdomainname.com’, you should now be able to log in there without being prompted for a password.
Multiple computers
If you want to set multiple computers to login to your account, download the .ssh folder from your server to the second computer. Delete the two rsa files and follow the instructions above until the one that begins “type ’scp…”. Instead, type ’scp .ssh/authorized_keys2 user@yourdomainname.com:~/.ssh/’.
Backup Script
The next thing is scripts to login to your server space and backup your files to your computer. The first file backs up your files in a compressed file format for easy downloading.
#!/bin/sh
mysqldump -h databaseURL.mydomain.com -u user -ppassword database_name> /home/user/backup.sql
rm archive.tar.gz backup.sql.gz
tar cvf archive.tar home/user/images home/user/public
gzip archive.tar backup.sql
Create a new file in TextEdit and copy the above code into it. Save it as archive.sh. Replace url of the database, the username and password, the database name and the path to your root directory on the server in the mysqldump line. The rm line deletes the files already on the server. the tar line copies the selected files and folders into an archive. The last line compresses the tar and sql files ina gzip format. These can be uncompressed by Stuffit.
#! /bin/sh
newtime=`date +%m-%d-%y_%I%M%p`
ssh -l user mydomain.com /home/user/archive.sh
scp user@.mydomain.com:backup.sql.gz ~/Documents/Path/To/Backup Folder/backup_$newtime.sql.gz
scp user@.mydomain.com:archive.tar.gz ~/Documents/Path/To/Backup Folder/archive_$newtime.tar.gz
Again replace place-holders with your information. It will run archive.sh, download the backup files from your server to your computer and rename them with the date and time. Save this file as backup.sh.
Upload archive.sh to the root of your account and set the permissions so that it can be executed (chmod 755 backup.sh). Put the second in a Crontab folder in your Library folder and set its permissions the same way.
To test the files:
- open Terminal and type ‘cd Library/Crontab’
- type ’sh backup.sh’
You should see the list of files added to the tar archive and the progress as the two files are downloaded. The only thing left to do is create a cron job to run the backup.sh file automatically.
crontab
Back in Terminal, type ’setenv EDITOR “pico”‘. Pico is the easiest Unix editor to use. if you like another, you probably already knew all of this.
- Use the arrow keys to navigate to the end of the “diskutil repairPermissions” line and hit return.
- My cron job is
0 18 * * * /Users/user/Library/Crontab/backup.sh. Unix uses the 24 hour clock. I have it set to run at 6pm every night since I can almost always be found on my computer then. The crontab won’t run if A) you aren’t logged in or B) The computer is asleep. You can find more information on the cron settings at MacDevCenter.com. - Hit ‘ctrl X’ to close the crontab and return to save the changes.
That’s it. You can now rest easier knowing that if something happens to your website you won’t lose anything.
By Laura @ 10:26 pm in Tutorials







