LinuxQuestions.org
Visit Jeremy's Blog.
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 09-15-2011, 08:00 AM   #1
saran_redhat
Member
 
Registered: May 2009
Location: chennai
Posts: 247

Rep: Reputation: 16
script need


Hi,

Can anyone tel me how to make folder to create tar.gz script with date
For example : I have one foder in my server path /home/data/. In side this folder I have lots of folder. Now I want to create each folder in side /home/data to make tar.gz format and stored some other path like /root/data with date using linux script.
For backup purpose . my English very bad please.
Thanks
 
Old 09-15-2011, 09:20 AM   #2
resolv_25
Member
 
Registered: Aug 2011
Location: Croatia
Distribution: Debian 7/Ubuntu 14.04
Posts: 57

Rep: Reputation: 9
Here is an example (if I understand you properly).
1. Open text editor and create script, for example, save as:mybackup.sh (inside your /home/data/)
2. Put this inside

Code:
#!/bin/sh

# Make data and directory in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"
# create dir wtih a today's date:
mkdir /home/data/$NOW

# create archive of /root/data as backup_date.tar.gz within a newly created directory of today
tar cvzf /home/data/$NOW/backup_$NOW.tar.gz /root/data
3. If this has to backup root directory, make root owner of the script, within a terminal write as root:
Code:
$ chown -R root:root mybackup.sh
4. Make it executable:
Code:
$ chmod +x mybackup.sh
5. Run it manually (as root):
Code:
$ ./mybackup.sh
Or put it in cron (as root) to be executed regularly, let's say every day, open editor as root:
Code:
$ crontab -e
Let's say every day in 23 hour 10 minutes, paste into opened editor for cron:

Code:
10 23 * * * /home/data/mybackup.sh
(explanation of this line:
10 (minutes) 23 (hours) * (every day) * (every week) * (every month)
it may be:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command

Last edited by resolv_25; 09-15-2011 at 09:29 AM.
 
Old 09-15-2011, 09:52 AM   #3
saran_redhat
Member
 
Registered: May 2009
Location: chennai
Posts: 247

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by resolv_25 View Post
Here is an example (if I understand you properly).
1. Open text editor and create script, for example, save as:mybackup.sh (inside your /home/data/)
2. Put this inside

Code:
#!/bin/sh

# Make data and directory in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"
# create dir wtih a today's date:
mkdir /home/data/$NOW

# create archive of /root/data as backup_date.tar.gz within a newly created directory of today
tar cvzf /home/data/$NOW/backup_$NOW.tar.gz /root/data
3. If this has to backup root directory, make root owner of the script, within a terminal write as root:
Code:
$ chown -R root:root mybackup.sh
4. Make it executable:
Code:
$ chmod +x mybackup.sh
5. Run it manually (as root):
Code:
$ ./mybackup.sh
Or put it in cron (as root) to be executed regularly, let's say every day, open editor as root:
Code:
$ crontab -e
Let's say every day in 23 hour 10 minutes, paste into opened editor for cron:

Code:
10 23 * * * /home/data/mybackup.sh
(explanation of this line:
10 (minutes) 23 (hours) * (every day) * (every week) * (every month)
it may be:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command

Hi

Thanks for the reply.

My need is lot of websites directory contains on the directory like /home/sites/. so inside this path lot of websites directorys stored. now I want to make that each directory backup with samename to any other path.like backup folder makes with tar.gz format. this is i want. i think u understand. Thanks once again
 
Old 09-15-2011, 10:20 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,476

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
If I understand what you're trying to -- in /home/data you have sub-directories, say
Code:
/home/data
/home/data/dir01
/home/data/dir02
...
/home/data/dir99
You want to make individual archives of dir01, dir02, ..., dir99 so that you can extract them in sub-directories of /root/data, so you would have
Code:
/root/data
/root/data/yyyy-mm-dd-dir01.tar.gz
/root/data/yyyy-mm-dd-dir02.tar.gz
...
/root/data/yyyy-mm-dd-dir99.tar.gz
You could do this manually (where yyyy-mm-dd is the year, month and day; e.g., 2011-09-15)
Code:
cd /home/data
tar cf yyyy-mm-dd.tar *
<wait a while>
gzip yyyy-mm-dd.tar                        < compress the archive with gzip >
That archive would contain the entire tree (all the directories and files found in /home/data); it would not have the path name /home/data, only dir01, dir02, ..., dir99, so it could be extracted in another directory, /root/data in this case.

That would be the simplest way; you will have one archive file containing every sub-directory. Keep in mind that you can extract only what you want from a tar archive; i.e., you could extract dir01 from a complete archive as above without extracting anything else.

A little more complicated way is to archive the individual directories semi-manually
Code:
#!/bin/sh
#     we need a date stamp of the form yyyy-mm-dd
DATESTAMP=`date +%F`
#     get into the parent directory
cd /home/data
#     for each directory on this list...
for DIRNAME in dir01 dir02 dir03
do
     #     create the tar archive
     tar cf ${DATESTAMP}-${DIRNAME}.tar
     #     compress it
     gzip ${DATESTAMP}-${DIRNAME}.tar
     #     move it to the backup directory
     mv ${DATESTAMP}-${DIRNAME}.tar.gz /root/data
done
Now let's get a little more sophisticated (not too sophisticated, just a little).

We'd really like to do this without having to manually list the individual directories, so let's use the ls utility to list them and a little shell magic to get only what we need
Code:
#!/bin/sh
#       we need a time stamp for the archive(s) yyyy-mm-dd
TIMESTAMP=`date +%F`
cd /home/data
#       we'll use the ls utility to get a list (including files)
for DIRNAME in `ls`
do
        #       if it's a directory...
        if [ -d ${DIRNAME} ]
        then
                #       create the tar archive
                tar cf ${TIMESTAMP}-{${DIRNAME}.tar ${DIRNAME}
                #       compress the archive with gzip
                gzip ${TIMESTAMP}-{${DIRNAME}.tar
                #       move the compressed archive to /root/data
                mv ${TIMESTAMP}-{${DIRNAME}.tar.gz /root/data
        fi
done
That's it.

I would recommend that you use the first method -- you'll only have one dated archive to deal with and you'll be able to extract all of it or only parts that you want. The other methods give you a large number of archive files to deal with and that may not be desirable as time goes on.

Hope this helps some.
 
1 members found this post helpful.
Old 09-15-2011, 03:37 PM   #5
resolv_25
Member
 
Registered: Aug 2011
Location: Croatia
Distribution: Debian 7/Ubuntu 14.04
Posts: 57

Rep: Reputation: 9
Yes, good scripts here.

Another way to accomplish the same is using some tool as backup-manager
After installation, there is a config file, /etc/backup-manager.conf (if I remember correctly)
and you can choose easily which directory to backup and time to live (how many days/weeks will this backup exist).
Good way the save the storage place, otherwise, in a couple of weeks or months you may have a large data.
Same tools may do backup of database, as MySQL, also, you may pipe some command if needed etc.
Backup manager is run on desirable time, again as cron job.
Also, it may copy data on another server via ssh or ftp.
Very easy to customize, I think it could be suitable for this purpose.

Last edited by resolv_25; 09-15-2011 at 03:42 PM.
 
  


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
Shell Script: Getting signal handlers to work when running an application by script pkjss Programming 4 09-14-2011 05:23 AM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 02:27 PM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 04:05 AM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 09:40 AM
I want to run script on the server from client machine in windows in a perl script vpradeep Linux - Newbie 2 09-01-2008 04:29 AM


All times are GMT -5. The time now is 11:50 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