rc.local : copy/backup files between hard drive & ram

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
4nt01n3

rc.local : copy/backup files between hard drive & ram

Post by 4nt01n3 »

Hello,

I'm trying to do something and following it's an example with my /home/antoine/.cache folder :

In my fstab, I want to create /media/ramdisk as a tmpfs

Code: Select all

tmpfs	/media/ramdisk/	    tmpfs	"options"	0	0
What I want to do is
1) create /media/ramdisk/.cache folder in it and a symlink to /home/antoine/.cache when I boot
2) copy the .cache folder on the ramdisk to my /home/antoine when I shutdown my computer

Then I've edited the /etc/rc.local file

Code: Select all

#!/bin/sh -e
#
# rc.local
#
/bin/mkdir /media/ramdisk/.cache
/bin/mv /home/antoine/.cache /media/.cache
/bin/ln -s /media/.cache /home/antoine/.cache
exit 0
I've adapted /etc/rc.local & /etc/init.d/rc.local to create etc/rc.local.shutdown & /etc/init.d/rc.local.shutdown files.
- /etc/rc.local.shutdown

Code: Select all

#!/bin/sh -e
#
# rc.local.shutdown
#
rm /home/antoine/.cache
mv /media/ramdisk/.cache /home/antoine
exit 0
- /etc/init.d/rc.local.shutdown (copy & adapt from the rc.local file)

Code: Select all

#! /bin/sh
### BEGIN INIT INFO
# Provides:          rc.local
# Required-Start:    $all
# Required-Stop:
# Default-Start:     0
# Default-Stop:
# Short-Description: Run /etc/rc.local.shutdown if it exist
### END INIT INFO


PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start() {
	if [ -x /etc/rc.local.shutdown ]; then
	        [ "$VERBOSE" != no ] && log_begin_msg "Running local shutdown scripts (/etc/rc.local.shutdown)"
		/etc/rc.local.shutdown
		ES=$?
		[ "$VERBOSE" != no ] && log_end_msg $ES
		return $ES
	fi
}

case "$1" in
    start)
	do_start
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    stop)
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac
Can someone check if what I do can work ?
And after that, I need to "update-rc.d", that's right ?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: rc.local : copy/backup files between hard drive & ram

Post by xenopeek »

Some errors in your /etc/rc.local file, it should be this I think:

Code: Select all

#!/bin/sh -e
#
# rc.local
#
/bin/mv /home/antoine/.cache /media/ramdisk
/bin/ln -s /media/ramdisk/.cache /home/antoine/.cache
exit 0
Image
4nt01n3

Re: rc.local : copy/backup files between hard drive & ram

Post by 4nt01n3 »

Yeah thanks, lots of errors.

(sorry for my english :/ )

I want to copy/backup files between USB stick & RAM because I'm going to travel with just a linux distribution on a USB stick with some files. With my friends we just take an old VAIO i bought in 2000, then if we lost it or if someone steals it, I always have my system and my files to work (writing, editing pictures,...) on another PC.

I'm trying to have the system as minimal as I need and if it's possible copy all is needed on a ramdisk at startup, create symlinks on the usb stick and then backup the ramdisk at shutdown, to improve system & software uses, especially with USB1 connection

So what I've done (here is an exemple with some firefox folders on my own PC)

1) I've created /media/RAMDISK folder

2) In my fstab I mount /media/RAMDISK as tmpfs, as I've done previously

3) I try to adapt the /etc/init.d/rc.local script :

Code: Select all

#! /bin/sh
### BEGIN INIT INFO
# Provides:          rc.local
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 6
# Short-Description: Run /etc/rc.local and /etc/rc.local.shutdown
### END INIT INFO


PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start() {
	if [ -x /etc/rc.local ]; then
	        [ "$VERBOSE" != no ] && log_begin_msg "Running local boot script (/etc/rc.local)"
		 /etc/rc.local
		 ES=$?
 		[ "$VERBOSE" != no ] && log_end_msg $ES
		return $ES
	fi
}

do_stop() {
	if [ -x /etc/rc.local.shutdown ]; then
	        [ "$VERBOSE" != no ] && log_begin_msg "Running local shutdown script (/etc/rc.local.shutdown)"
 		/etc/rc.local.shutdown
		ES=$?
 		[ "$VERBOSE" != no ] && log_end_msg $ES
 		return $ES
	fi
}

case "$1" in
    start)
	do_start
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    stop)
	do_stop
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac
3) Now I've these /etc/rc.local and /etc/rc.local.shutdown

rc.local

Code: Select all

#!/bin/sh -e
#
/bin/mv /home/antoine/.cache /media/RAMDISK #move files from usb stick to RAMDISK
/bin/ln -s /media/RAMDISK/.cache /home/antoine/.cache #create symlink
/bin/mv /home/antoine/.mozilla /media/RAMDISK
/bin/ln -s /media/RAMDISK/.mozilla /home/antoine/.mozilla
exit 0
rc.local.shutdown

Code: Select all

#!/bin/sh -e
#
/bin/cp -ur media/RAMDISK/.cache /home/antoine/.backup #backup files, it's safety if there is a problem the next time I use the PC
/bin/rm /home/antoine/.cache #remove symlink
/bin/mv /media/RAMDISK/.cache /home/antoine #put the files from RAMDISK to usb stick
/bin/cp -ur /media/RAMDISK/.mozilla /home/antoine/.backup
/bin/rm /home/antoine/.mozilla
/bin/mv /media/RAMDISK/.mozilla /home/antoine
exit 0
Finally I've created symlinks in the /etc/rcX.d folders :

Code: Select all

update-rc.d /etc/init.d/rc.local start 99 2 3 4 5 . stop 01 0 6 .
But something is wrong, that doesn't work, I'm just a new explorer of linux possibilities.
Maybe it's not a good idea (copying/backup files at startup/shutdown may take more time than if I use the system on the usb stick)
Maybe someone know an easiest solution to do what I want ?
Locked

Return to “Scripts & Bash”