Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sandro, you might want to consider spacing things out in your script and producing individual errors. Makes things much easier to read.</p> <pre><code>#!/bin/bash mybackupname="backup-fullsys-$(date +%Y-%m-%d).tar.gz" # Record start time by epoch second start=$(date '+%s') # List of excludes in a bash array, for easier reading. excludes=(--exclude=/$mybackupname) excludes+=(--exclude=/proc) excludes+=(--exclude=/lost+found) excludes+=(--exclude=/sys) excludes+=(--exclude=/mnt) excludes+=(--exclude=/media) excludes+=(--exclude=/dev) if ! tar -czf "$mybackupname" "${excludes[@]}" /; then status="tar failed" elif ! mv "$mybackupname" backups/filesystem/ ; then status="mv failed" else status="success: size=$(stat -c%s backups/filesystem/$mybackupname) duration=$((`date '+%s'` - $start))" fi # Log to system log; handle this using syslog(8). logger -t backup "$status" </code></pre> <p>If you wanted to keep debug information (like the stderr of <code>tar</code> or <code>mv</code>), that could be handled with redirection to a tmpfile or debug file. But if the command is being run via cron and has output, cron should send it to you via email. A silent cron job is a successful cron job.</p> <p>The series of <code>if</code>s causes each program to be run as long as the previous one was successful. It's like chaining your commands with <code>&amp;&amp;</code>, but lets you run other code in case of failure.</p> <p>Note that I've changed the order of options for <code>tar</code>, because the thing that comes after <code>-f</code> is the file you're saving things to. Also, the <code>-p</code> option is only useful when <em>extracting</em> files from a tar. Permissions are always saved when you create (<code>-c</code>) a tar.</p> <p>Others might wish to note that this usage of the <code>stat</code> command works in GNU/Linux, but not other unices like FreeBSD or Mac OSX. In BSD, you'd use <code>stat -f%z $mybackupname</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload