[HOW TO] Backup to a remote samba path

Post Reply
andreus
Posts: 134
Joined: Wed Dec 16, 2009 9:57 pm

[HOW TO] Backup to a remote samba path

Post by andreus » Fri Jan 06, 2012 8:25 pm

1. Create a share named "backup" in the dashboard

2. Automount remote samba backup share
2.1. Create a file "/etc/initng/samba-backup.i" with the following code, replacing "YOUR.REMOTESAMBA.PATH, "YOUR.REMOTESAMBA.USERNAME", "YOUR.REMOTESAMBA.PASSWORD" and "YOUR.REMOTESAMBA.IP" with your remote samba backup destination samba configurations (ex: //192.168.1.150/sambabackup; admin; testpassword; 192.168.1.150)
Note: First, make sure you can ping the remote machine and is accessible with your selected credencials

Code: Select all

#!/sbin/itype
# This is a i file, used by initng parsed by install_service
# Automount script for samba backup v1.00
# Author: andreus

service samba-backup {
    need = samba/initial;

    #Just change the environment path variables if needed
    env LOCAL_SHARE_BACKUP = /shares/backup;
    env REMOTE_SHARE_BACKUP = YOUR.REMOTESAMBA.PATH;
    env LOGFILE_PATH = /var/log/samba-backup.log;

    script start = {
	echo "$(date) ==== Start automount (samba-backup)====" >> ${LOGFILE_PATH}

	#check if disks are mounted (up to 120 seconds wait)
	echo "$(date) -> Wait for Disks (/media/internal_1/shares)" >> ${LOGFILE_PATH}
	COUNTER=0
	FAIL=1
	while [  $COUNTER -lt 24 ]; do
		if [ ! -d /media/internal_1/shares ]; then
			echo "$(date) Disks (/media/internal_1/shares) not yet ready... waiting more 5 seconds" >> ${LOGFILE_PATH}
			let COUNTER=COUNTER+1 
			sleep 5
		else
			echo "$(date) Disks (/media/internal_1/shares) ready!" >> ${LOGFILE_PATH}
			FAIL=0
			let COUNTER=24
		fi
	done
	if [ $FAIL != 0 ]; then
		echo "$(date) Disks (/media/internal_1/shares) ready FAILED!" >> ${LOGFILE_PATH}
	fi

	#check if shares are mounted (up to 120 seconds wait)
	echo "$(date) -> Wait for Shares (/shares)" >> ${LOGFILE_PATH}
	let COUNTER=0
	let FAIL=1
	while [  $COUNTER -lt 24 ]; do
		if [ ! -d ${LOCAL_SHARE_BACKUP} ]; then
			echo "$(date) Shares (/shares) not yet ready... waiting more 5 seconds" >> ${LOGFILE_PATH}
			let COUNTER=COUNTER+1 
			sleep 5
		else
			echo "$(date) Shares (/shares) ready!" >> ${LOGFILE_PATH}
			FAIL=0
			let COUNTER=24
		fi
	done
	if [ $FAIL != 0 ]; then
		echo "$(date) Shares (/shares) ready FAILED!" >> ${LOGFILE_PATH}
	fi

	#auto remote backup share mount
	if [ -d ${LOCAL_SHARE_BACKUP} ]; then
		echo "$(date) -> Bind remote backup share" >> ${LOGFILE_PATH}
		if [ -d $SHARE_BACKUP ]; then
			mount -t cifs ${REMOTE_SHARE_BACKUP} ${LOCAL_SHARE_BACKUP} -o user=YOUR.REMOTESAMBA.USERNAME,password=YOUR.REMOTESAMBA.PASSWORD,ip=YOUR.REMOTESAMBA.IP,nocase,noperm,file_mode=0777,dir_mode=0777
			echo "$(date) ${REMOTE_SHARE_BACKUP} binded with ${LOCAL_SHARE_BACKUP} = $? (0=ok)" >> ${LOGFILE_PATH}
		fi
	fi

        exit 0
    };

script stop = {
	echo "$(date) ==== Stop automount (samba-backup)====" >> ${LOGFILE_PATH}
        if [ -d ${LOCAL_SHARE_BACKUP} ]; then
		echo "$(date) -> Unbind remote backup share" >> ${LOGFILE_PATH}
		umount ${LOCAL_SHARE_BACKUP}
		echo "$(date) ${LOCAL_SHARE_BACKUP} unbinded = $? (0=ok)" >> ${LOGFILE_PATH}
        fi
        wait
        if [ -d ${LOCAL_SHARE_BACKUP} ]; then
		echo "$(date) -> Force unbind remote backup share" >> ${LOGFILE_PATH}
		umount -l ${LOCAL_SHARE_BACKUP}
		echo "$(date) ${LOCAL_SHARE_BACKUP} force unbinded = $? (0=ok)" >> ${LOGFILE_PATH}
        fi

	exit 0
    };
}
2.2 Test the "samba-backup.i" script using ngc commands

Code: Select all

ngc --start samba-backup
ls -lisa /shares/backup
ngc --stop samba-backup
2.3. Add the automount ("samba-backup") to "/etc/initng/samba.i" by adding the line "also_start = samba-backup;" as show in my "/etc/initng/samba.i" file

Code: Select all

#!/sbin/itype
# This is a i file, used by initng parsed by install_service
service samba/initial {
	need = virtual/net;
	script start = {
		echo "check presence of some samba directories"
		SAMBA_LOG="/var/log/samba"
		SAMBA_PID="/var/run/samba"
		SAMBA_LOCK="/var/lock/samba"
		SAMBA_CACHE="/var/cache/samba"
		
		[ ! -d  "$SAMBA_LOG" ] &&  /bin/mkdir -p "$SAMBA_LOG"
		[ ! -d  "$SAMBA_PID" ] &&  /bin/mkdir -p "$SAMBA_PID"
		[ ! -d  "$SAMBA_LOCK" ] &&  /bin/mkdir -p "$SAMBA_LOCK"
		[ ! -d  "$SAMBA_CACHE" ] &&  /bin/mkdir -p "$SAMBA_CACHE"
		
		exit 0
	};

}
daemon samba/smbd {
	need = samba/initial;
	also_start = samba-backup;
	respawn;
	exec daemon = /usr/sbin/smbd -F;
}

daemon samba/nmbd {
	need = samba/initial;
	respawn;
	exec daemon = /usr/sbin/nmbd -F;
}

daemon samba/winbind {
	need = samba/initial;
	respawn;
	exec daemon = /usr/sbin/winbindd -F;
}

virtual samba {
	need = samba/smbd samba/nmbd samba/winbind;
	also_stop = samba/smbd samba/nmbd samba/winbind;
}
2.4 Reload the NAS from the dashboard

2.5. You should now have an automounted remote samba share!

3. Create a rsync script to backup your shares
3.1. Create an rsync script "/root/samba-backup-rsync.sh" with this code, replacing "YOUR.SHARE1", "YOUR.SHARE2", "YOUR.SHAREN", etc with the name of the shares you want to backup

Code: Select all

#!/bin/bash

#Global variables
NAME="Rsync Backup System"
LOGFILE="/var/log/rsync/$(date +%Y%m%d)_samba-backup.log"

#Shares you want to backup
SHARES_TOBACKUP=( "YOUR.SHARE1" "YOUR.SHARE2" "YOUR.SHAREN" )
SHARE_DESTINATION="backup"

if [ ! -d /var/log/rsync/ ] ; then
	mkdir /var/log/rsync/
fi
clear
echo "[$(date)] -----------------------------------------------------------------------------------------" 2>&1 | tee -a $LOGFILE
echo "[$(date)]                                $NAME                                                     " 2>&1 | tee -a $LOGFILE
echo "[$(date)] -----------------------------------------------------------------------------------------" 2>&1 | tee -a $LOGFILE

for i in "${SHARES_TOBACKUP[@]}"
do
	echo "" 2>&1 | tee -a $LOGFILE
	echo "[$(date)] START: Backing up $i..." 2>&1 | tee -a $LOGFILE
	rsync -rlptDv --modify-window=1  /shares/"$i"/ /shares/"$SHARE_DESTINATION"/"$i"/ 2>&1 | tee -a $LOGFILE
	echo "[$(date)] END: Backing up $i..." 2>&1 | tee -a $LOGFILE
	echo "[$(date)]-----------------------------------------------------------------------------------------" 2>&1 | tee -a $LOGFILE
done
3.2. Give the script "/root/samba-backup-rsync.sh" execute permissions

Code: Select all

chmod 0744 /root/samba-backup-rsync.sh
3.3. Test the script. If everything went ok, it will start making the first backup of your selected shares!

Code: Select all

/root/samba-backup-rsync.sh
4. Add a job to cron for backup everyday at 03:00am
4.1. Create/Edit crontab

Code: Select all

crontab -e
And add the following

Code: Select all

0 3 * * * /root/samba-backup-rsync.sh
Notes:
- If you have problems with editor just run the following commands

Code: Select all

EDITOR=`/usr/bin/which vi`
export EDITOR=`/usr/bin/which vi`
- If you don't know how to work in vi editor see http://www.washington.edu/computing/unix/vi.html
- If you want to do another backup shedule see http://www.cyberciti.biz/faq/how-do-i-a ... unix-oses/

4.2. View crontab

Code: Select all

crontab -l
5. Everything is done! Confirm everything is ok by viewing the logs in "/var/log/rsync/"

markarthur
Posts: 5
Joined: Wed Jan 25, 2012 4:28 pm

Re: [HOW TO] Backup to a remote samba path

Post by markarthur » Wed Jan 25, 2012 5:04 pm

There are many application to backup the Ubuntu through a remote samba path. Each one has its weakness and strengths. Sometimes it is necessary to backup so many computer quickly so for that enterprise environments edition are there. Other are made for home environments which comes in simple GUI design. Steel it is needed to be accessed with the command line.

Post Reply