Home > Command & Utility > Backup Script

Backup Script

August 21, 2011 Posted by KP

My simple backup script
It runs daily with cron job.

#!/bin/sh

# directory to backup
BACKUP_DIR=’/home/’

# directory to store backup files
DEST_DIR=’/backup/’

# number of days to store backup files
MAX_DAYS=30

# base backup file name
BASE_FILENAME=’home’

# remove files that are 30 or more days old.
find $DEST_DIR -mtime +$MAX_DAYS -maxdepth 1 -name ‘*.tgz’ -exec /bin/rm -f ‘{}’ +

# generate backup file name with date stamp, no hour/minute info for easier remote backup.
destfilename=$BASE_FILENAME`date “+%Y%m%d”`.tgz

cd $BACKUP_DIR
tar czf $DEST_DIR$destfilename . &



Cross-server backup with scp
This script uses the same path info and runs later than the above one to make sure backup files have been generated, hence no need to check file existence. It runs weekly with cron job.

#!/bin/sh

BACKUP_DIR=’/backup/’
BASE_FILENAME=’home’
BASE_FILENAME_REMOTE=’remotehome’

# generate backup file name, the same with the backup script
local_filename=$BACKUP_DIR$BASE_FILENAME`date “+%Y%m%d”`.tgz
remote_filename=$BACKUP_DIR$BASE_FILENAME_REMOTE`date “+%Y%m%d”`.tgz

# copy remote backup file to local server
scp -i /root/.ssh/id_rsa -P 1234 loginame@hostname:$remote_filename $BACKUP_DIR

# copy local backup file to remote server
scp -i /root/.ssh/id_rsa -P 1234 $local_filename loginame@hostname:$BACKUP_DIR


Cron job

10 4 * * * /root/backup.sh
40 4 * * 0 /root/remote_backup.sh



Related Posts:

Filed Under: Command & Utility

Comments are closed.



Archives