Linux and Unix Networking How To
| Linux and Unix |
|---|
| Linux and Unix How To's |
| General · Backup and Networking · Disk · Look & Feel · Scripting |
| Linux and Unix Topics |
| How To · Troubleshooting · Technical Notes |
| Related topics |
| Thunderbird and Lightning How To · AIX Notes · Networking How To's · iPhone How To's |
[edit] How to do a full system backup with rsync and a bare metal restore
Backup can be done with a single command like this:
sudo rsync -vaz --delete --delete-excluded --stats --password-file ~/bin/backup-system.secrets --include-from ~/bin/backup-system.include --exclude-from ~/bin/backup-system.exclude --log-file ~/bin/backup-system.log / destination
destination can be either a mounted disk or a remote system that rsync supports, such as an sftp or an rsync server (rsyncd): rsync://rsyncdealer@backupsserver/systembackup The include file is as follows:
# include all folders but not files to simplify bare metal restore - /var/cache/system-tools-backends /var/cache/**/ /var/log/**/ /var/run/**/
The exclude file is shown here for a typical Debian based linux system. Some folders will be missing on RPM or Suse systems. Also note that /home is excluded from the system backup.
*~ /proc/** /sys/** /dev/** /media/** /tmp/** /home/ /lib/udev/devices/* /var/lib/dpkg/info/* # skip inaccessible folders even by root # strangely only the non-slash terminated string below works properly /var/cache/system-tools-backends # remove all files but not folders to simplify bare metal restore /var/cache/** /var/log/** /var/run/** /var/tmp/** /var/spool/cups/**
To restore simply copy backed up files to a new drive, using a working system. Make sure to preserve file attributes:
rsync -ghop source destination
Make sure your MBR and boot records are ok if you are running on an intel platform.
[edit] How to automatically back up a desktop that is not always on
If you know that you do not connect your system to different networks then your best and the easiest way to back it up is to add an entry to anacron. Ana[chronistic]cron will start jobs that are overdue, unlike cron that just ignores past items. Anacron itself is started by Ubuntu via Upstart either on boot (/etc/init), on wakeup (/etc/apm/event.d/) and daily via cron (/etc/cron.d). That is if anacron is not already up. To use it create symbolic link
sudo ln -s /home/user/bin/1backup-as-user /etc/cron.daily
and
sudo ln -s /home/user/bin/1backup-system-as-user /etc/cron.weekly
Since your anacron scripts are kicked in under the root authority you need to make the scripts assume your authority. For example backup-as-user would read like this:
sudo -u user -H /home/user/bin/backup
[edit] How to automatically back up a laptop when on home network
Create a bash script that can calculate dates and launch backups if a set number of days has passed. Something along these lines:
#!/bin/bash # This script is run when the network is up # it checks for the backup server to be available and then starts appropriate backups based on the date that they last ran # Schedule (every n days) HOMEBACKUP=1 # The datacalc routines date2stamp () { date --utc --date "$1" +%s } stamp2date (){ date --utc --date "1970-01-01 $1 sec" "+%Y-%m-%d %T" } dateDiff (){ case $1 in -s) sec=1; shift;; -m) sec=60; shift;; -h) sec=3600; shift;; -d) sec=86400; shift;; *) sec=86400;; esac dte1=$(date2stamp "$1") dte2=$(date2stamp "$2") diffSec=$((dte2-dte1)) if ((diffSec < 0)); then abs=-1; else abs=1; fi echo $((diffSec/sec*abs)) } launchBackup (){ if [ ! -e ~/bin/$1.timestamp ]; then echo "1979-01-01 01:01:01 PST" > ~/bin/$1.timestamp fi if [ $(dateDiff -d "now" "$(cat ~/bin/$1.timestamp)") -ge $2 ]; then echo "*** Launching the $3 backup" | tee -a ~/bin/backup-launcher.log ~/bin/$1 if [[ $? -eq 0 ]]; then echo $DATE > ~/bin/$1.timestamp else echo "*** Error $? during the $3 backup. Check the logs." | tee -a ~/bin/backup-launcher.log fi fi } echo ---------------------------------- >> ~/bin/backup-launcher.log DATE=`date "+%Y-%m-%d %H:%M:%S %Z"` echo "*** Starting backup launcher for $DATE" | tee -a ~/bin/backup-launcher.log launchBackup backup $HOMEBACKUP "home folder" echo "*** Backup launcher for $DATE successful" | tee -a ~/bin/backup-launcher.log
Then configure the network manager to run this script every time a connection to your home network is made, as described in this section
[edit] How to run scripts when connected to a specific network
Use NetworkManager's dispatch capabilities (works in KDE and Gnome). As root create a script in /etc/NetworkManager/dispatcher.d, say "55BackupLauncher.sh"
#!/bin/sh IF=$1 STATUS=$2 USER=username ROUTERIP="192.168.1.1" ROUTERMAC="00:fa:fa:fa:fa:ff" NETMASK="192.168.1.0/24" if [ "$IF" = "eth0" -o "$IF" = "wlan0" ] && [ "$STATUS" = "up" ]; then if [ -n "`/sbin/ip addr show $IF to $NETMASK`" ]; then if [ -n "`arp -a $ROUTERIP | grep $ROUTERMAC`" ]; then /bin/su alex -c "/home/alex/bin/backup-launcher.sh" exit $? fi fi fi
Make it root runnable
chmod 755 55BackupLauncher.sh
An restart your network manager
sudo service network-manager restart
The sample script above verifies that it is connected to a correct router by using router's hard-coded MAC. You could also check for a host answering on a port:
nc -z -w 3 $HOST $PORT if [[ $? -eq 0 ]]; then ...
Or check a wireless SSID:
SSID=$(/sbin/iwconfig $IFACE | sed -r -n '/SSID/{s/.*SSID:"([^"]+)".*/\1/g;p;q}' )
and
SSID=`iwconfig $IFACE | awk -F\" '{print $2}'`
[edit] How to list active network connections
If you need to know what program has what connection open:
lsof -i
or
lsof -i -P -n
for no network and port resolution
To look at a specific port
lsof -i :80
Same thing, no process information:
netstat --ip
or
netstat -an --ip
[edit] How to list all open tcp-udp connection and corresponding processes
sudo netstat -punta
[edit]
smbclient --list=//server.corp.dom/ --user=xxxx --workgroup=domain smbclient //server.corp.dom/f$ --user=xxxx --workgroup=domain
[edit] How to terminate an open TCP or UDP connection
for end connections use tcpkill from the dsniff package
sudo tcpkill -i lo:0 ip
For pass through connections use
apt-get install cutter
cutter {IP-address} {Port}
Examples: Cut all connections from 192.168.1.5 to server
# cutter 192.168.1.5
Cut all ssh connection from 192.168.1.5 to server
# cutter 192.168.1.5 22
Cut all ssh connection from 192.168.1.5 to ssh server 202.54.1.20
# cutter 202.54.1.20 192.168.1.5 22
[edit] How to enable an serving of X11 to any client
If client connections are rejected, run following on the server (cygwin)
xhost +
You could add this line to c:\cygwin\usr\X11R6\bin\startx.bat Note that this means anyone can connect to your X server, so use with caution.
[edit] How to clear DNS cache
sudo /etc/init.d/dns-clean start
should help. If not, try ncsd:
sudo aptitude install nscd sudo /etc/init.d/nscd restart
Also, if you are running Opera, go to Settings -> delete private data, check "delete entire cache" and click "delete"
|
|||||||||||