Backup Basics
From CafeWiki
To create a tarball of all files in a directory:
tar -czvf <backupfilename>.tar.gz /path/to/stuff/to/backup *
where: -c create
-z zip
-v verbose
-f filename
To extract the contents of the backup tarball into the current directory:
tar -xzvf <backupfilename>.tar.gz
where: -x extract
-z zip
-v verbose
-f filename
Here is a bash script that creates backups of specified directories and saves them with the date created in the filename. The second part of the script looks for backup files that are older than a specified retention limit and removes them.
#!/bin/bash ######################################################### # Directory Backup # # Name: BU.sh # Date: 15 Aug 2009 # Author: Dave # Version: 1.0 # # Description: This short script runs # a tar command to create # a full file backup for each of the # specified directories # ######################################################### # tar everything in each of the specified directories in turn # tar options create, zip, preserve permissions, filename tar -czpf /path/to/backups/directory/filename1_`date +"%Y-%m-%d"`.tar.gz /path/to/specified/directory1/ tar -czpf /path/to/backups/directory/filename2_`date +"%Y-%m-%d"`.tar.gz /path/to/specified/directory2/ # add additional directories as needed... # find all backup (-name "*.tar.gz") files (- type f) older than 60 days (-mtime +60) # and delete (-delete) them # this allows the last 8 weeks of backups to be on hand before being deleted find /path/to/backups/directory/ -name "*.tar.gz" -type f -mtime +60 -delete
To make this useful, it needs to run regularly without manual intervention, for this I add it to the crontab which executes the script once each week:
$> crontab -l ... # scripted backup of all important directories every Monday at 3:30am 30 3 * * 1 /path/to/scripts/BU.sh ...
