-2

Hello I'm new to Unix and I am trying to create a crontab job that moves all the files I have in my home directory where the name contains the letter f followed by a digit 1,3 or 7 to a directory called backups, on the 12th of April and November at 9:30 PM.

This is my home directory:

arsenal.by       flhome           list1            stmnpgs
arsenal.pass     flhome2          list2            test.c
assignment       foreachScript1   list2.c          testdir
availisting.csv  funxdir          local.cshrc      testfile
backups          funxdir2         local.login      tmp.test
backups1         homlnk           local.profile    train
biglist          lab4             myfile           treat
biglist.c        lab5             myfile2          trick
biglist2         lab6             Myhome.list      tricking
CFiles.tar.Z     lab7             myinfo.fl        troll
clssnotes.txt    lab8             myList           typescript
delfh            lec3             names.txt        workdir

If anyone could help me out with this it'd be much appreciated!

1 Answers1

0

Firstly home rolling a backup solution for work, professional or college, is usually a bad idea because the stakes of an error are potentially very high and local backups obviously have the possibility of being lost by whatever causes the original files to be inaccessible.

However it's a worthwhile exercise to show how you would do it in cron as it's a frequent type of task and it would provide you some cover while looking for a better solution.

Your date specification can be safely done as one cron entry as only the day of the year varies, if both the minute of the day and the day of the year (or the day of week) changed you would need two entries.

# M H DoM MoY DoW 
30 21 12 4,10 * BACKUPDIR=~/backups; ds=$(date +\%Y\%m\%d\%H\%M\%S); mkdir -p $BACKUPDIR; find ~/* -type d -prune -o -type f -name f\*\[137\] -exec mv {} $BACKUPDIR/{}.$ds \;

The find command is told to look at all entries in your home directory that do not start with a . ("visible" files), if they are directories to ignore them (do not descend the directory tree) and if they are files that start with an f to move them (not copy them) to the $BACKUPDIR. If you wanted any file containing an f instead the find pattern would be \*f\*\[137\]

Above we define two variables for the backup dir and a datestamp (the \ before the % are because it is is a special character to cron).

The file globbing patterns * and [] are similarly escaped because they are shell special and we want to pass them to the find command.

The reason to use a timestamp is that moving or copying files frequently causes unintentional overwriting of files so if the backup directory path does not contain a date stamp then the target file name should.

Lastly it might be better to use a tar command to create a compressed date stamped archive that you can easily copy elsewhere, a local backup directory is asking for trouble, particularly if nested underneath the directory you are working in.

eg: Something like

#!/bin/bash
backup_file=~/backups/backup.$(date +%Y%m%d%H%M%S).tar.gz
tar czf $backup_file $(find ~/* -type d -prune -o -type f -name f\*\[137\] -print)
# <Commands to copy the file elsewhere here>
# You should then copy this file elsewhere (another system) or email it to yourself (after possibly encrypting it)