LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-02-2012, 06:41 PM   #1
exomic
LQ Newbie
 
Registered: Feb 2012
Location: Canada, Quebec
Distribution: CentOS
Posts: 3

Rep: Reputation: Disabled
Talking Backup home to External HD


Hi, I have a external HD and I want to cron a job that run a script to backup my home dirs to my backup drive.

My cron job (I want to run it each week on Monday at 2am is that correct?):
Code:
0 2 * * 1 /media/ExternalHD/Backup/backup.sh
My backup.sh:

Code:
cp -R /home/sylvie /media/ExternalHD/Backup/Sylvie
cp -R /home/exomic /media/ExternalHD/Backup/Michael
cp -R /home/frederic /media/ExternalHD/Backup/Frederic
cp -R /home/Quicken /media/ExternalHD/Backup/Martin
cp -R /home/MesImages /media/ExternalHD/Backup/Photos
What I would like is a script that compress the user's folder, copy it to my ExternalHD and keep a history of like 3 backup file (like Backup2012-02-02_Photos.zip) and delete old backup older then like a month.

Thanks!

Last edited by exomic; 02-02-2012 at 06:42 PM.
 
Old 02-03-2012, 11:25 AM   #2
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 576

Rep: Reputation: 48
A Script to Backup Users' Home Directories:

Code:
for USER in `ls -1 /home | grep -vF 'lost+found'`; do 
  tar -vzcf /tmp/${USER}.tar.gz /home/${USER}
done
Note: Change the paths per your file-system/paths.

A Sample Run of the Script:

Code:
Verify the result/output:

[demo@localhost ~]$ ls -ltrh /tmp/*.gz
-rw-rw-r--. 1 demo demo  21M Feb  3 21:29 /tmp/demo.tar.gz
-rw-rw-r--. 1 demo demo 1.5K Feb  3 21:29 /tmp/lsdev.tar.gz
-rw-rw-r--. 1 demo demo   45 Feb  3 21:29 /tmp/test.tar.gz

[demo@localhost ~]$ file /tmp/demo.tar.gz 
/tmp/demo.tar.gz: gzip compressed data, from Unix, last modified: Fri Feb  3 21:28:52 2012
[demo@localhost ~]$
You can have another script to delete old backed up files.

Code:
[demo@localhost ~]$ find /tmp -name "*.tar.gz" -mtime -1 -print0 2>/dev/null | xargs -0 file 2>/dev/null
/tmp/demo.tar.gz:  gzip compressed data, from Unix, last modified: Fri Feb  3 21:28:52 2012
/tmp/lsdev.tar.gz: gzip compressed data, from Unix, last modified: Fri Feb  3 21:29:01 2012
/tmp/test.tar.gz:  gzip compressed data, from Unix, last modified: Fri Feb  3 21:29:01 2012
Note 1: -mtime -1 = Files modified within 24 hours / 1 day. Change it to +7 or +30 to mean files last modified +7 or +30 days old.

More Details: man find

Note 2: the "file" command is used to describe briefly about the file type.

Now I am going to use "rm" command to remove them instead:

Code:
[demo@localhost ~]$ find /tmp -name "*.tar.gz" -mtime -1 -print0 2>/dev/null | xargs -0 rm 2>/dev/null
Verify:

Code:
[demo@localhost ~]$ find /tmp -name "*.tar.gz" -mtime -1 -print0 2>/dev/null | xargs -0 file 2>/dev/null
[demo@localhost ~]$
Files deleted!

Well, I think I have worked enough for you to help you. Now consider giving me $100.

Cheers!
 
1 members found this post helpful.
Old 02-03-2012, 01:10 PM   #3
exomic
LQ Newbie
 
Registered: Feb 2012
Location: Canada, Quebec
Distribution: CentOS
Posts: 3

Original Poster
Rep: Reputation: Disabled
Wow thanks! I will check that tonight but looks perfect!

PS: what do you think of Bentleys? I have 2 in my fourth house in italia that need to be changed (2010 models). I may give one to you

*** Just kidding dont spam me via pm :P***
 
Old 02-04-2012, 05:23 AM   #4
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 576

Rep: Reputation: 48
Welcome! I was also kidding, man. I do those stuffs every days. Just for your curiosity and also because you are working on Linux, let me tell you something here. "2>/dev/null" means "redirect all errors or error messages to the black-hole named /dev/null which simply stores them nowhere."

Your cron-job is okay. In case it doesn't work as expected (depending upon some Unix/Linux flavours) then change it to:

Code:
0 2 * * 1 /bin/bash /media/ExternalHD/Backup/backup.sh
Usually a cron-job's output is emailed to the respective user. You can suppress it by using the redirecting it to the black-hole explained above.

Code:
0 2 * * 1 /bin/bash /media/ExternalHD/Backup/backup.sh &>/dev/null
& = Means "Normal Output and Error Messages as well".

Cheers!

Last edited by devUnix; 02-04-2012 at 05:26 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How do I mount External Hard Drive and what command to backup Home Folder to it ? mansour Linux - Newbie 91 10-09-2011 05:14 PM
How to Backup /home budbaker44 Linux - Newbie 3 10-07-2009 02:25 AM
Newbie - How do I backup my /Home folder to an external device kincanonmi Linux - Newbie 1 01-03-2009 11:10 PM
External USB backup plnelson Linux - Newbie 2 10-12-2004 10:57 AM
External Backup Server FreeFox Linux - General 4 08-22-2003 01:29 PM


All times are GMT -5. The time now is 01:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration