BIPEDU

my ideas in action

backup script – bash + ssh + rsync

This days I was searching for a more intelligent way to backup my files.

The latest configuration that I have use a Atom board as home linux server and a client ( laptop) that copy file to the server.

The strategy is quite simple and clever, I say. At every hour I check if the IP of my sever is ok. Then I check if the total size of the folder is different from what was before. By this method I can decide very fast if something changed in my folder.

If yes, then I copy the files with rsync over SSH. Rsync makes another verification and decide what files are necessary to be copied.

At the end I put in a file the new size of the folder so that next time ( next hour) I can backup again.

The main idea here is that my server use HDD in standby , to minimize the power consume ( and also noise !)

The full core of my bash script is below:

#!/bin/bash

notok=0

## find yout IP
set `/sbin/ifconfig | grep ‘inet addr:’ | grep -v ‘127.0.0.1’ | cut -d: -f2 | awk ‘{ print $1 }’ `
#echo $1
## put your IP in string format
myip=”$1″
## put IP of the server in string format
targetip=”192.168.1.2″

## check if yout IP is 192.168.1.6. If YES , then you are not on correct network
## so is impossible to make backup
if [ $myip = “192.168.1.6” ]; then
env DISPLAY=:0 notify-send ‘Your IP is 192.168.1.6’ ‘not possible to make backup backup’
notok=1
fi

## If your IP is not identical with 192.168.1.2, then check if the server is alive.
## if yes, then you can make backup
if [ $notok -eq 0 ]; then

# check if router is alive
ping -c 1 192.168.1.1 > /dev/null
if [ $? -eq 0 ]; then
##        echo  IP is up.
sleep 1
### check the size of the personal folder, if changed –> make backup.
if [ `cat ~/.size_personal.txt` -ne `du -s ~/personal | cut -f1` ]; then
#            echo something changed !!

env DISPLAY=:0 notify-send ‘begin backup’

#change permissions:
find /home/user/personal/ -type f -exec chmod 644 {} \;
find /home/user/personal/ -type d -exec chmod 755 {} \;

## add v option for verbose for rsync
rsync -rah –log-file=/tmp/backup_rsync.log -e “ssh -i /home/user/scripts/sshkey/tc_atom”  /home/user/personal/  user@192.168.1.2:/mnt/sda1/personal
sleep 1
## put size of personal folder in a file, for neck backup.
du -s ~/personal | cut -f1 > ~/.size_personal.txt

env DISPLAY=:0 notify-send ‘backup done’ ‘made backup for : Personal’

fi
fi

fi

Advertisements

4 responses to “backup script – bash + ssh + rsync

  1. Vera October 16, 2012 at 3:36 PM

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog
    that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  2. Harvey December 19, 2012 at 3:50 AM

    Do you mind if I quote a small number of your blog posts as long as
    I provide credit and sources returning to your webpage:
    https://bipedu.wordpress.com/2012/07/11/backup-script-bash-ssh-rsync/.
    I will aslo ensure to give you the proper anchor text
    hyperlink using your blog title: backup script. Be sure to let me know if this is ok with you.

    Thank you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: