Samba

Samba

Back in the day (when Shadow Copies, GitHub, SharePoint and Google Docs were unbeknown to me!), I relied on Linux file servers with Samba for basic Windows LAN company data shares.

Some Company files were more important than others, so having some working copies of those files it was of utter importance (you already know the drill: it happens!).

Given my file server was running CentOS, I used to rely on a very basic bash script that allowed me to automatically create a new working copy of a particular file every day.

This bash script loosely checked if my specific Excel file was open or not:

  • If the file was closed, it would've just created a new compressed tar archive copy of the file onto a sub-directory named BACKUP.
  • If the file was open, it would've notified me via email, prompting me to manually save & close then manually rerun the script.

NOTE: NOT closing the file via script was done on purpose, because this file could've been left open on someone else's computer and that person (most likely me, I admit it!) didn't save the changes for some arcane reason (multiple file copies open at the same time, etc.).

So, ultimately, a very simple semi-automated task for a local LAN scenario.

Here's the gist of it (Note - some Italian language comment follows):

#!/bin/sh

# some-mm-dd - [email protected]

# Controlla se /mnt/share/company/data/

# "filename.xls" è aperto e, se è chiuso,

# effettua una copia di backup, comprimendo e nominando il file

# in base alla data odierna nel formato americano.

#

#set -x  # turn on debugging mode

FILE=/mnt/share/company/data/filename.xls

DST=/mnt/share/company/data/BACKUP

SUBJ="Copia giornaliera di filename.xls fallita."

DATE=$(date +"%Y-%m-%d")

[email protected]

MSG=null

lsof "$FILE" | grep -q COMMAND &>/dev/null

if [ $? -ne 0 ]

then

tar cvjf $DST/filename-bck-$DATE.tbz2 "$FILE"

echo "$FILE backup copy completed successfully."

else

echo -e "\n" > $MSG

echo -e "$SUBJ\n" >> $MSG

echo -e "$DATE - Warning: file $FILE is Open.\n" >> $MSG

echo "Close it, then relaunch the script." >> $MSG

mail -s "$SUBJ" $ADMMAIL < $MSG

fi

- This was scheduled to run daily from cron at 11.00pm

Improve it!
- Check if the backup copy is good (ie. not corrupted).

SRC:
http://nixcraft.com/shell-scripting/15231-bash-shell-check-open-file.html

Senior Professional Network and Computer Systems Engineer during work hours and father when home.

Andrea strives to deliver outstanding customer service and heaps of love towards his family.

In this Ad-sponsored space, Andrea shares his quest for "ultimate" IT knowledge, meticulously brought to you in an easy to read format.

How to make a daily backup copy of a SMB-shared file.
4 (80%) 1 vote

Ask me anything