4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 Automatic backup of *directories*
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
dariottolo
Junior WebHelper
Junior WebHelper


Joined: 05 Aug 2002
Posts: 4
Location: Germany

PostPosted: Thu Feb 26, 2004 9:47 pm (13 years, 9 months ago) Reply with QuoteBack to Top

Hi everyone,

I have been searching all day long with google, in hotscripts and freshmeat, but could not find anything helpful.

I am on shared hosting, can use ssh and run cronjob.

I need a way to tar.gz one (or more) directory, every x hours.
The compressed file should have a name that reminds creation date (something like directory_2004_02_26.tar.gz), but it's not necessary. It would be good if the new file does not overwrite the old one.

I do not need to send it via mail, or upload it to a different server.
I can do it via ssh, through tar, but I do not know how to run the process "continuosly", via cron.

Any hint will be greatly appreciated.

Thanks a lot for your time.

Dario
OfflineView User's ProfileFind all posts by dariottoloSend Personal MessageVisit Poster's WebsiteYahoo MessengerMSN Messenger
SfCommand
Senior WebHelper
Senior WebHelper


Joined: 10 Nov 2002
Posts: 143
Location: UK

PostPosted: Thu Feb 26, 2004 10:49 pm (13 years, 9 months ago) Reply with QuoteBack to Top

* */X * * * /path/to/tar -cvzf /path/to/storage/space/file.tar.gz /dir/to/tar

if you want to run it every 2 hours, then X == 2, X==3 for every 3 hours etc.

HTH

________________________________
Miguel

http://community.34sp.com
http://www.miguel.me.uk | http://www.sfcommand.co.uk | http://www.ssdg.org.uk
OfflineView User's ProfileFind all posts by SfCommandSend Personal MessageVisit Poster's Website
dariottolo
Junior WebHelper
Junior WebHelper


Joined: 05 Aug 2002
Posts: 4
Location: Germany

PostPosted: Sat Feb 28, 2004 4:49 pm (13 years, 9 months ago) Reply with QuoteBack to Top

SfCommand wrote:
* */X * * * /path/to/tar -cvzf /path/to/storage/space/file.tar.gz /dir/to/tar

if you want to run it every 2 hours, then X == 2, X==3 for every 3 hours etc.

HTH


Thanks a lot for your help. I tired it, and it works, but not exactly as I hoped: the old file.tar.gz is overwritten by the new one.

Is there any way to keep the old file?

What I am afraid of, is that I can not download the old backup, before the new one is created.
OfflineView User's ProfileFind all posts by dariottoloSend Personal MessageVisit Poster's WebsiteYahoo MessengerMSN Messenger
SfCommand
Senior WebHelper
Senior WebHelper


Joined: 10 Nov 2002
Posts: 143
Location: UK

PostPosted: Sat Feb 28, 2004 11:11 pm (13 years, 9 months ago) Reply with QuoteBack to Top

OK, here's the code:
Code:
* */X * * * /path/to/tar -cvzf /path/to/storage/space/filename-`date "+%d-%m-%y"`.tar.gz /dir/to/tar

Those are backticks (located by the 1 key)
That would give a filename of the sort
filename-28-02-04.tar.gz

Reason for edit: I found out how to do it Very Happy
Thanks to those people who helped me get the answer

________________________________
Miguel

http://community.34sp.com
http://www.miguel.me.uk | http://www.sfcommand.co.uk | http://www.ssdg.org.uk
OfflineView User's ProfileFind all posts by SfCommandSend Personal MessageVisit Poster's Website
dariottolo
Junior WebHelper
Junior WebHelper


Joined: 05 Aug 2002
Posts: 4
Location: Germany

PostPosted: Sun Feb 29, 2004 11:47 am (13 years, 9 months ago) Reply with QuoteBack to Top

Hi SfCommand,

