You are not logged in.

#1 2015-06-14 13:21:32

jo-shva
Member
From: Sherman, TX USA
Registered: 2010-10-18
Posts: 133

bashrc add btrfs snapshot [SOLVED]

I'd like to add a snapshot directive to my bashrc to make it faster and easier.  I haven't had much sucess doing this on my own.  I am looking for help to add the following:

echo `date "+%Y%m%d-%H%M%S"` > /run/btrfs-root/__current/ROOT/SNAPSHOT
echo "Fresh install" >> /run/btrfs-root/__current/ROOT/SNAPSHOT
btrfs subvolume snapshot -r /run/btrfs-root/__current/ROOT /run/btrfs-root/__snapshot/ROOT@`head -n 1 /run/btrfs-root/__current/ROOT/SNAPSHOT`
rm /run/btrfs-root/__current/ROOT/SNAPSHOT

And have the ROOT part as open to change to home or var.
I know it must be run as su, not sudo.
I hve very little experience with bash this is why I am asking for help, nothing I came up with worked.
Help would be greatly appreciated.

Last edited by jo-shva (2015-06-15 10:25:25)


"Democracy is being allowed to vote for the candidate you dislike least." -- Robert Byrne
http://killhellokitty.deviantart.com/

Offline

#2 2015-06-15 08:37:15

runical
Member
From: The Netherlands
Registered: 2012-03-03
Posts: 896

Re: bashrc add btrfs snapshot [SOLVED]

What exactly do you want to achieve? Do you want to add an automatic snapshot to the bashrc (because that will not work or go horribly wrong) or do you want to make a function that you can run at will?

Offline

#3 2015-06-15 08:59:02

jo-shva
Member
From: Sherman, TX USA
Registered: 2010-10-18
Posts: 133

Re: bashrc add btrfs snapshot [SOLVED]

A function sounds good.  I tried everything I could think of to use an alias to run it and it went terribley wrong.
I guess you could say I'd like to simplify the task.
Thank you for getting back to me.

Last edited by jo-shva (2015-06-15 09:02:44)


"Democracy is being allowed to vote for the candidate you dislike least." -- Robert Byrne
http://killhellokitty.deviantart.com/

Offline

#4 2015-06-15 09:08:37

runical
Member
From: The Netherlands
Registered: 2012-03-03
Posts: 896

Re: bashrc add btrfs snapshot [SOLVED]

In that case you should just put everything in a function. Just put the commands you normally run in a function block and run that. See [1] for a simple example. The resulting function can be run using su (using sudo or su to run non-root commands as a normal user), or you can just put su (or sudo) in front of the command you need to be run as root.  Good luck!

[1] http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html

Offline

#5 2015-06-15 09:25:24

jo-shva
Member
From: Sherman, TX USA
Registered: 2010-10-18
Posts: 133

Re: bashrc add btrfs snapshot [SOLVED]

Do I do it as a single function, or as multiple called functions?

#!/bin/bash 
           function snapshot {
su echo `date "+%Y%m%d-%H%M%S"` > /run/btrfs-root/__current/ROOT/SNAPSHOT}
{echo "Fresh install" >> /run/btrfs-root/__current/ROOT/SNAPSHOT}
{btrfs subvolume snapshot -r /run/btrfs-root/__current/ROOT /run/btrfs-root/__snapshot/ROOT@`head -n 1 /run/btrfs-root/__current/ROOT/SNAPSHOT`}
{rm /run/btrfs-root/__current/ROOT/SNAPSHOT}

Or does each line need to be run as a separate function?

Last edited by jo-shva (2015-06-15 09:51:41)


"Democracy is being allowed to vote for the candidate you dislike least." -- Robert Byrne
http://killhellokitty.deviantart.com/

Offline

#6 2015-06-15 09:50:48

jo-shva
Member
From: Sherman, TX USA
Registered: 2010-10-18
Posts: 133

Re: bashrc add btrfs snapshot [SOLVED]

then I tried:

function snapshotD {
su echo `date "+%Y%m%d-%H%M%S"` > /run/btrfs-root/__current/ROOT/SNAPSHOT}
function snapshotN {echo "Latest" >> /run/btrfs-root/__current/ROOT/SNAPSHOT}
function snapshot {btrfs subvolume snapshot -r /run/btrfs-root/__current/ROOT /run/btrfs-root/__snapshot/ROOT@`head -n 1 /run/btrfs-root/__current/ROOT/SNAPSHOT`}
function snapshotC {rm /run/btrfs-root/__current/ROOT/SNAPSHOT}

