I want to backup my ubuntu filesystem, and I wrote this little script. It is very basic, but being my first try I am afraid to do mistakes. And since it will take few hours to complete to see results, I think it is better to ask you as experienced programmers if I did something wrong.
I’m particularly interested in > will that record output of mv or will it output also results of tar?
Also variables inside tar command is it correct way?
#!/bin/bash
mybackupname="backup-fullsys-$(date +%Y-%m-%d).tar.gz"
{ time tar -cfpzv $mybackupname --exclude=/$mybackupname --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev / && ls -gh $mybackupname && mv -v $mybackupname backups/filesystem/ ; } > backup-system.log
exit
Anything I should know before I run this?
Sandro, you might want to consider spacing things out in your script and producing individual errors. Makes things much easier to read.
If you wanted to keep debug information (like the stderr of
tarormv), 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.The series of
ifs causes each program to be run as long as the previous one was successful. It’s like chaining your commands with&&, but lets you run other code in case of failure.Note that I’ve changed the order of options for
tar, because the thing that comes after-fis the file you’re saving things to. Also, the-poption is only useful when extracting files from a tar. Permissions are always saved when you create (-c) a tar.Others might wish to note that this usage of the
statcommand works in GNU/Linux, but not other unices like FreeBSD or Mac OSX. In BSD, you’d usestat -f%z $mybackupname.