You are not logged in.

#1 2013-10-12 00:44:18

Bluerider
Member
Registered: 2012-07-22
Posts: 97

TMPFS Backup of Certain Folders

Salutations! I've written a bash script named tmpfs_backupv2 that links certain folders to a ramdisk and backs them up every 5 minutes. Can I get some code review before I release it as GPLv3?

  • Requirements     : rsync, bash

  • Modifiable Lines  :

    • flush_dirs      | directories to back up every five minutes

    • cache_dirs    |   directories to not backup

    • backup_dir   |   directory to backup to

    • flush_time    |   time to flush flush_dirs to backup (follows sleep format)

    • bin_path       |   path of script

    To see the available options, just run the script:

    <path to script>/tmpfs_backupv2 --help

I currently run it on startup in $HOME/.xprofile on my Live USB.

Save as tmpfs_backupv2

#!/bin/bash

## script to backup tmpfs home directories
## copyright by Mark Lee

main() {  ## main insertion function
  ## set some directories and variables
  declare -ra flush_dirs=("$HOME/.config");              ## directory : folders to move to ram
  declare -ra cache_dirs=("$HOME/.cache");               ## directory : cache
  declare -r  ram_dir="/tmp/$(whoami)-tmpfs-cache";      ## directory : tmpfs root directory
  declare -r  backup_dir="$HOME/tmpfs-back";             ## directory : backup directory
  declare -r  flush_time="5m";                           ## time      : time to flush to backup_dir
                                                         ##   (follows sleep format)
  declare -r  stop_sync="$ram_dir/stop-sync.lck";        ## lock      : stop syncing all files
  declare -r  bin_path="$HOME/Scripts/tmpfs_backup-pkg"; ## set the binary 

  ## run some functions
  parse-args $@;                                         ## parse command line switches
  check-setup;                                           ## check to see if dirs are properly linked
  ram-sync-daemon;                                       ## sync flush_dirs to backup_dir all
}

check-setup() { ## setup tmpfs directories
  if [ ! -d "$backup_dir" ]; then                        ## check if backup directory exists and
     mkdir "$backup_dir" &&                              ## make backup directory if it doesn't
     for a in ${cache_dirs[*]} ${flush_dirs[*]}; do
         cp -r "$a" "$backup_dir"/;                      ## flush all cache & non-volatile dirs to backup
     done;
  fi;
  
  if [ ! -d "$ram_dir" ]; then                           ## check if tmpfs root exists and
     mkdir "$ram_dir" &&                                 ## make tmpfs root if it doesn't and
     for a in ${flush_dirs[*]}; do
         name="$(basename $a)";                          ## strip the directory of its root path
         cp -r "$backup_dir"/"$name" "$ram_dir"/ ||      ## flush backed up files to tmpfs root
         mkdir "$ram_dir/$name";                         ##  make the ram directory if there was no backup
         rm -r "$a";                                     ## remove the permanent directory
         ln -s "$ram_dir/$name" "$a";                    ## soft link cache & non-volatile dirs to tmpfs root
     done;
     for a in ${cache_dirs[*]}; do
         name="$(basename $a)";                          ## strip the directory of its root path
         mkdir "$ram_dir"/"$name";                       ## make the tmpfs cache directory
         rm -r "$a";                                     ## remove the cache directory (might not exist)
         ln -s "$ram_dir/$name" "$a";                    ## soft link cache directory to tmpfs root
     done;
  fi;
}

flush-cache() {  ## flush cache to backup
  for a in ${flush_dirs[*]}; do                          ## loop for all flush_dirs
      name="$(basename $a)";                             ## strip the directory of its root path    
      rsync -r --delete \
       "$ram_dir/$name" "$backup_dir"/;                  ## flush to backup_dir
  done;
}

ram-sync-daemon() {  ## daemon to sync directories to disk
  ##############################################################
  #    flush_dirs  | sync every $flush_time  to $backup_dir    #
  #    cache_dirs  | sync only on shutdown                     #
  ##############################################################
  while [[ ! -e "$stop_sync" ]] ;do                      ## run unless lock file is present
     sleep $flush_time;                                  ## wait for flush_time
     flush-cache;                                        ## flush cache to backup
  done;
}

help-man() {  ## print help manual
echo '
 where options are:
  [usage]: tmpfs_backup [options]
  
  -r    or --restart        | restart tmpfs_backup
  -nc   or --no-check       | do not run tmpfs_backup setup
  -f    or --flush-cache    | flush tmpfs to backup
  -kill or --kill           | kill tmpfs_backup ram-sync-daemon
  -h    or --help           | print this help screen

  Examples:
     tmpfs_backup                <run tmpfs_backup> 
     tmpfs_backup -flush-cache   <flush tmpfs cache immediately>
     tmpfs_backup -restart       <restart tmpfs_backup>
     tmpfs_backup -kill          <shut down ram-sync-daemon>

  Press "Ctrl-C" to exit any time
'
}

parse-args() {  ## handle command line switches                               
  for a in $@; do                                        ## check all switches passed
     case $a in                                          ## evaluate switches according to cases
          -nc|-no-check|--no-check)
             alias check-setup='echo >/dev/null'         ## link check-setup to a null function
             ;;
          -r|-restart|--restart)
             echo "Restarting tmpfs_backup...";          ## restart tmpfs_backup by generating
             touch "$stop_sync" &&                       ##   stop_sync file and
             rm "$stop_sync" &&                          ##   delete stop_sync file and
             nohup \
              bash "$bin_path/tmpfs_backupv2" -nc &      ##   fork tmpfs_backup and don't setup dirs 
             echo "Restarted tmpfs_backup";
             exit 0;
             ;;
          -kill|--kill)
             echo "Killing tmpfs_backup...";
             touch "$stop_sync" &&                       ## generate the stop_sync file to
             flush-cache;                                ##   stop ram-sync-daemon and
             echo "Killed tmpfs_backup!";                ##   flush the cache and 
             exit 0;                                     ##   exit the script
             ;;
          -f|-flush-cache|--flush-cache)
             flush-cache &&                              ## flush cache immediately to backup
             echo "Flushed cache to backup";
             exit 0;
             ;;
          -h|-help|--help|/?)
             help-man;                                   ## print the help manual and
             exit 0;                                     ##   exit the script
             ;;
          *)
             echo "Unrecognized option : $a";            ## tell user that the switch is not supported
             help-man;                                   ##   print the help manual and
             exit 1;                                     ##   exit the script
             ;;
     esac;
  done;
}

main $@;  ## run the main insertion function

Last edited by Bluerider (2013-10-12 01:40:45)

Offline

#2 2013-10-12 00:51:58

Bluerider
Member
Registered: 2012-07-22
Posts: 97

Re: TMPFS Backup of Certain Folders

I've had comments on how I comment things before; if it is easier to read the script without comments just use the following code :

echo '#!/bin/bash' > <path to file>/tmpfs_backupv2-stripped;
awk -F'#' '{print $1}' <path to file>/tmpfs_backupv2 |  \
awk 'NF' >> <path to file>/tmpfs_backupv2-stripped

tmpfs_backupv2-stripped will not have comments.

Last edited by Bluerider (2013-10-12 00:56:11)

Offline

#3 2013-10-12 11:25:21

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Re: TMPFS Backup of Certain Folders

Seems similar to anything-sync-daemon which is mentionned in the SSD page of the wiki.

Offline

#4 2013-10-12 16:50:53

Bluerider
Member
Registered: 2012-07-22
Posts: 97

Re: TMPFS Backup of Certain Folders

I'm reading anything-sync-daemon.in. Mine has the big difference of not requiring root access. Users call the script so there shouldn't be large security issues regarding directory permissions.

Offline

#5 2013-10-12 16:58:18

Bluerider
Member
Registered: 2012-07-22
Posts: 97

Re: TMPFS Backup of Certain Folders

In addition, I'm seeing that the anything-sync-daemon.in uses a substantially bigger script (245 lines vs. 129 lines). I'm glad that anything-sync-daemon exists (I haven't tested it out or even heard of it before it was mentioned by stqn), but I feel tmpfs_backupv2 is more concise, cleaner, and sets good default directory permissions.

Offline

#6 2013-10-13 12:21:42

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,678
Website

Re: TMPFS Backup of Certain Folders

Bluerider wrote:

In addition, I'm seeing that the anything-sync-daemon.in uses a substantially bigger script (245 lines vs. 129 lines). I'm glad that anything-sync-daemon exists (I haven't tested it out or even heard of it before it was mentioned by stqn), but I feel tmpfs_backupv2 is more concise, cleaner, and sets good default directory permissions.

These are two different products.  ASD is full blown systemd service controlled product that allow so the syncing of multiple targets by multiple users.  Your product is a user-level one.  ASD is heavily commented and has many one-liners spread over multiple lines with \ characters for ease of reading, so comparing the total # of lines is a pretty pointless metric.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#7 2013-10-13 16:06:53

Bluerider
Member
Registered: 2012-07-22
Posts: 97

Re: TMPFS Backup of Certain Folders

graysky wrote:
Bluerider wrote:

In addition, I'm seeing that the anything-sync-daemon.in uses a substantially bigger script (245 lines vs. 129 lines). I'm glad that anything-sync-daemon exists (I haven't tested it out or even heard of it before it was mentioned by stqn), but I feel tmpfs_backupv2 is more concise, cleaner, and sets good default directory permissions.

These are two different products.  ASD is full blown systemd service controlled product that allow so the syncing of multiple targets by multiple users.  Your product is a user-level one.  ASD is heavily commented and has many one-liners spread over multiple lines with \ characters for ease of reading, so comparing the total # of lines is a pretty pointless metric.

Since they are different products, I'm wondering if there's a place for my script on the AUR.

Offline

#8 2013-10-13 16:50:58

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,678
Website

Re: TMPFS Backup of Certain Folders

The AUR is the Wild Wild West; there is no formal approval process.  Just upload a PKGBUILD for your wares and it's there.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#9 2013-10-14 02:40:22

Bluerider
Member
Registered: 2012-07-22
Posts: 97

Re: TMPFS Backup of Certain Folders

I've patched the system locking issue caused by syncing files with slow write speed (like to my USB stick). I now use a copy on write system where tmpfs files are copied in ram and then flushed to backup. See updated code.

#!/bin/bash

## script to backup tmpfs home directories
## written by Mark Lee

main() {  ## main insertion function
  ## set some directories and variables
  declare -ra flush_dirs=("$HOME/.config");              ## directory : folders to move to ram
  declare -ra cache_dirs=("$HOME/.cache");               ## directory : cache
  declare -r  ram_dir="/tmp/$(whoami)-tmpfs-cache";      ## directory : tmpfs root directory
  declare -r  backup_dir="$HOME/tmpfs-back";             ## directory : backup directory
  declare -r  COW="$ram_dir/COW";                        ## directory : Copy-on-write to stop system locking
                                                         ##              from slow writes to backup
  declare -r  flush_time="5m";                           ## time      : time to flush to backup_dir
                                                         ##   (follows sleep format)
  declare -r  stop_sync="$ram_dir/stop-sync.lck";        ## lock      : stop syncing all files
  declare -r  bin_path="$HOME/Scripts/tmpfs_backup-pkg"; ## set the binary 

  ## run some functions
  parse-args $@;                                         ## parse command line switches
  check-setup;                                           ## check to see if dirs are properly linked
  ram-sync-daemon;                                       ## sync flush_dirs to backup_dir all
}

check-setup() { ## setup tmpfs directories
  if [ ! -d "$backup_dir" ]; then                        ## check if backup directory exists and
     mkdir "$backup_dir" &&                              ## make backup directory if it doesn't
     for a in ${cache_dirs[*]} ${flush_dirs[*]}; do
         cp -r "$a" "$backup_dir"/;                      ## flush all cache & non-volatile dirs to backup
     done;
  fi;
  
  if [ ! -d "$ram_dir" ]; then                           ## check if tmpfs root exists and
     mkdir "$ram_dir" &&                                 ## make tmpfs root if it doesn't and
     mkdir "$COW"  &&                                    ## make copy-on-write directory
     for a in ${flush_dirs[*]}; do
         name="$(basename $a)";                          ## strip the directory of its root path
         cp -r "$backup_dir"/"$name" "$ram_dir"/ ||      ## flush backed up files to tmpfs root
            mkdir "$ram_dir/$name";                      ##  make the ram directory if there was no backup
         rm -r "$a";                                     ## remove the permanent directory
         ln -s "$ram_dir/$name" "$a";                    ## soft link cache & non-volatile dirs to tmpfs root
     done;
     for a in ${cache_dirs[*]}; do
         name="$(basename $a)";                          ## strip the directory of its root path
         mkdir "$ram_dir"/"$name";                       ## make the tmpfs cache directory
         rm -r "$a";                                     ## remove the cache directory (might not exist)
         ln -s "$ram_dir/$name" "$a";                    ## soft link cache directory to tmpfs root
     done;
  fi;
}

function flush-cache() {  ## flush cache to backup
  for a in ${flush_dirs[*]}; do                          ## loop for all flush_dirs
      name="$(basename $a)";                             ## strip the directory of its root path
      rsync -r "$ram_dir/$name" "$COW/";                 ## COW-like copy to stop system from locking due
                                                         ##   to slow writes to backup
      rsync -r --delete "$COW/$name" "$backup_dir/";     ## flush files to backup
      rm -r "$COW/$name"                                 ## remove COW file
  done;
}

ram-sync-daemon() {  ## daemon to sync directories to disk
  ##############################################################
  #    flush_dirs  | sync every $flush_time  to $backup_dir    #
  #    cache_dirs  | sync only on shutdown                     #
  ##############################################################
  while [[ ! -e "$stop_sync" ]] ;do                      ## run unless lock file is present
     sleep $flush_time;                                  ## wait for flush_time
     flush-cache;                                        ## flush cache to backup
  done;
}

help-man() {  ## print help manual
echo '
 where options are:
  [usage]: tmpfs_backup [options]
  
  -r    or --restart        | restart tmpfs_backup
  -nc   or --no-check       | do not run tmpfs_backup setup
  -f    or --flush-cache    | flush tmpfs to backup
  -kill or --kill           | kill tmpfs_backup ram-sync-daemon
  -h    or --help           | print this help screen

  Examples:
     tmpfs_backup                <run tmpfs_backup> 
     tmpfs_backup -flush-cache   <flush tmpfs cache immediately>
     tmpfs_backup -restart       <restart tmpfs_backup>
     tmpfs_backup -kill          <shut down ram-sync-daemon>

  Press "Ctrl-C" to exit any time
'
}

parse-args() {  ## handle command line switches                               
  for a in $@; do                                        ## check all switches passed
     case $a in                                          ## evaluate switches according to cases
          -nc|-no-check|--no-check)
             alias check-setup='echo >/dev/null'         ## link check-setup to a null function
             ;;
          -r|-restart|--restart)
             echo "Restarting tmpfs_backup...";          ## restart tmpfs_backup by generating
             touch "$stop_sync" &&                       ##   stop_sync file and
             rm "$stop_sync" &&                          ##   delete stop_sync file and
             nohup \
              bash "$bin_path/tmpfs_backupv2" -nc &      ##   fork tmpfs_backup and don't setup dirs 
             echo "Restarted tmpfs_backup";
             exit 0;
             ;;
          -kill|--kill)
             echo "Killing tmpfs_backup...";
             touch "$stop_sync" &&                       ## generate the stop_sync file to
             flush-cache;                                ##   stop ram-sync-daemon and
             echo "Killed tmpfs_backup!";                ##   flush the cache and 
             exit 0;                                     ##   exit the script
             ;;
          -f|-flush-cache|--flush-cache)
             flush-cache &&                              ## flush cache immediately to backup
             echo "Flushed cache to backup";
             exit 0;
             ;;
          -h|-help|--help|/?)
             help-man;                                   ## print the help manual and
             exit 0;                                     ##   exit the script
             ;;
          *)
             echo "Unrecognized option : $a";            ## tell user that the switch is not supported
             help-man;                                   ##   print the help manual and
             exit 1;                                     ##   exit the script
             ;;
     esac;
  done;
}

main $@;  ## run the main insertion function

Last edited by Bluerider (2013-10-14 02:40:39)

Offline

#10 2013-10-17 02:53:26

Bluerider
Member
Registered: 2012-07-22
Posts: 97

Re: TMPFS Backup of Certain Folders

I've split up the check function to allow online additions/removals from the flush_dirs and cache_dirs array. Now one can update the arrays in the script and run

tmpfs_backupv2 --reboot

to add/remove new/cached directories.

#!/bin/bash

## script to backup tmpfs home directories
## written by Mark Lee

main() {  ## main insertion function
  ## set some directories and variables
  declare -ra flush_dirs=("$HOME/.config");              ## directory : folders to move to ram
  declare -ra cache_dirs=("$HOME/.cache");               ## directory : cache
  declare -r  ram_dir="/tmp/$(whoami)-tmpfs-cache";      ## directory : tmpfs root directory
  declare -r  backup_dir="$HOME/tmpfs-back";             ## directory : backup directory
  declare -r  COW="$ram_dir/COW";                        ## directory : Copy-on-write to stop system locking
                                                         ##              from slow writes to backup
  declare -r  flush_time="5m";                           ## time      : time to flush to backup_dir
                                                         ##   (follows sleep format)
  declare -r  stop_sync="$ram_dir/stop-sync.lck";        ## lock      : stop syncing all files
  declare -r  bin_path="$HOME/Scripts/tmpfs_backup-pkg"; ## set the binary 

  ## run some functions
  parse-args $@;                                         ## parse command line switches
  check-setup;                                           ## check to see if dirs are properly linked
  ram-sync-daemon;                                       ## sync flush_dirs to backup_dir all
}

check-setup() { ## setup tmpfs directories
  ## ensure some directories are set up
  for a in "$backup_dir" "$ram_dir" "$COW"; do
      if [ ! -d "$a" ]; then
         mkdir "$a";                                    ## make directory if it doesn't exist
      fi;
  done;
  
  ## set up all non-volatile directories
  for a in "${flush_dirs[*]}"; do                          
      name=$(basename "$a");                             ## strip directory of path
      if [ ! -d "$ram_dir/$name" ]; then
         cp -r "$backup_dir/$name" "$ram_dir/" ||        ## flush backup to tmpfs root else
          cp -r "$a" "$ram_dir/" ||                      ##  flush original directory to tmpfs root else
           mkdir "$ram_dir/$name";                       ##   make tmpfs root cache directory
         rm -r "$a";                                     ## remove original directory (doesn't matter if it fails)
         ln -s "$ram_dir/$name" "$a";                    ## soft link non-volatile directory to tmpfs root
      fi;
  done;
  
  ## set up all cache directories
  for a in "${cache_dirs[*]}"; do
      name=$(basename "$a");                             ## strip directory of path
      if [ ! -d "$ram_dir/$name" ]; then
         mkdir "$ram_dir/$name";                         ## make tmpfs root cache directory
         rm -r "$a";                                     ## remove original directory (doesn't matter if it fails)
         ln -s "$ram_dir/$name" "$a";                    ## soft linke cache directory to tmpfs root
      fi;
  done;
}

