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

  1. open up Terminal.app (Applications/Utilities/…)
  2. run the command ’ssh-keygen -t rsa’
  3. You will be asked what file you want to save the key in, press return to accept the default.
  4. You will be asked for a passphrase. Hit return twice to have no passphrase.
  5. type ‘cd .ssh’ to enter the root ssh directory.
  6. type ‘cat id_rsa.pub >> authorized_keys2′ to add your public key to the list of authorized keys.
  7. type ‘chmod 600 id_rsa’ to change the permissions on that file.
  8. type ‘cd ..’ to return to your home directory
  9. type ’scp -r .ssh user@yourdomainname.com:~/’ to copy the ssh keys you just created to the webserver.
  10. ssh into the .ssh folder on the server and chmod 300 authorized_keys2.
  11. 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:

  1. open Terminal and type ‘cd Library/Crontab’
  2. 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.

  1. Use the arrow keys to navigate to the end of the “diskutil repairPermissions” line and hit return.
  2. 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.
  3. 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

Comments are closed.

map1 map2 map3 map4 map5 map6 map7 map8 map9 map10 map11 map12 map13 map14 map15 map16 map17 map18 map19 map20 map21 map22 map23 map24 map25 map26 map27 map28 map29 map30 map31 map32 map33 map34 map35 map36 map37 map38 map39 map40 map41 map42 map43 map44 map45 map46 map47 map48 map49 map50 map51 map52 map53 map54 map55 map56 map57 map58 map59 map60 map61 map62 map63 map64 map65 map66 map67 map68 map69 map70 map71 map72 map73 map74 map75 map76 map77 map78 map79 map80 map81 map82 map83 map84 map85 map86 map87 map88 map89 map90 map91 map92 map93 map94 map95 map96 map97 map98 map99 map1 map2 map3 map4 map5 map6 map7 map8 map9 map10 map11 map12 map13 map14 map15 map16 map17 map18 map19 map20 map21 map22 map23 map24 map25 map26 map27 map28 map29 map30 map31 map32 map33 map34 map35 map36 map37 map38 map39 map40 map41 map42 map43 map44 map45 map46 map47 map48 map49 map50 map51 map52 map53 map54 map55 map56 map57 map58 map59 map60 map61 map62 map63 map64 map65 map66 map67 map68 map69 map70 map71 map72 map73 map74 map75 map76 map77 map78 map79 map80 map81 map82 map83 map84 map85 map86 map87 map88 map89 map90 map91 map92 map93 map94 map95 map96 map97 map98 map99 map100