Bash backup script for Linux and Windows (cygwin)
Posted by Erik Hjelmås - 23/03/12 at 12:03:17 pmWe do backups to protect against three threats:
- Accidential deletion of files and directories
- Laptop theft
- Hardware failure
Since I was unable to get the Back in Time (similar to a Mac’s Time Machine) working properly, I’ve written a script which addresses item 1 “Accidential deletion of files and directories”, meaning it does backups locally on your hard drive. Based on Mike Rubel’s excellent writing on making use of hard links in backups (and several others who published similar works), the script is written in bash and seems to run fine on both Linux and Windows (cygwin). More info in the start section of the script itself.
backupscript.bash with sample rsyncexcludes for linux and for windows (both should really be adapted based on your own preference).
Install instructions for Linux
- Make sure rsync is installed
- Edit two first sections of backupscript.bash and copy it to
/usr/local/bin/backupscript
and make it executable with chmod +x /usr/local/bin/backupscript - Copy rsyncexcludes to /usr/local/etc/
- Create the directory /backups and make yourself the owner of it
sudo chown username:username /backups - Run the first full backup manually
rsync -a --exclude-from=/usr/local/etc/rsyncexcludes \ /home/$(whoami)/ \ /backups/day-backup-$(date "+%d-%m-%Y") - Add a line to /etc/crontab
15,45 * * * * username /usr/local/bin/backupscript
Install instructions for Windows
- Install cygwin with bash, coreutils, rsync and an editor (e.g. vim).
- In an editor in Cygwin, edit two first sections of backupscript.bash
and copy it to
/usr/local/bin/backupscript - Copy rsyncexcludes to /usr/local/etc/
- Create the directory C:\backups
- Run the first full backup manually from Cygwin
rsync -a --exclude-from=/usr/local/etc/rsyncexcludes \ /cygdrive/c/Users/$(whoami)/ \ /backups/day-backup-$(date "+%d-%m-%Y") - Check that the backupscript runs fine manually from dosprompt or PowerShell:
C:\cygwin\bin\bash -c "/usr/local/bin/backupscript"
- Schedule C:\cygwin\bin\bash -c “/cygdrive/c/backups/backupscript”
to run every half hour with
taskschd.msc or schtasks.exe
Scripting tutorials: Perl
Posted by Erik Hjelmås - 22/03/11 at 06:03:18 amHere is a Perl tutorial based on the same framework as the Bash and PowerShell tutorials:
Perl tutorial (slides) | Perl tutorial (slides with lecture notes)
I would very much like as many comments on these as possible so I can improve them whenever I have time.
Scripting tutorials: PowerShell
Posted by Erik Hjelmås - 21/03/11 at 02:03:22 pmHere is a PowerShell tutorial based on the same framework as the Bash tutorial:
PowerShell tutorial (slides) | PowerShell tutorial (slides with lecture notes)
I would very much like as many comments on these as possible so I can improve them whenever I have time.
Scripting tutorials: Bash
Posted by Erik Hjelmås - 11/02/11 at 10:02:55 amI believe that a good way of learning different scripting languages is by studying them using the same basic structure and examples. This will help students understand the basic differences in syntax, and help them getting started scripting in a new language quite easy. Of course this comes at the expense of not being able to show all the powers of the specific language, but I think that should be left to a follow-up session instead of covering that in the getting-started session.
Using the same basic structure and examples also helps the students compare languages and gives them a basic understanding of which language to choose for the task at hand. Choosing the right tool for the right job is nice.
During my courses (operating systems; system administration) I teach the languages Bash, Perl, PowerShell and Cfengine. Since I have not found any teaching material on the internet which teaches these languages based on the principles I have mentioned above, I decided to write some material myself:
Bash tutorial (slides) | Bash tutorial (slides with lecture notes)
During the next month I will publish similar PDFs for Perl and PowerShell as well.
I would very much like as many comments on these as possible so I can improve them whenever I have time.
Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds.
Valid XHTML and CSS.