function flush-cache() {  ## flush cache to backup
  for a in "${flush_dirs[*]}"; do                        ## loop for all flush_dirs
      name=$(basename "$a");                             ## strip the directory of its root path
      rsync -r "$ram_dir/$name" "$COW/";                 ## COW-like copy to stop system from locking due
                                                         ##   to slow writes to backup
      rsync -r --delete "$COW/$name" "$backup_dir/";     ## flush files to backup
      rm -r "$COW/$name";                                ## remove COW file
  done;
}

ram-sync-daemon() {  ## daemon to sync directories to disk
  ##############################################################
  #    flush_dirs  | sync every $flush_time  to $backup_dir    #
  #    cache_dirs  | sync only on shutdown                     #
  ##############################################################
  while [[ ! -e "$stop_sync" ]] ;do                      ## run unless lock file is present
     flush-cache;                                        ## flush cache to backup
    sleep $flush_time                                   ## wait for flush time
  done;
}

help-man() {  ## print help manual
echo '
 where options are:
  [usage]: tmpfs_backup [options]
  
  -start  or --start          | run tmpfs_backup
  -r      or --restart        | restart tmpfs_backup
                              |     does not run check function
  -reboot or --reboot         | reboot tmpfs_backup
                              |     runs check function
  -nc     or --no-check       | do not run tmpfs_backup setup
  -f      or --flush-cache    | flush tmpfs to backup
  -stop   or --stop           | kill tmpfs_backup ram-sync-daemon
  -h      or --help           | print this help screen

  Examples:
     tmpfs_backup                <run tmpfs_backup>
     tmpfs_backup -start         <run tmpfs_backup> 
     tmpfs_backup -flush-cache   <flush tmpfs cache immediately>
     tmpfs_backup -restart       <restart tmpfs_backup>
     tmpfs_backup -kill          <shut down ram-sync-daemon>

  Press "Ctrl-C" to exit any time
'
}

parse-args() {  ## handle command line switches                               
  for a in $@; do                                        ## check all switches passed
     case $a in                                          ## evaluate switches according to cases
          -start|--start)
             echo "Starting tmpfs_backupv3";
             ;;
          -nc|-no-check|--no-check)
             alias check-setup='echo >/dev/null'         ## link check-setup to a null function
             ;;
          -r|-restart|--restart)
             echo "Restarting tmpfs_backup...";          ## restart tmpfs_backup by generating
             touch "$stop_sync" &&                       ##   stop_sync file and
             rm "$stop_sync" &&                          ##   delete stop_sync file and
             nohup \
              bash "$bin_path/tmpfs_backupv2" -nc &      ##   fork tmpfs_backup and don't setup dirs 
             echo "Restarted tmpfs_backup";
             exit 0;
             ;;
           -reboot|--reboot)
             echo "Rebooting tmpfs_backup...";           ## reboot tmpfs_backup by generating
             touch "$stop_sync" &&                       ##  stop_sync file and
             flush-cache &&                              ##  flush cache immediately to backup and
             rm "$stop_sync" &&                          ##  delete stop_sync file and
             nohup \                           
              bash "$bin_path/tmpfs_backupv2" &          ## fork tmpfs_backup
             echo "Rebooted tmpfs_backup";
             exit 0;
             ;;
          -stop|--stop)
             echo "Stopping tmpfs_backup...";
             touch "$stop_sync" &&                       ## generate the stop_sync file to
             flush-cache;                                ##   stop ram-sync-daemon and
             echo "Stopped tmpfs_backup!";               ##   flush the cache and 
             exit 0;                                     ##   exit the script
             ;;
          -f|-flush-cache|--flush-cache)
             flush-cache &&                              ## flush cache immediately to backup
             echo "Flushed cache to backup";
             exit 0;
             ;;
          -h|-help|--help|/?)
             help-man;                                   ## print the help manual and
             exit 0;                                     ##   exit the script
             ;;
          *)
             echo "Unrecognized option : $a";            ## tell user that the switch is not supported
             help-man;                                   ##   print the help manual and
             exit 1;                                     ##   exit the script
             ;;
     esac;
  done;
}

