DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 15th January 2010
bgobs bgobs is offline
Port Guard
 
Join Date: Jun 2008
Location: Sofia, BG
Posts: 17
Default Request for sh script for compressing files

I need bash script to backup my sites. Something like this:

(I dont know very well english so I'll try to explain myself with PHP )

Code:
$date = date("m-Y-");
$sitesPath = "/some/path/to/web/sites/";
if (is_dir($sitesPath)) {
	$handle = opendir($sitesPath);
	if (!empty($handle)) {
		while (false !== ($file = readdir($handle))) {
			if (is_dir($sitesPath.$file)) { $dir_array[] = $file; }
		}
		
	}
	closedir($handle);
}


if (is_array($dir_array)) {
	foreach ($dir_array as $dir) {
		echo "Archiving: ".$sitesPath."/".$dir."<br />";
		exec("tar -zcvf ".$date."-".$dir.".tar.gz ".$sitesPath."/".$dir);
	}
}
Can someone write bash script for me and for free? just for my deep dark brown eyes ?

And of course sorry for my english

Thanks for reading !
__________________
FreeBSD 6.2 RELEASE
Reply With Quote
  #2   (View Single Post)  
Old 15th January 2010
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,125
Default

I once made the mistake to look to deeply in the brown eyes of my then future wife.

Anyway, something like this?
Code:
#!/bin/sh

SITES='
/home/www/site-1
/home/www/site-2
/home/www/site-3
'


for THIS in ${SITES} ; do
    DATE=$(date "+%Y-%m-%d_%H-%M")
    SITENAME=$(basename ${THIS})
    echo tar cvzf ${DATE}_${SITENAME}.tgz -C ${THIS}
done
Run with debugging so you can see the expansion of the variables
Code:
$ sh -vx site_backup 
#!/bin/sh

SITES='
/home/www/site-1
/home/www/site-2
/home/www/site-3
'
+ SITES=
/home/www/site-1
/home/www/site-2
/home/www/site-3



for THIS in ${SITES} ; do
    DATE=$(date "+%Y-%m-%d_%H-%M")
    SITENAME=$(basename ${THIS})
    echo tar cvzf ${DATE}_${SITENAME}.tgz -C ${THIS}
done
+ date +%Y-%m-%d_%H-%M
+ DATE=2010-01-16_00-13
+ basename /home/www/site-1
+ SITENAME=site-1
+ echo tar cvzf 2010-01-16_00-13_site-1.tgz -C /home/www/site-1
tar cvzf 2010-01-16_00-13_site-1.tgz -C /home/www/site-1
+ date +%Y-%m-%d_%H-%M
+ DATE=2010-01-16_00-13
+ basename /home/www/site-2
+ SITENAME=site-2
+ echo tar cvzf 2010-01-16_00-13_site-2.tgz -C /home/www/site-2
tar cvzf 2010-01-16_00-13_site-2.tgz -C /home/www/site-2
+ date +%Y-%m-%d_%H-%M
+ DATE=2010-01-16_00-13
+ basename /home/www/site-3
+ SITENAME=site-3
+ echo tar cvzf 2010-01-16_00-13_site-3.tgz -C /home/www/site-3
tar cvzf 2010-01-16_00-13_site-3.tgz -C /home/www/site-3
For operation in real, you need to delete the echo in front of the tar command.
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #3   (View Single Post)  
Old 16th January 2010
bgobs bgobs is offline
Port Guard
 
Join Date: Jun 2008
Location: Sofia, BG
Posts: 17
Default

Thanks J65nko!
__________________
FreeBSD 6.2 RELEASE
Reply With Quote
  #4   (View Single Post)  
Old 16th January 2010
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,125
Default

On reviewing it, I see I forgot to specify the directory to backup.

Solution 1: Omit the "-C", that only changes the directory
Disadvantage: the path will be in the backup

Solution 2: Do something like this
Code:
tar cvzf 2010-01-16_00-13_site-2.tgz -C /home/www site-1
You can use dirname(1) to extract the path for "-C"
Code:
$ dirname /home/www/site-1
/home/www
$ basename home/www/site-1
site-1
I probably couldn't concentrate because of the memories of the brown eyes

Leaving out the "-C" option
Code:
# tar cvzf robert /home/robert    
tar: Removing leading / from absolute path names in the archive
home/robert
home/robert/.cshrc
home/robert/.login
home/robert/.mailrc
home/robert/.profile
home/robert/Maildir
home/robert/Maildir/tmp
home/robert/Maildir/new
home/robert/Maildir/cur
home/robert/.exrc
Here the home dir is stored.

Code:
tar cvzf robert -C /home robert                                                 
robert
robert/.cshrc
robert/.login
robert/.mailrc
robert/.profile
robert/Maildir
robert/Maildir/tmp
robert/Maildir/new
robert/Maildir/cur
robert/.exrc
Here only the "robert' directory name.
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #5   (View Single Post)  
Old 16th January 2010
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Default

If you change

Code:
SITES='
/home/www/site-1
/home/www/site-2
/home/www/site-3
'
to:

Code:
SITES=${SITES:-'
/home/www/site-1
/home/www/site-2
/home/www/site-3
'}
You can also override the default list of sites by exporting the SITES environment variable before you run the script. E.g. $ SITES="/a /b /c"; export SITES; run-script-here. Other variations of possible, if you read the shells manual page about expansions.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #6   (View Single Post)  
Old 16th January 2010
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,125
Default

You even don't have to export the variables, you could use env(1):

Code:
# env SITES='/usr/local/www/aaa /usr/local/www/bbb'  site-backup
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
matts: a shell script to mail attachments from the command line J65nko Guides 7 3rd August 2022 03:13 AM
Request for guides milo974 General software and network 1 16th December 2009 06:42 PM
mod_gzip2 not compressing pages Weaseal FreeBSD Ports and Packages 0 23rd September 2008 11:56 PM
Request for Opinions: A secure way of sharing modules TerryP Off-Topic 2 10th August 2008 07:18 PM
Create a script to rmove oldest files disco Programming 5 14th July 2008 09:25 PM


All times are GMT. The time now is 05:57 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick