Archive for the ‘Scripts’ Category
Backing up my Macbook with rsync, SSH and Cron
I’ve created a simple script which syncs my documents folder with a remote computer. The files are transferred across the SSH protocol and the backup is performed daily with assistance of cron.
My rsync script is very simple:
#!/bin/bash rsync -avzr --delete-excluded --exclude="*.log" --exclude="*.aux" --exclude=".svn" --exclude=".classpath" --exclude=".cache" --exclude=".project" --exclude="*.class" --exclude="*.swp" --exclude=".DS_Store" --exclude=".metadata" -e "ssh -c blowfish" /Users/USERNAME/Documents USER@REMOTE_COMPUTER:REMOTE_DIRECTORY
It was quite tricky to set up the cron job until i realized that cron does know very little of the user’s system. The trick was to add SSH_AUTH_SOCK=/tmp/501/SSHKeychain.socket in front of the script refrence in crontab, so that it looks like:
55 21 * * * SSH_AUTH_SOCK=/tmp/501/SSHKeychain.socket /Users/USERNAME/etc/backup.sh
This will cause the backup script to run at 21:55 each night. For more info on cron, check out unixgeeks.org. Add the following at the end if you’d like to suppress the mail from cron: >& /dev/null.
Note that for this script to run, SSH has to be set up with keypairs authentication.
Simple scp script for copying to SSH enabled accounts
I frequently use scp to copy files to shell accounts. It’s quite annoying to type the full account info each time, so by writing a very small and very simple shell script some typing can be saved:
#!/bin/bash scp -r "$1" login@server.domain.com:$2
The script copies file $1 to the destination $2 on the server. With -r option directories are copied as well. The “” are for taking care of file/directory names with spaces in.