Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've been trying to set up a scheduled backup for my server but i can't seem to get it working. I need to copy the contents of /var/www into a compressed file (tar?) in a folder in my home directory every 24 hours. I also want to perform a dump of all mysql databases every 24 hours and store it in another folder in my home directory. I want both backups to be named with the current date.

The server is running Ubuntu 12.04.1, i only have access through SSH so no graphical interfaces.

Is this possible to do? I've been looking into the TAR and rsync utility, but i'm not sure how to do it. If anyone can help me out if would be very appreciated.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Write a script that does it and set cron to run it daily by placing it on /etc/cron.daily. Read enter link description here

Your script will need to run run rsync (it has a --archive option) and use the date to generate filename. For the MySQL database you would need to create a dump before. You can write the language in whatever language you like. In Perl it would be something like:

my ($day, $month, $year) = (localtime)[3,4,5];
my $time = sprintf ("%04d-%02d-%02d", $year+1900, $month+1, $day);    

my @backup_commands = (
  "/usr/bin/rsync --archive --copy-links /var/www /somewhre/$time",
  "/usr/bin/rsync --archive --copy-links /mysqldunmp /somewhere/$time",
);

foreach (@backup_commands) {
  system($_);
  if ($? == -1){
    ## add you favaorite method of reporting here
  }
}

You'll need to adjust the path names, set up the part of creating the MySQL dump and where should problems be logged and the like. But it gives you a structure already.

share|improve this answer
    
Thank you! I'm a bit of a newbie, but i'll try to figure this out. What does the archive option do? I tried running one of the commands and it copied all the files to the specified location, but it didn't compress it or anything as far as i could see. –  qwerty Sep 2 '12 at 15:01
    
I got it working, now i just have to fix the mysql dump. One question though, could you possibly add the hour:minute to the $time variable? That would be very appreciated. And also, is it possible to only copy the contents of /var/www? As of now, it copies the entire www folder, i would like to only copy what's in it if that is possible. –  qwerty Sep 2 '12 at 15:20
    
check localtime on perldoc perldoc.perl.org/functions/localtime.html About copying the files, not the directory, simply add a slash to the end of the path. Read the man page of rsync (it's on the Usage section). –  carandraug Sep 2 '12 at 15:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.