sorry for bothering, but it does not work Sad(((

I changed the path of the backup file, and the directory to backup, and set up the crontab via ssh, but nothing happens.

This is my crontab entry:

Code:

* * * * * tar -cvzf /home/diludovi/public_html/php/backup/filename-`date "+%d-%m-%y"`.tar.gz /home/diludovi/public_html/tim


Any idea about what I did wrong?

If I remove the -`date "+%d-%m-%y"` part, the directory tim is correctly compressed, and the .tar.gz file is created in /php/backup directory.
OfflineView User's ProfileFind all posts by dariottoloSend Personal MessageVisit Poster's WebsiteYahoo MessengerMSN Messenger
SfCommand
Senior WebHelper
Senior WebHelper


Joined: 10 Nov 2002
Posts: 143
Location: UK

PostPosted: Sun Feb 29, 2004 12:07 pm (13 years, 9 months ago) Reply with QuoteBack to Top

I'm not entirely sure what the problem is. I've tried touch'ing a file using `date "+%d-%m%y` to append the date, and it creates that file ok.
However, I also tried tar'ing a directory, and although I got a number of errors, it did create the backup.
I wonder if the errors are causing it to stop working?
(Also, your cron is set to run every minute of every hour. I would've thought that was a bit often Very Happy)

Double check that you are leaving a blank line after the cron job, in the crontab.

________________________________
Miguel

http://community.34sp.com
http://www.miguel.me.uk | http://www.sfcommand.co.uk | http://www.ssdg.org.uk
OfflineView User's ProfileFind all posts by SfCommandSend Personal MessageVisit Poster's Website
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Sun Feb 29, 2004 12:20 pm (13 years, 9 months ago) Reply with QuoteBack to Top

I think he's using cPanel so the blank line shouldn't be a problem...

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
dariottolo
Junior WebHelper
Junior WebHelper


Joined: 05 Aug 2002
Posts: 4
Location: Germany

PostPosted: Sun Feb 29, 2004 1:28 pm (13 years, 9 months ago) Reply with QuoteBack to Top

Daniel and SfCommand,

thanks a lot for your replies.

I used the * * * * * on the cron, because I was changing the crontab entry, and did not want to wait... Now it changed it.

I managed to find a script that does exactly the same thing, plus it sends an email, when the process is over. But still, the back-up file is created as "backup2-29-2004.tar.gz". If I run it twice a day, I will lose the old one, that is overwritten by the new one.

Could you please take a look at this script, and tell me if there a way to hack it, in order to have the back-up file look like backup-month-day-year-hour-minute.tar.gz?

Thanks a lot in advance.

Dario

Code:

#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);

###############################################
##
## World Wide Backup Copywright 2001 WORLD WIDE CREATIONS www.worldwidecreations.com
##
## You are free to use this software any way you wish provided you follow the following terms:
## That you cannot use the source in any script/software you redistribute or sell in any way.
## You can NOT redistribute this software in any way.
##
## For complete licensing terms, please contact us at our website at www.worldwidecreations.com
##
## This software was created on October 4th 2001.
##
## This software is intended to create a backup file called backup.tar.gz on your server
## then the software will FTP this software anywhere you wish, providing you have
## access to the said FTP server.  You can also choose NOT to FTP the backup file
## if all you wish is to use this to create a backup file.
##
## You can choose to timestamp the backups.
##
## This software was tested on a Linux platform with a GNU version of Tar
## You may need to modify the code slightly if your system will not use gzip.
##
## You can crontab this script, but you should make sure you do not set the cronjob
## too frequently as this could use up your bandwidth quickly if you set a cronjob to
## execute too often.  You should test this script first without a cronjob and make sure everything is working
## correctly, before you set this up as a working backup.
##
## A weekly crontab would look something like:
##
## 0 0 * * 1 perl /path/to/backup.pl
##
## This would execute the script every Monday.
##
## Dont forget to define your Path to Perl and to CHMOD this 755.
##
## This script is not designed to run as a CGI WEB script.
## you can attempt it if you wish, but with a large backup your browser will likely
## time out.  Use telnet or crontab instead.
##
## Support can be found in our forum at www.worldwidecreations.com
##
###############################################

#Path to the directory you want to be as your base directory to back up.
#All files AND directories off of this path will be archived. NO TRAILING SLASH
$basepath = "/home/diludovi/public_html/tim";

##Where you want the backup.tar.gz file to be placed (not where it will be FTP'd). NO TRAILING SLASH!
$tarpath = "/home/diludovi/public_html/php/backup";

##If you want to timestamp the filename, select this as 1, otherwise 0;
$date_yes = 1;

##If you DONT want the script to FTP this somewhere then check this 1, otherwise leave as 0
$just_back_up = 1;

#If you want the backup file deleted on the SOURCE server after FTP quits, check this as 1;
$delete_source = 0;

##FTP server
$ftpserver = "yourftpserver.com";

##FTP username
$ftpusername = "yourftpusername";

##FTP password
$ftppass = "ftppassword";

##CWD directory, in other words, when the script logs into the FTP server, where would you like the backup.tar.gz file to be placed
$cwdd = "cgi-bin";

##want to be notified of transfer status by email (sendmail required)?
$notify = 1;

#path to sendmail
$sendmail = "/usr/sbin/sendmail";

##email address:
$to = 'backup@diludovico.it';

##Who will the email address be from
$from = 'from@diludovico.it';


##############################NO MORE EDITING REQUIRED ####################################
use Net::FTP;
$hitch = ();
($day, $month, $year) = (localtime)[3,4,5];
$year = $year + 1900; $month++;
if ($date_yes) { $hitch = "$month-$day-$year"; }
print "ARCHIVING FILES\n";
@the = `tar --exclude backup$hitch.tar.gz -czf $tarpath/backup$hitch.tar.gz $basepath`;
@mat = stat("$tarpath/backup$hitch.tar.gz");
if ($mat[7] <= 0) { print "Backup didnt happen.  The error returned by the system (if any) is @the\n"; &send("Backup FAILED. Tar did not backup any files.  Error message returned from server (if any) is @the", "Backup FAILED"); exit; }
print "Files Archived to backup$hitch.tar.gz\nFile Size Is $mat[7] Bytes\n";
if ($just_back_up) {
&send("Backup success.  Backup size $mat[7] bytes", "Backup Success");
print "Ok, files are backed up, No FTP requested. Shutting down\n";
exit;
}
print "Connecting into server\n";
if ($ftp = Net::FTP->new("$ftpserver", Debug => 0)) { print "Logging in to server\n"; } else { print "could not find server! Exiting\n"; &send("Backup FAILED.  Could NOT connect to $ftpserver", "Backup FAILED"); exit; }
if ($ftp->login("$ftpusername","$ftppass")) { print "Username and Password ACCEPTED\n"; } else { print "could not log into server with Username and Password. Exiting!\n"; &send("Backup FAILED.  Could NOT connect to $ftpserver with the Username And Password provided - Authentication error.", "Backup FAILED"); exit; }
if ($ftp->cwd("$cwdd")) { print "Changed to the directory without any trouble - UPLOADING\n"; } else { print "Could not change to the appropriate directory, uploading anyway\n"; }
if ($ftp->binary) { print "Changed to Binary Mode No Problem\n"; } else { print "Could not change to binmode $!\n"; exit; }
if ($ftp->put("$tarpath/backup$hitch.tar.gz")) { print "Everything appears ok with the upload\n"; } else { print "Can not upload file $!\n EXITING\n"; &send("Backup FAILED.  Could NOT upload to $ftpserver. Error message returned from server is: $1", "Backup FAILED"); exit; }
$ftp->quit;
if ($delete_source) {
print "You chose to delete the source after FTP, deleting $tarpath/backup$hitch.tar.gz...";
unlink("$tarpath/backup$hitch.tar.gz") or print "COULD NOT DELETE! $!";
print "\nDeleted!\n";
}
print "File Transferred OK!\nFinished\n";
if ($notify) {
&send("Backup success.  Transferred $mat[7] bytes to $ftpserver", "Backup Success");
}

sub send {
if ($notify) {
open MAIL, "|$sendmail -t" or print "Could not open Sendmail $!\n";
                print MAIL "To: $to\nFrom: $from\nSubject: $_[1]\n\n$_[0]\n\n.\n";
                close MAIL;
}
}
OfflineView User's ProfileFind all posts by dariottoloSend Personal MessageVisit Poster's WebsiteYahoo MessengerMSN Messenger
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   




You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot edit your posts in this forum.
You cannot delete your posts in this forum.
You cannot vote in polls in this forum.


Page generation time: 0.080339 seconds :: 18 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme