Introduction
There are 2 ways to control files on multiple machines in the software development world.
- Deploy or build file from version control system
- Backup files to version control system
In my opinion, ideally, everything should be done by the way 1 because it ensures the files are same across the all machines.
However in the real world, sometime it is hard to deploy all files from version control system because of the various reasons - such as linux/unix permission or organization structure or too difficult to automating deployment or etc. etc....
In that case, we should take the way 2.
In this post, I will introduce simple bash script for back up files to Subversion.
Of course you can use your favorite version control system such as Git instead of Subversion ;)
If you are a software developer, you can easily refine my bash script and create git version quickly ;)
Bash Script for Back Up Files to Subversion
See below script! You just save the script and simply execute it!
Note: I use "scp" command but you can use rsync command which synchronize files between remote host and local host.
#!/bin/bash SVN_USER="your svn user name" SVN_PASSWORD="your svn password" SVN_BASE_COMMAND="svn --username $SVN_USER --password $SVN_PASSWORD --no-auth-cache --non-interactive --ignore-externals --quiet" SVN_BASE_COMMIT_COMMAND="svn --username $SVN_USER --password $SVN_PASSWORD --no-auth-cache --non-interactive --quiet" checkout(){ CHECKOUT_DIR=${1//$SVN_ROOT/} CHECKOUT_DIR=${CHECKOUT_DIR////_} echo checkout directory $1 to $CHECKOUT_DIR if [ ! -d $HOME/$CHECKOUT_DIR ] then $SVN_BASE_COMMAND co $1 $HOME/$CHECKOUT_DIR else $SVN_BASE_COMMAND update $HOME/$CHECKOUT_DIR fi } # you can use rsync command instead of "scp + rm command combination" if rsync is available scp_conf_files() { host=$1 dest_dir=$host dest_dir_conf=$dest_dir"/httpd_conf" dest_dir_php_ini=$dest_dir"/php_ini" echo "copying from $host" # create directory if it doesn't exist `mkdir -p $CHECKOUT_DIR/$dest_dir_conf` # remove files except .svn directory `find $CHECKOUT_DIR/$dest_dir_conf -type f | grep -v "\.svn" | xargs rm -f` # scp files from target remote host # you should put appropriate 'user' and 'host' for your environment # in this example, we copy apahce httpd.conf, extra directories, php.ini `scp -r user@$host:/etc/httpd/conf/httpd.conf $CHECKOUT_DIR/$dest_dir_conf/` `scp -r user@$host:/etc/httpd/conf/extra $CHECKOUT_DIR/$dest_dir_conf/` `mkdir -p $CHECKOUT_DIR/$dest_dir_php_ini` `scp -r user@$host:/etc/php.ini $CHECKOUT_DIR/$dest_dir_php_ini/` # create timestamp in order to record when the backup is done timestamp=`date +%Y%m%d%H%M%S` `echo -e "$timestamp" > $CHECKOUT_DIR/$dest_dir/timestamp.txt` } set -e set -o pipefail echo "=== start backup ===" cd $HOME echo "= checking out backup from svn repo =" checkout "your svn repository url" echo "= done =" echo "= committing files =" # add new or updated files to svn version control `$SVN_BASE_COMMIT_COMMAND add $CHECKOUT_DIR/* --force` # list up deleted files deleted_files=$(echo `$SVN_BASE_COMMAND status $CHECKOUT_DIR/| grep '^!' | awk '{print $2}'`) # if there are deleted files, marked as svn delete if [ ! -z "$deleted_files" -a "$deleted_files" != " " ] then `$SVN_BASE_COMMIT_COMMAND delete $deleted_files` fi # commit files! `$SVN_BASE_COMMIT_COMMAND commit $CHECKOUT_DIR -m "Auto backup by script"` echo "= done =" echo "=== backup done ==="
コメント