that give me an error syntax error near unexpected token `{echo'


"Democracy is being allowed to vote for the candidate you dislike least." -- Robert Byrne
http://killhellokitty.deviantart.com/

Offline

#7 2015-06-15 10:24:15

runical
Member
From: The Netherlands
Registered: 2012-03-03
Posts: 896

Re: bashrc add btrfs snapshot [SOLVED]

A function goes between {}. It is C or Java where you surround the function body in brackets. So instead of putting each line between brackets, you put the brackets around the full function body.
You also need to quote complex commands when executing them with su, but the su in your function seems extraneous as all commands need to be run using su I think? (I don't use btrfs) So, you should either run the function using sudoor quote the whole function (like you quote for echo and date) and put it into su using su -c. The sudo way is simple using an alias:

alias snapshot="sudo snapshot"

I have included a slightly corrected version that should get you on the way:

function snapshot {
         echo `date "+%Y%m%d-%H%M%S"` > /run/btrfs-root/__current/ROOT/SNAPSHOT
         echo "Fresh install" >> /run/btrfs-root/__current/ROOT/SNAPSHOT
         btrfs subvolume snapshot -r /run/btrfs-root/__current/ROOT /run/btrfs-root/__snapshot/ROOT@`head -n 1 /run/btrfs-root/__current/ROOT/SNAPSHOT`
         rm /run/btrfs-root/__current/ROOT/SNAPSHOT
}

EDIT: Please look up the exact quoting rules and some basic shell scripting.

Last edited by runical (2015-06-15 10:25:15)

Offline

#8 2015-06-15 10:24:56

jo-shva
Member
From: Sherman, TX USA
Registered: 2010-10-18
Posts: 133

Re: bashrc add btrfs snapshot [SOLVED]

This is the answer:

function snapshot { 
echo `date "+%Y%m%d-%H%M%S"` > /run/btrfs-root/__current/$1/SNAPSHOT &&
echo "Fresh install" >> /run/btrfs-root/__current/$1/SNAPSHOT &&
btrfs subvolume snapshot -r /run/btrfs-root/__current/$1 /run/btrfs-root/__snapshot/$1@`head -n 1 /run/btrfs-root/__current/$1/SNAPSHOT` &&
rm /run/btrfs-root/__current/$1/SNAPSHOT 
}

but I had to place it in roots bashrc for it to work.

Thank you runical for pointing me in the right direction.  It work properly now.

Last edited by jo-shva (2015-06-15 10:54:54)


"Democracy is being allowed to vote for the candidate you dislike least." -- Robert Byrne
http://killhellokitty.deviantart.com/

Offline

#9 2015-06-15 10:27:28

runical
Member
From: The Netherlands
Registered: 2012-03-03
Posts: 896

Re: bashrc add btrfs snapshot [SOLVED]

The && might be extraneous (it backgrounds the process if I recall correctly). And yeah, I found out that aliases and su -c don't go together as well.

Last edited by runical (2015-06-15 10:28:04)

Offline

#10 2015-06-15 10:59:44

jo-shva
Member
From: Sherman, TX USA
Registered: 2010-10-18
Posts: 133

Re: bashrc add btrfs snapshot [SOLVED]

the && works, I could use || but I don't understand the differance.  And yes, I can't add su to the beginging of it, I just have to type it in first.


"Democracy is being allowed to vote for the candidate you dislike least." -- Robert Byrne
http://killhellokitty.deviantart.com/

Offline

#11 2015-06-15 11:52:49

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,573
Website

Re: bashrc add btrfs snapshot [SOLVED]

The && does not background anything, that would be a single &.  The double ampersand is a logical operator and likely has no business being in this function - especially if || would work as well.

Functions can be multiple lines, that's the whole point.  You don't need anything at then end to extend to a new line, just do it like Runical did in post #7.

The && is the logical and, so it has a side effect in this function (which might actually be useful) that each command is only executed if the one before it succeeds.  The || (logical or) would be complete nonsense, as the function would only progress untila single command was successful - so the whole function would stop after the first line.

If you actually want the same thing as the side effect of the && then it might be better to use the `set` command - I think it is `set -e` so that any command that fails causes that (sub)shell to exit immediately.

Jo-shva, You may want to take some time checking out Greg's Wiki as knowing some basic shell scripting - or at least how shell commands work - can make using arch much easier.  If nothing else, it will help protect you from making silly mistakes that might end up causing problems.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#12 2015-06-15 12:05:38

runical
Member
From: The Netherlands
Registered: 2012-03-03
Posts: 896

Re: bashrc add btrfs snapshot [SOLVED]

Trilby wrote:

The && does not background anything, that would be a single &.

Ah, yes. Silly me. Thanks for the correction.

Offline

#13 2015-06-15 12:23:30

jo-shva
Member
From: Sherman, TX USA
Registered: 2010-10-18
Posts: 133

Re: bashrc add btrfs snapshot [SOLVED]

Thank you for your helpfull message and clearing up the questions as to what the && and|| would do and of cource that they are unneeded in a function.  I hadn't found any examples of functions with multilines for me to go by.  I will follow your advice as I could use better bash knowledge.  Thank You!


"Democracy is being allowed to vote for the candidate you dislike least." -- Robert Byrne
http://killhellokitty.deviantart.com/

Offline

#14 2015-06-15 17:00:41

Spider.007
Member
Registered: 2004-06-20
Posts: 1,175

Re: bashrc add btrfs snapshot [SOLVED]

Just an FYI, I don't know if you wrote this or you copied it from somewhere but it can be reduced to simply:

btrfs subvolume snapshot -r /run/btrfs-root/__current/ROOT /run/btrfs-root/__snapshot/ROOT@`date "+%Y%m%d-%H%M%S"`

something I also found interesting, how did this partition end up in /run/btrfs-root; did you create an fstab entry for that? It seems like a very peculiar location; /run isn't usually used for mounts...

Offline

#15 2015-06-15 19:46:50

jo-shva
Member
From: Sherman, TX USA
Registered: 2010-10-18
Posts: 133

Re: bashrc add btrfs snapshot [SOLVED]

Cool, nice to see it reduced to one line.
I got the install from this guide: http://blog.fabio.mancinelli.me/2012/12 … BTRFS.html
It was the only guide I could find that worked; for installing btrfs with subvolumes.


"Democracy is being allowed to vote for the candidate you dislike least." -- Robert Byrne
http://killhellokitty.deviantart.com/

Offline

Board footer

Powered by FluxBB