Apps :: 7 day file backup script?



Hello All,
Can anyone provide a 7 day round robin file backup script. I'm trying to backup a file once a day such that I have a weekly archive of the file. It doesn't need to be an incremental backup and the file can be renamed xyz1-10. Brute force will do. I have cron running already so the script can be run from cron once a day.

Not sure I understand your goal. What kind of file? Are you just trying to write like a daily log into a weekly file so you, in effect, end up with 52 weekly logs at the end of the year?

If that's all it is, you can do that with a couple simple lines in cron. The first to run weekly (depending which day you want to start) either by itself or via script to touch a file using date so you have a file named "file-year-weeknumber.log" or whatever. The next would run daily and cat whatever file you want written daily to the file opened in the previous sentence. A third run at whichever day to clear out the previous daily entry.

Is that what you mean?

I saw you were on after I replied but you didn't answer. If what I wrote is what you want to do, you can use a script like this to start the weekly log:
Code Sample
#!/bin/bash
lognumber=$(date +%Y%W)
exec touch filename.$lognumber

Give it a name (logmaker), chmod +x, in $PATH (such as /opt/bin), add a weekly cron job with that. Run this week, it gives you a file named "filename.200803" and the next week would be the same but 04. That will give you sequential weekly logs if that's what you want. It's also an optional step -- the next one should create the file if it doesn't already exist, but my preference is to use touch even if it's an extra step.

EDIT: Cleaned up everything and am leaving just this part to reduce possible confusion. This would be the daily script to set in your crontab...
Code Sample
#!/bin/bash

# set variables
lognumber=$(date +%Y%W)
logday=$(date [whatever you want -- standard date, day of week, etc.])

# if you want a timestamp:
echo $logday >> filename.$lognumber

# this line does what you seem to want:
cat thefileyouwantlogged >> filename.$lognumber

# the next two entries are to clear and restart the daily log file:
rm thefileyouwantlogged
touch thefileyouwantlogged


The last two entries are redundant if the file already resets itself daily anyway. You can also leave out the logday and echo $logday entries if you don't care about timestamping each entry. Set the logday to whatever variables you want (Google: linux man date). Use full paths for "thefileyouwantlogged" and "filename.$lognumber" so you won't have any drama.

I don't know what you meant by brute force. Or even if this is what you wanted. Many ways to skin a cat. Like I said, I would do a weekly job to touch the weekly log file.

If you just want to back it up, this is how Apple does it for the OSX daily/weekly/monthly maintenance scripts (Which are based off the Berkley BSD scripts):

Code Sample

#!/bin/sh -
echo ""
printf %s "Rotating log files:"
cd /var/log
for i in system.log; do
   if [ -f "${i}" ]; then
       printf %s " ${i}"
       if [ -x /usr/bin/gzip ]; then gzext=".gz"; else gzext=""; fi
       if [ -f "${i}.5${gzext}" ]; then mv -f "${i}.5${gzext}" "${i}.6${gzext}"; fi
       if [ -f "${i}.4${gzext}" ]; then mv -f "${i}.4${gzext}" "${i}.5${gzext}"; fi
       if [ -f "${i}.3${gzext}" ]; then mv -f "${i}.3${gzext}" "${i}.4${gzext}"; fi
       if [ -f "${i}.2${gzext}" ]; then mv -f "${i}.2${gzext}" "${i}.3${gzext}"; fi
       if [ -f "${i}.1${gzext}" ]; then mv -f "${i}.1${gzext}" "${i}.2${gzext}"; fi
       if [ -f "${i}.0${gzext}" ]; then mv -f "${i}.0${gzext}" "${i}.1${gzext}"; fi
       if [ -f "${i}" ]; then
             touch "${i}.$$" && chmod 640 "${i}.$$" && chown root:admin "${i}.$$"
             mv -f "${i}" "${i}.0" && mv "${i}.$$" "${i}" && if [ -x /usr/bin/gzip ]; then
               gzip -9 "${i}.0"; fi
       fi
   fi
done



Bash or Sh get the number of files with "file-name" as the start, and depending on the number there, move each one up in name and then compresses the last one, creating a new file.

This script keeps a total of 8 files (original, original.0-6.gz). Add/remove "if [ -f "${i}.5${gzext}" ]; then mv -f "${i}.5${gzext}" "${i}.6${gzext}"; fi" statements as needed. Just keep in mind that this script would be ran daily, and would always keep the 9 files around, but will force overwrite the oldest one every day. So original.7.gz would be overwritten every day.

For a weekly script to back up the files (If you actually want a copy of every file for every week, and not just a backup of the last 7 days) would be simply

Code Sample

#!/bin/sh -
mkdir /path/to/weekly/backup/$(date +%W-%y)
cp /path/to/files/filename.[0-6].gz /path/to/weekly/backup/$(date +%W-%y)/

Run that on either day 1 of the week before the daily script or day 7 of the week after the daily script (After the file has been created and is done being used).

Also, if you don't need to gzip the file. just remove "&& if [ -x /usr/bin/gzip ]; then gzip -9 "${i}.0"; fi" part of the daily script.

That OSX script is terrible =o)
Next Page...
original here.