LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-07-2014, 07:14 PM   #1
urglik
LQ Newbie
 
Registered: Sep 2014
Posts: 3

Rep: Reputation: Disabled
Need help with cron job syntax. Thank you.


hello everyone,

i've been having some fun setting up a vps to host some sites for my friends.

in an effort to back up their sites i'm using cron jobs and grive to tar the site folder and sync it to google drive.

the problem that has come up is that syncing looks for new or different file names and the cron job that tars the site folder saves it with same name each time by overwriting the existing file.

the syntax i use is

tar -cvpzf /site_backups/purpleearth.tar.gz /home/purpleearth/public_html

is there a way to have the tar file saved with a postfix like 01, 02, etc. depending on the existing files in the directory?

thank you for sharing your experience.
 
Old 09-07-2014, 07:28 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,552

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
The easiest and most descriptive option is to postfix the date instead of a counter. That way not only do you know "3 came after 2", but you know when each backup was actually saved, and you never have to worry about duplicates.

To do that, just change your tar command to:
Code:
tar -cvpzf /site_backups/purpleearth_$(date -u +%Y%m%d_%H%M%S).tar.gz /home/purpleearth/public_html
Of course you can change the format code to whatever you wish, use "man date" for the available options. The "-u" flag uses UTC for the timestamping rather than your current local time.
 
Old 09-07-2014, 07:49 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
Two things: don't use "-v" with tar (unless you need to capture and log output) and try to use a backup scheme that provides retention.

Code:
#!/bin/bash --
# Set debug mode when testing:
set -vxe
# Set default behaviour:
LANG=C; LC_ALL=C; export LANG LC_ALL
# Be nice:
which nice >/dev/null 2>&1 && nice -n +20 $$ >/dev/null 2>&1
which ionice >/dev/null 2>&1 && ionice -c3 -n3 -p $$ >/dev/null 2>&1
# This provides retention for a week and only backups newer files during the week:
NOW=$(/bin/date +%a); THEN=$(/bin/date +"%Y-%m-%d" --date="1 day ago")

case "$NOW" in
Sun) # Create full backup:
     tar -cpzf "/site_backups/purpleearth-${NOW}.tar.gz" /home/purpleearth/public_html
      ;;
*)   # Create backup relative to yesterday:
     tar --newer "$THEN" -cpzf "/site_backups/purpleearth-${NOW}.tar.gz" /home/purpleearth/public_html
     ;;
esac
# Now rsync stuff and exit
# rsync --whatever-args source dest;
exit 0
 
Old 09-07-2014, 09:08 PM   #4
urglik
LQ Newbie
 
Registered: Sep 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Doh!

Last edited by urglik; 09-07-2014 at 10:50 PM.
 
Old 09-07-2014, 10:48 PM   #5
urglik
LQ Newbie
 
Registered: Sep 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
wow,

thanks for the timely replies.


i tried suicidaleggrol's suggestion straight-away and it's a good quick fix to where i was at.

thank you. it brought my backup plan together.

i'm really digging grive.

google drive is stable, free, expansive.

for a beginner there were a lot of potential options at first glance.



unspawn, always being the one to take it too the next level,

if i can get my head around it, your suggestion intrigues me.

please to ask a few questions...

- i'm not following the rsync command. i had thought rsync was another option besides grive.
is this a different approach to backing up to google drive you are suggesting?

- is this a cron job?

- why did you include the first line?


trying to run before i learn how to walk...

thank you very much for your assistance.

Last edited by urglik; 09-07-2014 at 10:50 PM.
 
Old 09-08-2014, 01:27 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
Quote:
Originally Posted by urglik View Post
i'm not following the rsync command. i had thought rsync was another option besides grive.
is this a different approach to backing up to google drive you are suggesting?
No, by all means, if you use grive, keep using that.


Quote:
Originally Posted by urglik View Post
is this a cron job?
If you dump it in /etc/cron.daily/ it could be.


Quote:
Originally Posted by urglik View Post
why did you include the first line?
Since it is an executable shell script it should start with a shebang / hashbang (see http://en.wikipedia.org/wiki/Shebang_(Unix) or for gory details see for example http://www.in-ulm.de/~mascheck/various/shebang/) and exit politely (zero).
 
  


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
Error running cron job, syntax error? mpyusko Debian 2 12-20-2012 10:57 AM
Syntax for : Receiving email once Cron job is completed Linux_Newbee83 Linux - Newbie 5 01-04-2012 10:11 PM
linux cron job duplicate job question cpthk Linux - Newbie 4 09-11-2009 09:52 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 09:16 AM
Cron Job Syntax for a task in seconds AJones Linux - Software 2 07-22-2005 01:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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