main $@;  ## run the main insertion function

Last edited by Bluerider (2013-10-17 02:57:43)

Offline

#11 2013-10-18 18:53:12

Bluerider
Member
Registered: 2012-07-22
Posts: 97

Re: TMPFS Backup of Certain Folders

Script now handles shutdown automatically. This will be uploaded to the AUR soon.

#!/bin/bash

## script to backup tmpfs home directories
## written by Mark Lee

main() {  ## main insertion function
  ## set some directories and variables
  declare -ra flush_dirs=("$HOME/.config");              ## directory : folders to move to ram
  declare -ra cache_dirs=("$HOME/.cache");               ## directory : cache
  declare -r  ram_dir="/tmp/$(whoami)-tmpfs-cache";      ## directory : tmpfs root directory
  declare -r  backup_dir="$HOME/tmpfs-back";             ## directory : backup directory
  declare -r  COW="$ram_dir/COW";                        ## directory : Copy-on-write to stop system locking
                                                         ##              from slow writes to backup
  declare -r  flush_time="5m";                           ## time      : time to flush to backup_dir
                                                         ##   (follows sleep format)
  declare -r  stop_sync="$ram_dir/stop-sync.lck";        ## lock      : stop syncing all files
  declare -r  bin_path="$HOME/Scripts/tmpfs_backup-pkg"; ## set the binary 

  ## run some functions
  parse-args $@;                                         ## parse command line switches
  check-setup;                                           ## check to see if dirs are properly linked
  
  ram-sync-daemon &                                      ## sync flush_dirs to backup_dir all
}

check-setup() { ## setup tmpfs directories
  ## ensure some directories are set up
  for a in "$backup_dir" "$ram_dir" "$COW"; do
      if [ ! -d "$a" ]; then
         mkdir "$a";                                    ## make directory if it doesn't exist
      fi;
  done;
  
  ## set up all non-volatile directories
  for a in ${flush_dirs[*]}; do                          
      name=$(basename "$a");                             ## strip directory of path
      if [ ! -d "$ram_dir/$name" ]; then
         cp -r "$backup_dir/$name" "$ram_dir/" ||        ## flush backup to tmpfs root else
          cp -r "$a" "$ram_dir/" ||                      ##  flush original directory to tmpfs root else
           mkdir "$ram_dir/$name";                       ##   make tmpfs root cache directory
         rm -r "$a";                                     ## remove original directory (doesn't matter if it fails)
         ln -s "$ram_dir/$name" "$a";                    ## soft link non-volatile directory to tmpfs root
      fi;
  done;
  
  ## set up all cache directories
  for a in ${cache_dirs[*]}; do
      name=$(basename "$a");                             ## strip directory of path
      if [ ! -d "$ram_dir/$name" ]; then
         mkdir "$ram_dir/$name";                         ## make tmpfs root cache directory
         rm -r "$a";                                     ## remove original directory (doesn't matter if it fails)
         ln -s "$ram_dir/$name" "$a";                    ## soft linke cache directory to tmpfs root
      fi;
  done;
}

function flush-cache() {  ## flush cache to backup
  for a in ${flush_dirs[*]}; do                          ## loop for all flush_dirs
      name=$(basename "$a");                             ## strip the directory of its root path
      rsync -r "$ram_dir/$name" "$COW/";                 ## COW-like copy to stop system from locking due
                                                         ##   to slow writes to backup
      rsync -r --delete "$COW/$name" "$backup_dir/";     ## flush files to backup
      rm -r "$COW/$name";                                ## remove COW file
  done;
}

