Backup Bash Script
Yes, more Geekness, but this time hopefully useful to someone. In my boredom, I decided to create a backup script for my important Linux directories. This way hopefully next time I upgrade my OS or experiment a little too wildly, I will have backups of all the stuff I trashed. I figured it would be easy enough to do. I mean all I really had to do was copy the directories with all the attributes and permissions to my external drive. In Linux its as easy as cp -a
. So I generated a simple little script that copied my home directory to the backup drive. Then I discovered I need to test for whether or not the external drive was mounted. Turned out that was pretty easy to do with test -d.
Then things got complicated. Mind you, I know diddly about BASH scripting. I wanted it to be interactive when ran manually so that I would know exactly what was happening at what point. So I researched and discover read. So I had a great little backup script, but I wanted to add directories that had root access. So I had to test for root access and abort if not root (later I will just run it as root). So I had to research how to test for uid. Finally, and as it turned out the most frustrating part, was determining backups older than Xdays and deleting them. I tried a bunch of complicated find commands, but in the end all I needed was a simple date -d “7 days ago” string since the directories are structured by date in the backup.
Anyway, in the end, the script turned into a bunch of if:then loops, but it seems to work well. So I figured I’d post it up here in case anyone was looking for such a thing. I also created an automatic running version of the script that requires no user-input and logs output to a file so I could run it in a cron easily. Anyone know how to test for whether or not the script is running from a cron or not? ![]()
Backup script version 0.000000001 after the jump (hopefully not eaten by the CMS) . I’ll put the automatic one in the discussion forum. Feel free to edit and improve upon these as you see fit, and if you have any suggestions – suggest!