Page 1 of 1

Modyfied ways of saving changes

Posted: 12 Feb 2017, 20:14
by tome
I have added some prefixes to /etc/changes-exit.conf (start of lines descriptions):

"/" - file/dir saved during reboot and shutdown when 'changes=EXIT:' cheatcode is used
"!/" - exclude particular subfolder(s) if parent directory is designed to be saved during shutdown ('changes=EXIT:')
For below, new added prefixes 'changes=EXIT:' is unneeded:
"@/" - "path" will be symlink to "LINKDEST"/"path" made during boot (if target exist and other conditions are fulfilled)
"+/" - if file/dir was changed will be copied to "COPYDEST" during shutdown
"++/" - like "+/" but will be created numbered backups of newer files next to copied ones
"+-/" - like "+/" but will be created backups moved to Trash
"&" - line will be executed (designed for special symlinks)

Below is next part of my /etc/changes-exit.conf:

Code: Select all

# Additional descriptions:
# "+/", "++" and "+-" are designed to keep some (not large) files in rootcopy or another place
# "@" - designed to keep large, important or frequently changed content of directories/files between reboots. If paths are files or non empty directories made by you outside modules, symlinks will not be created - it is better to use it without changes=/ cheatcode. Paths with space characters may be not supported.
# By default LINKSTREE is "/" (LINKSTREE=/) and it should be set or changed only for testing

# Uncomment proper destinations or change them, only one for COPYDEST and one for LINKDEST: 
# Below COPYDEST is /data/rootcopies/rootcopy3 on boot device and LINKDEST is /data on boot device
#COPYDEST=grep -A1 "Booting" /var/log/porteus-livedbg|tail -n1|sed 's^//^/^g' | sed 's^$^/data/rootcopies/rootcopy3^'
#LINKDEST=grep -A1 "Booting" /var/log/porteus-livedbg|tail -n1|sed 's^//^/^g' | sed 's^$^/data^'

#COPYDEST=/mnt/sda1/data/rootcopies/rootcopy3

#LINKDEST=/mnt/data
#LINKDEST=/tmp/data
In order to save changes in this way you need linkstree.sh script run at start (/etc/rc.d/rc.local) and copyto.sh script run during shutdown (/etc/rc.d/rc.local_shutdown):

linkstree.sh:

Code: Select all

#!/bin/sh

LINKSTREE=`cat /etc/changes-exit.conf | grep '^\s*LINKSTREE\s*=' |  sed -n '$p' | sed 's:^\s*LINKSTREE\s*=\s*::' | sed 's:\s*$::' | sed 's:^\`::' | sed 's:\`$::'`
LINKDEST=`cat /etc/changes-exit.conf | grep '^\s*LINKDEST\s*=' |  sed -n '$p' | sed 's:^\s*LINKDEST\s*=\s*::' | sed 's:\s*$::' | sed 's:^\`::' | sed 's:\`$::'`

[[ `eval "$LINKDEST" 2>/dev/null` ]] && LINKDEST=`eval "$LINKDEST"`

[[ `eval "$LINKSTREE" 2>/dev/null` ]] && LINKSTREE=`eval "$LINKSTREE"`
! [[ "$LINKSTREE" = "" || "$LINKSTREE" = "/" ]] && mkdir -p "$LINKSTREE"

if [[ ! "$LINKDEST" || "$LINKSTREE" == "$LINKDEST" ]]; then echo "Paths not set for symlinks"; else
s=`cat /etc/changes-exit.conf | grep '^@/' | sed s:^@/:/: | tr ' ' '#'`
#d=`cat /etc/changes-exit.conf | grep '^@/' | sed s:^@/:$LINKDEST/: | grep -v ' '`
#mkdir -p $d
t=`cat /etc/changes-exit.conf | grep '^@/' | sed s:^@/:$LINKSTREE/: | sed s:/$:: | sed 's/\(.*\)\/.*/\1/' | sort -u | grep -v ' '`
hgt=`cat /etc/changes-exit.conf | grep '^@/home/guest' | sed s:^@/:$LINKSTREE/: | sed s:/$:: | sed 's/\(.*\)\/.*/\1/' | sort -u | grep -v ' '`
rt=`cat /etc/changes-exit.conf | grep '^@/' | sed s:^@/:$LINKSTREE/: | sed s:/$:: | sed 's/\(.*\)\/.*/\1/' | sort -u | grep -v ' \|/home/guest'`
td=`cat /etc/changes-exit.conf | grep '^@/' | sed s:^@/:$LINKSTREE/: | grep -v ' '`

