I have routinely kept my history &quot;best effort&quot; for many many years now by exporting HISTSIZE=10000 and HISTFILESIZE=1000000, and then occasionally doing a cp -a ~&#x2F;.bash_history{,-$(date +%s)}. I actually use this history archive, and wish it was higher fidelity. (In practice, I am thinking I should just run all my sessions through script... ;P.)<p><a href="http:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man1&#x2F;script.1.html" rel="nofollow">http:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man1&#x2F;script.1.html</a><p>I have thereby had it on my todo list for a month or so, ever since I learned of the bash DEBUG trap, to instead store my history into an sqlite3 database and have it separated by host. Seeing this reminded me &quot;oh yeah, I really should do that&quot; (as no: there&#x27;s no way in hell I&#x27;m going to just send all of the commands I type to some random guy with a website ;P).<p>This is &quot;the simplest thing that could possibly work&quot; and might very well break if you set some crazy history configuration variables I don&#x27;t use. Note that it works for pipes specifically and only because it can overwrite the same entry to the database multiple times using &quot;insert or replace&quot; (prevention of which is what normally makes these scripts so complex).<p><pre><code>    histsql=~&#x2F;.bash_sqlite3

    sqlite3 &quot;${histsql}&quot; &#x27;
        create table if not exists &quot;session&quot; (
            &quot;id&quot; integer not null primary key autoincrement,
            &quot;address&quot; text not null,
            &quot;process&quot; integer not null,
            &quot;tty&quot; text not null,
            &quot;user&quot; text not null,
            &quot;start&quot; timestamp not null default current_timestamp,
            &quot;end&quot; timestamp null
        );

        create table if not exists &quot;command&quot; (
            &quot;session&quot; integer not null,
            &quot;line&quot; integer not null,
            &quot;time&quot; timestamp not null default current_timestamp,
            &quot;pwd&quot; text not null,
            &quot;text&quot; text not null,
            primary key (&quot;session&quot;, &quot;line&quot;)
        );
    &#x27;

    histmac=$(ifconfig | sed -e &#x27;s&#x2F;  *$&#x2F;&#x2F;; &#x2F;\(ether\|HWaddr\) &#x2F; { s&#x2F;.* &#x2F;&#x2F;; q; }; d;&#x27;)
    histtty=$(tty)

    histssn=$(sqlite3 &quot;${histsql}&quot; &quot;
        insert into \&quot;session\&quot; (
            \&quot;address\&quot;, \&quot;process\&quot;, \&quot;tty\&quot;, \&quot;user\&quot;
        ) values (
            &#x27;${histmac&#x2F;&#x2F;\&#x27;&#x2F;&#x27;&#x27;}&#x27;, &#x27;${$&#x2F;&#x2F;\&#x27;&#x2F;&#x27;&#x27;}&#x27;,
            &#x27;${histtty&#x2F;&#x2F;\&#x27;&#x2F;&#x27;&#x27;}&#x27;, &#x27;${USER&#x2F;&#x2F;\&#x27;&#x2F;&#x27;&#x27;}&#x27;
        );

        select last_insert_rowid();
    &quot;)

    function histend {
        sqlite3 &quot;${histsql}&quot; &quot;
            update \&quot;session\&quot; set
                \&quot;end\&quot; = current_timestamp
            where
                \&quot;id\&quot; = &#x27;${histssn&#x2F;&#x2F;\&#x27;&#x2F;&#x27;&#x27;}&#x27;;
        &quot;
    }

    trap histend EXIT

    function histadd {
        local data=&quot;$(HISTTIMEFORMAT= history 1)&quot;
        if [[ -z $data ]]; then return; fi

        data=&quot;${data#&quot;${data%%[![:space:]]*}&quot;}&quot;
        local line=&quot;${data%%&#x27; &#x27;*}&quot;

        data=&quot;${data#*&#x27; &#x27;}&quot;
        data=&quot;${data#&quot;${data%%[![:space:]]*}&quot;}&quot;

        sqlite3 &quot;${histsql}&quot; &quot;
            insert or replace into \&quot;command\&quot; (
                \&quot;session\&quot;, \&quot;line\&quot;,
                \&quot;pwd\&quot;, \&quot;text\&quot;
            ) values (
                &#x27;${histssn&#x2F;&#x2F;\&#x27;&#x2F;&#x27;&#x27;}&#x27;, &#x27;${line&#x2F;&#x2F;\&#x27;&#x2F;&#x27;&#x27;}&#x27;,
                &#x27;${PWD&#x2F;&#x2F;\&#x27;&#x2F;&#x27;&#x27;}&#x27;, &#x27;${data&#x2F;&#x2F;\&#x27;&#x2F;&#x27;&#x27;}&#x27;
            );
        &quot;
    }

    trap histadd DEBUG</code></pre>