Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a scp command before reboot or shutdown?

I want to upload file before reboot or shutdown.
1.From my vps to vps
Setting for upload.service

vim /etc/systemd/system/upload.service
[Unit]
Description=upload files into my vps
Before=shutdown.target reboot.target
Requires=network-online.target
After=network.target

[Service]
ExecStart=/bin/true
ExecStop=/bin/bash /home/upload.sh

[Install]
WantedBy=multi-user.target

Script for upload.sh

vim /home/upload.sh
/usr/bin/scp -P 22 -i /home/.ssh/id_rsa /home/wp.bak root@remote_ip:/home

It is time to test it.

systemctl enable upload
reboot

It is verified that wp.bak can uploaded from my vps1 to vps2 at reboot.
2.From pc at home to vps
ssh credential has been established between my pc at home and vps.
Same setting as case1.

journalctl -u upload
Started upload files into my vps.
ssh: connect to host xxxxxxxxxx port 22: Network is unreachable
lost connection

It is no use to write After=network.target as After=network.target ssh.service.

Do as nbari say.

sudo vim /etc/systemd/system/upload.service
[Unit]
Description=upload files into my vps
Before=shutdown.target reboot.target
After=network.target network-online.target 
Requires=network-online.target network.target    

[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=/bin/bash /home/debian9/upload.sh  

[Install]
WantedBy=multi-user.target


sudo vim  /home/upload.sh 
/usr/bin/scp -P 22 -i /home/.ssh/id_rsa /home/wp.bak root@remote_ip:/home 

sudo systemctl daemon-reload 
sudo systemctl enable upload

Reboot pc.

sudo journalctl -u upload
-- Logs begin at Fri 2018-04-27 10:46:34 HKT, end at Fri 2018-04-27 11:00:23 HKT
Apr 27 10:46:51 hwy systemd[1]: Started upload files into my vps.

It seems that upload service works fine.

issue1: Why

ExecStart=/bin/true
ExecStop=/bin/bash /home/upload.sh

works fine on my vps?

Why

RemainAfterExit=true
ExecStop=/bin/bash /home/upload.sh  

can work instead of

ExecStart=/bin/true
ExecStop=/bin/bash /home/upload.sh

in my home pc?

issue2:

wp.bak is a big file more than 3G.
time /usr/bin/scp -P 22 -i /home/.ssh/id_rsa /home/wp.bak root@remote_ip:/home cost me 3000s (50 minutes,almost one hour) or more.
Get the file size in my pc

ls  -al  /home/wp.bak
-rw-r--r-- 1 debian9 debian9 3977379840 Apr 22 12:23 /home/wp.bak

Get the uploaded file size in my vps.

ssh root@vps_ip
ls  -al  /home/wp.bak
-rw-r--r-- 1 root root 63045632 Apr 27 02:46 /home/wp.bak

Why only 1.6% ,small part of it uploaded?
63045632/3977379840=0.0158
My servant--home computer lied to me.
Please give a explanation in detail.

like image 997
showkey Avatar asked Apr 23 '18 15:04

showkey


People also ask

What is scp command in Windows?

The scp command line utility copies files securely between hosts on a network. It uses Secure Shell sftp subsystem for data transfer, and uses the same authentication and provides the same security as Secure Shell. Scp will ask for passwords or passphrases if they are needed for authentication.


2 Answers

I was available to upload the file using scp before rebooting, using a slightly different configuration for the service using this: /etc/systemd/system/upload.service:

[Unit]
Description=upload files into my vps
Requires=network.target
After=network.target
Before=shutdown.target reboot.target halt.target
DefaultDependencies=no

[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=/bin/bash /root/upload.sh

[Install]
WantedBy=multi-user.target

I had to use RemainAfterExit=true, instead of ExecStart=/bin/true

After creating the service I run systemctl daemon-reload and systemctl enable yourservice

I tested using this in the upload.sh:

scp /root/foo.txt [email protected]:

Previously I setup the ssh-keys to prevent getting prompt for the password/passphrase

The output of journalctl -u upload:

Apr 26 08:35:53 my-vm systemd[1]: Started upload files into my vps.
Apr 26 08:35:53 my-vm systemd[1]: Starting upload files into my vps...

As a fallback you could also use:

upload.sh && reboot

In this case, it will only reboot if your upload script succeed.

like image 131
nbari Avatar answered Oct 19 '22 19:10

nbari


So I created a service file like below

root@vagrant:/etc/systemd/system# cat shutdownscript.service
[Unit]
Description=...
After=network-online.target network.target

[Service]
Type=oneshot
#RemainAfterExit=true
#ExecStart=/bin/true
ExecStart=/bin/bash -c "cd /home/vagrant/ && wget https://download-cf.jetbrains.com/webstorm/WebStorm-2017.2.6.dmg"

[Install]
WantedBy=reboot.target

Then enabled the same like below

systemctl daemon-reload
systemctl enable shutdownscript

And did a reboot. After the reboot

$ ls ~/*.dmg
/home/vagrant/WebStorm-2017.2.6.dmg

File downloaded on Shutdown

PS: The reason I chose downloading file instead of scp was that I didn't have a external server to scp to and I wanted to make sure dns resolution happens. But this should work on scp also

like image 2
Tarun Lalwani Avatar answered Oct 19 '22 17:10

Tarun Lalwani