Current position:  Home > Default > I need help making a backup script

I need help making a backup script

Time:November 30
Advertisement
I'm trying to create this full system backup script that I plan to run manually. I have some commands to compress and extract my root directory and everything in it, but they're not complete because there are a few other features I would like to have. The problem is that I have no idea how to implement these specific features, and my googling of bash scripting documentation only left me confused and at square 1. This is what I have so far (just commands I can run in the terminal):
To compress:
sudo tar -cpf ~/archive.tar.gz / --exclude=/proc
To extract & restore:
sudo tar -xpf /home/agi/archive.tar.gz -C /
I want this script to:
1. allows me to run as
sh ~/.bin/backup.sh [directory to back up]
What I mean is there should be some sort of parameter in the script so that when I run it, I can choose whatever directory I want to compress rather than just be limited to just /.
2. puts finished .tar.gz in my home directory (I plan to transfer the archive to external media or online later; this gives me the freedom to not need to have a specific drive connected to run the script). I already have this in the commands, but I just wanted to point out that this is important.
3. names finished .tar.gz DD-MM-YY-backup.tar.gz where DD is day, MM is month, YY is year.
4. the "sudo" part works correctly. That is, the script, when run, asks for a password and can be granted root permissions (or is granted root permissions in some other way) so that I can back up all parts of my system without these sorts of errors.
5. shows some sort of progress indicator. Either a progress bar, shows the tar program output, or something that gives me an indication that the script is working and perhaps how long it will take. This one is really optional, for as long as the process is working properly then I'm ok. But if it is possible and not ridiculously complicated or difficult, it would be very nice.
Advertisement
What I mean is there should be some sort of parameter in the script so that when I run it, I can choose whatever directory I want to compress rather than just be limited to just /.
On my backup script, I specify $* which should accept any arguments passed to the script.. ex: $ ./backup.sh /source/folder /source/folder2.
in the script:
#!/bin/sh
tar -cpf $HOME/$(date +%d%m%Y)-backup.tar.gz $* --exclude=/proc
2. puts finished .tar.gz in my home directory (I plan to transfer the archive to external media or online later; this gives me the freedom to not need to have a specific drive connected to run the script). I already have this in the commands, but I just wanted to point out that this is important.
In the above script, that is what the ~ or $HOME should do..
3. names finished .tar.gz DD-MM-YY-backup.tar.gz where DD is day, MM is month, YY is year.
Same thing, the above script should do the trick, you might want to modify the date parameters (check the date manual)
4. the "sudo" part works correctly. That is, the script, when run, asks for a password and can be granted root permissions (or is granted root permissions in some other way) so that I can back up all parts of my system without these sorts of errors.
You could put something to prevent the script from running unless you are root, or you could have it prompt for a password and use sudo within the script. I would recommend the first. Here is a code snippet:
#Check for root
if [ $(whoami) != "root" ]; then
echo "Error: You cannot perform this operation unless you are root"
exit 1
fi
5. shows some sort of progress indicator. Either a progress bar, shows the tar program output, or something that gives me an indication that the script is working and perhaps how long it will take. This one is really optional, for as long as the process is working properly then I'm ok. But if it is possible and not ridiculously complicated or difficult, it would be very nice.
You might be able to use a similar program to tar, but with progress. Maybe someone else could shed some light here..
Hope this helps...