phirebird

Schedule a job under Linux using cron

My last post was for a backup script that tar’ed up an entire Linux box (with the exception of some useless directories) and then uploaded it to an FTP server. Useful stuff, but a pain if you have to run it yourself manually every day! Enter, cron.

Cron is the daemon that executes commands according to a set schedule, and crontab is the utility used to manipulate it. To add a job, login as root and issue the command crontab -e. You’ll be presented with an editor for you to add the new job. The format the new line will need to format is as follows:

* * * * * command
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

So, if we wanted our backup script (located at /back/dobackup.sh) to run at 10pm every night, you’d insert:

0 22 * * * /back/dobackup.sh

Save and exit (as you’re in vi, that’s the key sequence: escape : w q enter), and it’ll be added to your crontab. To check, issue: crontab -l

 

Did you find this hint useful? Or are you wondering how to learn more? Well, here’s a few books that I’ve found useful over the years:


Linux Command Line and Shell Scripting Bible
 
Linux: The Complete Reference, Sixth Edition
 
Linux Pocket Guide – Essential Commands
 
Linux All-in-one Desk Reference

 

 

Backup a Linux box through FTP (push) and tar

Another quick Linux how-to. This time how to backup up an entire box using into a tar archive and push it to an FTP server. In my case, this ran every night and the FTP server was the central tape backup unit.

It’s a good idea to create yourself a separate directory to store the backup shell script and FTP script. For want of a better name, I created /back (10 out of 10 for originality, right?). In there, first create the backup script dobackup.sh:

rm /back/*.tgz
d=$(date +%y%m%d)
tar cvpzf /back/back$d.tgz --exclude=/proc --exclude=/lost+found --exclude
    =/back --exclude=/mnt --exclude=/sys /
lftp -f /back/backftpscript
Then change the mode of the file to allow execution:
[root@nas01 /]# chmod a+x dobackup.sh
[root@nas01 /]#
And then create the second file backftpscript (the FTP script):
open -u <ftp username>,<ftp password> <ftp host>
mput /back/*.tgz
bye
That’s it – you’re good to go! Just execute /dobackup.sh and it’ll tar everything and then upload it to the specified FTP server.

Here’s an explanation as to how it works and line-by-line what’s going on:

rm /back/*.tgz
Remove last nights backup! (or traces of any others)

d=$(date +%y%m%d)
Grab todays date (in the format yymmdd) and stuff it a variable ($d)

tar cvpzf /back/back$d.tgz –exclude=/proc –exclude=/lost+found –exclude=/back –exclude=/mnt –exclude=/sys /
Create an archive, make it Verbose, Preserve the permissions, filter the archive through gZip, call the File /back/back$d.tgz (e.g back101022.tgz for 22nd Oct 2010).  Exclude from the archive useless stuff like /back (the backup dir itself), /sys, /mnt, etc. And finally the target of the archive, /

lftp -f /back/backftpscript
lftp is like the ftp command, but with a lot more functionality and protocols. Probably warrants a how-to of its own, but needless to says it’s a lot more useful for use in situations like these. This particular line starts lftp and runs our ftp script, backftpscript. The contents of which are:

open -u <ftp username>,<ftp password> <ftp host>
Pretty self explanatory. Open a connection to <ftp host> and login as <ftp username> using the password <ftp password>

mput /back/*.tgz
After we’ve connected, upload (or put) all the .tgz files from our local /back directory

bye
Bid farewell and drop the connection (in a nice, polite sort of way)

Need this to run on a nightly basis? Take a look at my other how-to on cron jobs

Are you wondering how to learn more about Linux? Well, here’s a few books that I’ve found useful over the years that might just help you more:


Linux Command Line and Shell Scripting Bible
 
Linux: The Complete Reference, Sixth Edition
 
Linux Pocket Guide – Essential Commands
 
Linux All-in-one Desk Reference

 

 

Creating and mounting an ext3 partition

It’s been a while, but here’s a quick how-to on creating a new file system and mounting on a Linux box.
My scenario was a 512MB CF card as an IDE slave – so /dev/hdb (b being the second IDE disk) – with no existing partitions, and create a mount point of /back (for backup purposes).

Here’s how:

[root@nas01 /]# fdisk /dev/hdb
Command (m for help): d
No partition is defined yet!
(Note: If you’ve got an existing partition on the drive, d will remove the partition and you’ll get a message like “Selected partition 1″. If you’ve got any additional partitions, d them too)
Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-993, default 1): <hit enter>
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-993, default 993): <hit enter>
Using default value 993
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

[root@nas01 /]# mkfs.ext3 /dev/hdb1
mke2fs 1.41.8 (11-July-2009)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
125488 inodes, 500440 blocks
25022 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
62 block groups
8192 blocks per group, 8192 fragments per group
2024 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@nas01 /]# mkdir /back

[root@nas01 /]# mount /dev/hdb1 /back

[root@nas01 /]# df -m
Filesystem           1M-blocks      Used Available Use% Mounted on
/dev/hda1                 1930       741      1090  41% /
tmpfs                            1980         0         1980  0%   /dev/shm
/dev/hdb1                  474          11       439      3%   /back

[root@nas01 /]#
If like me you want this to mount every time that you start up – just edit /etc/fstab and tag on a new line:

/dev/hdb1               /back                 ext3    defaults        1 1

 

Did you find this hint useful? Or are you wondering how to learn more? Well, here’s a few books that I’ve found useful over the years:


Linux Command Line and Shell Scripting Bible
 
Linux: The Complete Reference, Sixth Edition
 
Linux Pocket Guide – Essential Commands
 
Linux All-in-one Desk Reference

 

 

phirebird