There’re some easy file backup n’ sync online services such as Amazon S3 and Dropbox, however I find it not so handy when I want to sync my linux files hierarchially.

I initially used git to sync my .vimrc file and my own ebuild repo, but it is not cost-efficient as I actually pay little attention to the tracking commit things. So I ended up using rsync to synchronize my linux files to my dreamhost account.

Preparation

Assuming you’ve already had a dreamhost account with shell access priviledge (any other hosts with shell access will be fine as well). And you’re going to backup the following files on your system to the dreamhost server:

/home/yourname/.aliasrc
/home/yourname/.bashrc
/home/yourname/.config/openbox/
/home/yourname/files/
/home/yourname/.vimrc
/home/yourname/.vim/
/home/yourname/.xinitrc
/home/yourname/.Xdefaults
/etc/fstab
/etc/fonts/local.conf
/etc/make.conf
/etc/portage/package.*
/etc/X11/xorg.conf

Ofc you dont want others to see your loveletter.txt and bankaccount.txt under files directory, so the two files should be excluded from synchronization.

Put the following two lines into a textfile (such as ~/.rsync/excludes) which we’ll use it later:

/home/yourname/files/loveletter.txt
/home/yourname/files/bankaccount.txt

Howto

I wrote this script for sync:

#!/bin/bash

includes="
/home/yourname/.aliasrc
/home/yourname/.bashrc
/home/yourname/.config/openbox/
/home/yourname/files/
/home/yourname/.vimrc
/home/yourname/.vim/
/home/yourname/.xinitrc
/home/yourname/.Xdefaults
/etc/fstab
/etc/fonts/local.conf
/etc/make.conf
/etc/portage/package.*
/etc/X11/xorg.conf
"

rsync -avzuCRpP --delete --delete-excluded $includes --exclude-from "/home/yourname/.rsync/excludes" -e ssh USERNAME@HOSTNAME:linux/ 

The $includes contains the files you want to sync, * wildcard supported, and to exclude the private files you’ll need the --exclude-from paramater.

USERNAME@HOSTNAME is your dreamhost account and hostname, :linux/ means sync the files to the linux directory under your dreamhost home directory.

-a parameter makes sure your files will be synchronized recursively.

-C is used for ignoring files like .git, .svn, *.bak and so on, ofc we dont want to waste time on transfering these useless things.

-R is needed for transfering hierarchically, this means I can sync my /etc/make.conf file right under linux/etc/ directory on my dreamhost.

-P parameter is quite useful when the transaction is interrupted, you can continue the process somewhen later.

Now you can chmod +x scriptname before you run it first time.

Fetch the backup files from your dreamhost is easy, just to run:

rsync -e ssh -av USERNAME@HOSTNAME:linux/ backup/

The files will be transfered to the backup directory on the local computer.

You may need the Password-less SSH Login if you want to add this script to the cron job.

I use this cron job to make sure the script run automatically at 23:30 p.m. on every Monday and Thursday.

30 23 * * 1,4   punkid   sh /home/punkid/scripts/autobackup.sh

No Responses So Far

Leave a Reply ↓