Search code examples
bashemacsorg-modeorg-babel

Reference to local directory inside org-mode 'src' block evaluated remotely via TRAMP


Hopefully this will be an easy one to solve. I am trying to backup my personal website straight into my HDD with a compact org-mode 'src' block, which is evaluated remotely via tramp.

#+BEGIN_SRC sh :results silent :dir /ssh:username@hostname:
  # Backup mysql database
  mysqldump -u user -p database > public_html/BACKUP.sql
  # Compress directory with the above file and the website main configs
  tar zcvf - public_html > $(date +%Y_%m)-website_backup.tar.gz
  # Remove database backup from server
  rm public_html/BACKUP.sql
#+END_SRC

I am happy with almost everything in the above code, however, I would like the tar command to create the output file on my machine or the HDD (locally essentially). Currently the tar.gz file is created in the remote machine. Is there a way to specify my local directory inside of the above code? Thank you in advance for any help.

UPDATE

The final setup is as follows

#+NAME: remote
#+BEGIN_SRC sh :results silent :dir /ssh:username@hostname:
  # Backup mysql database
  mysqldump -u user -p database > public_html/BACKUP.sql
  # Compress directory with the above file and the website main configs
  tar zcvf - public_html > $(date +%Y_%m)-website_backup.tar.gz
  # Remove database backup from server
  rm public_html/BACKUP.sql
#+END_SRC
#+NAME: local 
#+BEGIN_SRC sh :results silent :var r=remote
  echo $r
  rsync -av --remove-source-files -e ssh user@hostname:$(date +%Y_%m)-website_backup.tar.gz /path_to_HDD
#+END_SRC

which by executing the local block will produce the desired output. Note: It presumes the use of ssh-agent. If anyone knows how to do this with auth-source please let me know.


Solution

  • You can create another code block that's executed locally, and call scp or whatever from that. Then reference the remote one in the local one. Here's a toy example, where my machine is shannon and the remote one is easybake:

    #+NAME: remote
    #+BEGIN_SRC shell :dir /ssh:easybake:
    hostname
    #+END_SRC
    
    #+RESULTS: remote
    : easybake
    
    #+NAME: local 
    #+BEGIN_SRC shell :var r=remote
    echo $r
    hostname
    #+END_SRC
    
    #+RESULTS: local
    | easybake |
    | shannon  |
    

    You don't need to use the output of remote, the :var directive is what runs it.