4

I have a bash script that runs as a cron to backup files on the server.

#!/bin/bash
FILE=/path/to/backup_$(date +%Y%m%d).tar
tar -cf $FILE /backup/this /and/that /and/someotherfiles
gzip $FILE

When I run the script directly using:

sudo bash ./backup-files.sh

It gzips the file, but last night when the cron ran it left it as a tar. Would the cron have saved a log somewhere that'd point to why this may be the case?

Marco Ceppi
  • 48,827
bcmcfc
  • 848

3 Answers3

6

Is the partition where backup is located at its limits?

Your script has created the tarball, but gzip didn't have any space left to compress it.

(you can combine both commands with -z flag for tar)

Woops, I forgot one thing: add -v flag to your tar command. It will display what it does.

6

As Pierre has mentioned, you may want to use the -z flag. Generally, I always use tar zcvfP for backing up entire directories and preserving their structure and permissions. The v flag is there too, also useful.

TuxSax
  • 193
3

If it happens again that your script doesn't do what you want, you can add set -x on top of your script. It will output exactly what it does and this output is often helpful to find bugs. If you call it as a cronjob make sure that $EMAIL is set appropriate.

qbi
  • 19,515