mkdir -p "$LINKSTREE/home"
a=`stat -c %a "$LINKSTREE/home"`
chmod 777 "$LINKSTREE/home"
su -l guest -c "mkdir -p `echo $hgt`"
mkdir -p $rt
chmod 755 "$LINKSTREE/home"
chmod $a "$LINKSTREE/home"

if [[ "$LINKSTREE" != "" && "$LINKSTREE" != "/" ]]; then 
rm -fr $td
for i in $s; do
i=`echo $i | tr '#' ' '`
ln -snfT  "$LINKDEST$i" "$LINKSTREE$i"
done;
else
for i in $s; do 
i=`echo $i | tr '#' ' '`
if ! [[ "$( ls -A "/mnt/live/memory/changes$i")" || "$( ls -A "/mnt/live/memory/images/changes$i")" || -f "/mnt/live/memory/changes$i" || -f "/mnt/live/memory/images/changes$i" || ! -e "$LINKDEST$i" || -e "$LINKSTREE$i" && "`df -T "$LINKSTREE$i"/ | awk '{print $2}' | sed -n '2p'`" != "aufs" ]] 2>/dev/null; then
rm -rf  "$LINKSTREE$i"
ln -snfT  "$LINKDEST$i" "$LINKSTREE$i"
fi
done
fi
fi


apart=`cat /etc/changes-exit.conf | grep '^&' | sed s:^\&:: | tr ' ' '#'`

for i in $apart; do 
i=`echo $i | tr '#' ' '`
$i
done
copyto.sh:

Code: Select all

#!/bin/sh

COPYDEST=`cat /etc/changes-exit.conf | grep '^\s*COPYDEST\s*=' |  sed -n '$p' | sed 's:^\s*COPYDEST\s*=\s*::' | sed 's:\s*$::' | sed 's:^\`::' | sed 's:\`$::'`

[[ `eval "$COPYDEST" 2>/dev/null` ]] && COPYDEST=`eval "$COPYDEST"`
mkdir -p "$COPYDEST" 2>/dev/null

if [[ -e "$COPYDEST" ]]; then

cd /mnt/live/memory/changes
r1=`cat /etc/changes-exit.conf | grep '^+/' | sed s:^+/:: | tr ' ' '#'`
r2=`cat /etc/changes-exit.conf | grep '^++/' | sed s:^++/:: | tr ' ' '#'`
r3=`cat /etc/changes-exit.conf | grep '^+-/' | sed s:^+-/:: | tr ' ' '#'`

#r2
for i in $r2; do 
i=`echo $i | tr '#' ' '`
#cp -afu  --backup=off --parents "$i" "$COPYDEST" 2>/dev/null
cp -afu  --backup=t --parents "$i" "$COPYDEST" 2>/dev/null
#gvfs-trash -f "$COPYDEST/$i.~"*~
done

#r1
for i in $r1; do 
i=`echo $i | tr '#' ' '`
cp -afu  --backup=off --parents "$i" "$COPYDEST" 2>/dev/null
#cp -afu  --backup=t --parents "$i" "$COPYDEST" 2>/dev/null
#gvfs-trash -f "$COPYDEST/$i.~"*~
done

#r3
for i in $r3; do 
i=`echo $i | tr '#' ' '`
#cp -afu  --backup=off --parents "$i" "$COPYDEST" 2>/dev/null
cp -afu  --backup=simple --parents "$i" "$COPYDEST" 2>/dev/null
gvfs-trash -f "$COPYDEST/$i~" "$COPYDEST/$i"/*~ "$COPYDEST/$i"/*/*~ "$COPYDEST/$i"/*/*/*~
done

cd
fi

#remove all numbered backups from $COPYDEST
#for x in `find $COPYDEST -name "*~*~" 2>/dev/null | tr ' ' '#'`; do x=`echo $x | tr '#' ' ' `; test -e "$COPYDEST/$x" && gvfs-trash -f "$COPYDEST/$x"; done
I have made some tests and it works quite good.