ram-sync-daemon() {  ## daemon to sync directories to disk
  ##############################################################
  #    flush_dirs  | sync every $flush_time  to $backup_dir    #
  #    cache_dirs  | sync only on shutdown                     #
  ##############################################################
  
  trap "bash $bin_path/tmpfs_backupv2 -stop" \
     SIGINT SIGKILL SIGTERM                              ## set up a trap to handle sigterm and kill signals 
     
  while [[ ! -e "$stop_sync" ]] ;do                      ## run unless lock file is present
     flush-cache;                                        ## flush cache to backup
     sleep $flush_time;                                  ## wait for flush_time
  done;
}

help-man() {  ## print help manual
echo '
 where options are:
  [usage]: tmpfs_backup [options]
  
  -start  or --start          | run tmpfs_backup
  -r      or --restart        | restart tmpfs_backup
                              |     does not run check function
  -reboot or --reboot         | reboot tmpfs_backup
                              |     runs check function
  -nc     or --no-check       | do not run tmpfs_backup setup
  -f      or --flush-cache    | flush tmpfs to backup
  -stop   or --stop           | kill tmpfs_backup ram-sync-daemon
  -h      or --help           | print this help screen

  Examples:
     tmpfs_backup                <run tmpfs_backup>
     tmpfs_backup -start         <run tmpfs_backup> 
     tmpfs_backup -flush-cache   <flush tmpfs cache immediately>
     tmpfs_backup -restart       <restart tmpfs_backup>
     tmpfs_backup -kill          <shut down ram-sync-daemon>

  Press "Ctrl-C" to exit any time
'
}

parse-args() {  ## handle command line switches                               
  for a in $@; do                                        ## check all switches passed
     case $a in                                          ## evaluate switches according to cases
          -start|--start)
             echo "Starting tmpfs_backupv3";
             ;;
          -nc|-no-check|--no-check)
             alias check-setup='echo >/dev/null'         ## link check-setup to a null function
             ;;
          -r|-restart|--restart)
             echo "Restarting tmpfs_backup...";          ## restart tmpfs_backup by generating
             touch "$stop_sync" &&                       ##   stop_sync file and
             rm "$stop_sync" &&                          ##   delete stop_sync file and
             nohup \
              bash "$bin_path/tmpfs_backupv2" -nc &      ##   fork tmpfs_backup and don't setup dirs 
             echo "Restarted tmpfs_backup";
             exit 0;
             ;;
           -reboot|--reboot)
             echo "Rebooting tmpfs_backup...";           ## reboot tmpfs_backup by generating
             touch "$stop_sync" &&                       ##  stop_sync file and
             flush-cache &&                              ##  flush cache immediately to backup and
             rm "$stop_sync" &&                          ##  delete stop_sync file and
             nohup \                           
              bash "$bin_path/tmpfs_backupv2" &          ## fork tmpfs_backup
             echo "Rebooted tmpfs_backup";
             exit 0;
             ;;
          -stop|--stop)
             echo "Stopping tmpfs_backup...";
             touch "$stop_sync" &&                       ## generate the stop_sync file to
             flush-cache;                                ##   stop ram-sync-daemon and
             echo "Stopped tmpfs_backup!";               ##   flush the cache and 
             exit 0;                                     ##   exit the script
             ;;
          -f|-flush-cache|--flush-cache)
             flush-cache &&                              ## flush cache immediately to backup
             echo "Flushed cache to backup";
             exit 0;
             ;;
          -h|-help|--help|/?)
             help-man;                                   ## print the help manual and
             exit 0;                                     ##   exit the script
             ;;
          *)
             echo "Unrecognized option : $a";            ## tell user that the switch is not supported
             help-man;                                   ##   print the help manual and
             exit 1;                                     ##   exit the script
             ;;
     esac;
  done;
}

main $@;  ## run the main insertion function

Last edited by Bluerider (2013-10-18 19:47:20)

Offline

Board footer

Powered by FluxBB