Computerglitch

An ongoing adventure

Directory Rsync Script

This is a script I wrote to search mounted windows machines from a linux server for a specific directory. Once the directory was located I wanted the directory found to be rsynced to the local servers /backup partition.


The following script is named script-build and is placed in / The script is commented to understand what’s going on.

#!/bin/sh
#Find the directory on the windows machine to be backed up.
find /mnt -name Qualcomm -print > /tmp/mail-bkup.tmp

#Edit the mail-bkup.tmp file to add proper spacing so linux can recognize the spacing.
sed -r -e 's# #\\ #g' /tmp/mail-bkup.tmp > /tmp/mkdir.tmp

#Edit the mail-bkup.tmp file to add proper spacing so linux can recognize the spacing and create a separate file.
sed -r -e 's# #\\ #g' /tmp/mail-bkup.tmp > /tmp/mail-bkup.tmp1

#Append /backup to the join.tmp file.
sed 's/ˆ/\/backup/' /tmp/mkdir.tmp > /tmp/join.tmp

#Append mkdir -p /backup to the mkdir.tmp1 file.
sed 's/ˆ/mkdir -p \/backup/' /tmp/mkdir.tmp > /tmp/mkdir.tmp1

#Add #!/bin/sh to the head of the mkdir.main script.
sed '1s/ˆ/#!\/bin\/sh\n/' /tmp/mkdir.tmp1 > /tmp/mkdir.main

#Append rsync -a to mail-bkup.tmp2
sed 's/ˆ/rsync -a /' /tmp/mail-bkup.tmp1 > /tmp/mail-bkup.tmp2

#Put mail-bkup.tmp2 and join.tmp side by side and output the result to mail-bkup.tmp4
paste /tmp/mail-bkup.tmp2 /tmp/join.tmp > /tmp/mail-bkup.tmp4

#Add #!/bin/sh to the head of mail-bkup.main
sed '1s/ˆ/#!\/bin\/sh\n/' /tmp/mail-bkup.tmp4 > /tmp/mail-bkup.main

#Set both scripts executable
chmod +x /tmp/mkdir.main
chmod +x /tmp/mail-bkup.main

#cleanup our mess
rm /tmp/join.tmp
rm /tmp/mail-bkup.tmp
rm /tmp/mail-bkup.tmp1
rm /tmp/mail-bkup.tmp2
rm /tmp/mail-bkup.tmp4
rm /tmp/mkdir.tmp
rm /tmp/mkdir.tmp1

This script will create two more scripts (/tmp/mail-bkup.main and /tmp/mkdir.main)


This is how I have crontab setup to execute the scripts:

#CREATE THE SCRIPTS TO BACKUP CLIENT EMAILS AT 10PM
0 22 * * * /script-build

#EXECUTE THE SCRIPTS TO CREATE DIRECTORY STRUCTURE AND SYNC EMAIL AT 1AM & 2AM RESPECTIVELY
0 1 * * * /tmp/mkdir.main
0 2 * * * /tmp/mail-bkup.main

Comments