Script to Backup Files Over a Network Using Rsync

Written by BinnyVA on June 4, 2008 – 9:47 pm -

Script

This script will backup the specified files to another computer on your network. You can also use this to send your files to a remote server. This script compliments the last Rsync Backup script. Its possible to combine both the script together, I prefer to keep them separate.

The Setup

For this to work, you need to have a password-less login system over ssh. You should configure the remote system to accept your credentials by giving your public key to the remote server. If you are not sure how to do that, just leave a comment and I’ll make a post on how to set it up.

The configuration file is the same format as the one used in the last Rsync script. But in this case, the file name will be ‘rsyncnetworkbackup.config‘.

The Code


#!/usr/bin/perl

#The folder on the remote system that must be used to store the data
$backup_folder = '/home/neo/Backup'; #Final '/' must NOT be there.
# The user for whom we have set up the key based login
$backup_user = 'neo';
# The IP address/domain name of the remote system.
$backup_server = '192.168.0.30';

use File::Basename;
my $config_file = dirname($0) . "/rsyncnetworkbackup.config";
my @all_locations = removeComments(getFileContents($config_file));

foreach my $folder_locations (@all_locations) {
	my($folder,$backup_location) = split(/\s+/,$folder_locations);

	print "Backing up $folder to $backup_location ... ";
	`rsync -avze ssh $folder $backup_user\@$backup_server:\"$backup_folder/$backup_location\"`;
	print "Done\n";
}

sub getFileContents {
	my $file = shift;
	my @lines;

	open (FILE,$file) || die("Can't open '$file': $!");

	@lines=<FILE>;
	close(FILE);
	return @lines;
}

sub removeComments {
	my @lines = @_;

	@cleaned = grep(!/^\s*#/, @lines); #Remove Comments
	@cleaned = grep(!/^\s*$/, @cleaned); #Remove Empty lines

	return @cleaned;
}

Tags: , , , , ,
Posted in Networking, Scripting, Shell Scripts | 2 Comments »

Using Konqueror as an FTP Client

Written by BinnyVA on May 5, 2007 – 4:38 pm -

Konqueror Logo

As a web developer good FTP clients are a necessity for me. I have to upload the scripts to the server. I used to use gFTP for this. Good client - clean interface, easy to use. But not as powerful as I wanted. Then I found FireFTP - the extension for Firefox - it is a good client. But recently I found that my favorite application, Konqueror, can do it much better than all the other clients.

Didn’t know konqueror could be used as a FTP client? Just copy and paste this FTP URL into the address bar of Konqueror and see for yourself…
ftp://checksoftware.com/

Advantages

The main advantage of using Konqueror as the FTP client is that you are very familiar with it. All the shortcuts, the tricks, you use when browsing through your files are available when you connect to a remote server as well. Ctrl+C and Ctrl+V? The FTP client has got it. What if you want to browse two folders in the remote server at the same time? Konqueror can do it.

2 Pane View

Like many other FTP Clients, you can split Konqueror into two panes(Window->Split View Left and Right). This is perhaps the best way to use Konqueror as an FTP Client.

Konqueror using Split Window Interface

X Pane View

Unlike other FTP Clients, Konqueror goes further than just 2 panes. You can keep on splitting a window to make it ‘just perfect’ for you.

Tabbed View

If paned view is not enough, just open up a new tab - and continue splitting it.

Multiple Protocols - FTP/SFTP/FISH

Konqueror supports many protocols - among which these three are the most important to us…

fish
You can use fish if you have a shell account on the remote server. For example, fish://admin@linkdesk.com would open a connection for user binnyva to a remote server ‘linkdesk.com’.
ftp
File Transfer Protocol - you should be knowing this if you have come this far ;-)
sftp
Secure FTP.

Problems

So far I have found only one issue with it - I was not able to configure Konqueror to act as an FTP client properly when I was behind a proxy. I have reasons to believe that it was my fault - but I did spend some time trying. Could someone else confirm this?

Conclusion

Konqueror is the best FTP Client there is. Period.


Posted in Applications, KDE, Tools, Tools, Web Development | 6 Comments »

Internet Connection Sharing using iptables

Written by BinnyVA on April 16, 2007 – 4:26 pm -

Networking

It is very easy to setup an internet connection sharing in Linux system using iptables. This method can be used to share an internet connection from a Linux system(I used Fedora Core 6, but it should work on other distributions that support iptables). Another method of doing this is using a proxy server like squid.

Enable IP forwarding

Run as root

sysctl -w net.ipv4.ip_forward=1

To enable it in system startup, edit the file /etc/sysctl.conf and set

net.ipv4.ip_forward = 1

iptables

Run command as root

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
service iptables save

Configuration

The configuration should be like this

Router

Connected to the internet provider
IP : 192.168.1.1

Internet Connected System

eth0 (LAN Card 1)

Connected to router

  • IP : 192.168.1.10
  • Netmask : 255.255.255.0
  • Gateway : 192.168.1.1 (IP of the router)

eth1 (LAN Card 2)

Connected to the other system

  • IP : 192.168.0.20 (Not the same network as the first card)
  • Netmask : 255.255.255.0
  • Gateway : 192.168.1.1 (IP of the router)

Second System

LAN Card connected to the first system

  • IP : 192.168.0.30
  • Netmask : 255.255.255.0
  • Gateway : 192.168.0.20 (IP of the second Card in the first system)

Disclaimer

Linux networks is not a subject I am an expert on. So take my advice with a pinch of salt. The above procedure worked for me. YMMV.

Status

Some results of various commands are shown here. Check to see if it matches the result on your system.

# iptables -t nat -L POSTROUTING
Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
MASQUERADE  all  —  anywhere             anywhere
# cat /proc/sys/net/ipv4/ip_forward
1
# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Posted in Command Line, Fedora, Networking, Tutorials | 17 Comments »

Using SSH over a Proxy

Written by BinnyVA on April 9, 2007 – 1:42 am -

Cockscrew

Back in the days when I was working in a web development company, I had to ssh into some sites for setting up crons, importing database dumps etc. But there was a problem - my system was behind a proxy. And SSH did not have an option to connect through a proxy.

To use ssh over a proxy connection, you need a software called Cocrkscrew. Download the source and compile/install it.

Installation

wget http://www.agroman.net/corkscrew/corkscrew-2.0.tar.gz
tar -xzf corkscrew-2.0.tar.gz
cd corkscrew-2.0
./configure
make
su
[Enter Password]
make install

Configuration

Open the file ~/.ssh/config

Add this line..

ProxyCommand /usr/local/bin/corkscrew <proxy_server_ip> <proxy_server_port> %h %p

Example…

ProxyCommand /usr/local/bin/corkscrew 192.168.1.15 80 %h %p

Here 192.168.1.15 is the proxy server and 80 is the port.

After this is done, just connect to the remote server using ssh.

ssh google.com


Posted in Networking, Tools | 1 Comment »