Useful Rsync BackUp Command

Rsync is a utility software and network protocol for Unix-like systems that synchronizes files and directories from one location to another while minimizing data transfer by using delta encoding when appropriate.

It is a very useful too for backing up your data which is an absolute must.

Here is my command for backing up a users system on a Mac.

[shell]
rsync -auv –delete –ignore-errors –force –exclude ‘Movies’ –log-file=backup.log /Users/UserName/ /Volumes/UserName/BACKUP/UserName/
[/shell]

Lets explain some of these options I have used.

  • a : archive mode
  • u : update, skip files that are newer on the receiver
  • v : verbose, increase verbosity
  • –delete : This tells rsync to delete extraneous files from the receiving side (ones that aren’t on the sending side), but only for the directories that are being synchronized.
  • –ignore-errors: Delete even if there are I/O errors (this is needed if running on a live system)
  • –force : This tells rsync to force deletion of dirs even if not empty.
  • –exclude : Exclude a folder, use –exclude-from then file name with a list of directories if there are multiple
  • –log-file : Create a log file

Run this once a day and you should be all good, obviously first run takes a while but after that only new files will be copied across to your backup location and run time will be dramatically reduced.

Mac OS X launchd Automount Network Shares

As Apple seem to like to dictate what they should do with the world, they kindly got rid of the robust amazing cron feature of linux, oh and they also seem to of got rid of /etc/fstab … nice so basically you now have to make shell scripts and load them with launchd to do anything automated.

Thanks apple for making life harder as ALWAYS.

Well as this is quite mind boggling upon first look it needs to be written down as my brain just can’t fit all in.

Basically you need to do three things.

  • Create a shell script containing your commands you want to run.
  • Create .plist file with a load of jazz in it which can seem pretty confusing but not to apple, but more specifically contains the path to your shell script and the frequency you want it to run.
  • Copy the .plist file to /Library/LaunchDaemons/
  • Load the .plist file via launchctl load

The shell script:

[shell]
#!/bin/bash

local_admin_user="xxx" # an admin user name on the computer running this script
local_admin_pswd="xxx" # the password for the above user
mount_point="/Volumes/Myshare" # the name the mounted share point will be known as on this computer

host_name="my-host.local" # the domain name or IP of the system hosting the share point
share_point="MyShare" # the name of the remote share point
share_owner="userX" # the user name of the owner of the remote share point
share_pswd="xxx" # the password for the above user

# IMPORTANT:
# If the share_pswd contains any symbol characters, they need to be escaped.
# So, instead of: share_pswd="abc&123#"
# Do this: share_pswd="abc\&123\#"

#IMPORTANT
# Because I am running this at startup, the mount kept failing because the network wasn’t ready yet
# Adding this delay fixes that issue, only took 3 hours of debugging…. doh
sleep 15

# let’s clean up just in case the mount was dropped
echo $local_admin_pswd | sudo -S -u $local_admin_user umount $mount_point
echo $local_admin_pswd | sudo -S -u $local_admin_user rmdir $mount_point

# basic steps to mount the private share point
echo $local_admin_pswd | sudo -S -u $local_admin_user mkdir $mount_point
echo $local_admin_pswd | sudo -S -u $local_admin_user chown $local_admin_user $mount_point
echo $local_admin_pswd | sudo -S -u $local_admin_user chmod +rwx $mount_point
echo $local_admin_pswd | sudo -S -u $local_admin_user mount_afp afp://$share_owner:[email protected]$host_name/$share_point/ $mount_point
[/shell]

The .plist file

[html]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.apple.automount.readynas</string>
<key>ProgramArguments</key>
<array>
<string>/Scripts/automount_readynas.sh</string>
</array>
<key>UserName</key>
<string>root</string>
<key>UserGroup</key>
<string>wheel</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
[/html]

The copy command

[shell]
sudo cp /Scripts/com.apple.automount.readynas.plist /Library/LaunchDaemons/
[/shell]

Now don’t forget to load the .plist file

[shell]
sudo launchctl load /library/LaunchDaemons/com.apple.automount.readynas.plist
[/shell]

So with all that done, restart your mac and see if it works!

Thanks to these useful sources:

http://answer123.com/misc/launchd_notes.html
http://www.gregwillits.ws/articles/using_launchd_to_mount_afp_share_point_upon_startup

Leverage Browser Caching

Here is a very useful .htaccess snippet to leverage browser caching and help improve website speed.

[code]
<IfModule mod_expires.c>

# Enable expirations
ExpiresActive On

# Default directive
ExpiresDefault "access plus 1 month"

# My favicon
ExpiresByType image/x-icon "access plus 1 year”

# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"

# CSS
ExpiresByType text/css "access 1 month”

# Javascript
ExpiresByType application/javascript "access plus 1 year"

</IfModule>
[/code]

Source: http://fortheloveofseo.com/blog/performance/leverage-browser-caching-how-to-add-expires-headers/