How to execute a scp command before reboot or shutdown?
Asked Answered
R

3

9

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.

Rival answered 23/4, 2018 at 15:9 Comment(0)
F
3

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.

Floriated answered 26/4, 2018 at 8:31 Comment(5)
Why changing ExecStart=/bin/true into RemainAfterExit=true take effcet ?Rival
Why can't upload big files?Rival
@it_is_a_literature regarding RemainAfterExit check this answers https://mcmap.net/q/371428/-when-should-the-option-remainafterexit-needs-to-be-set-true-when-creating-new-systemd-services/1135424, did you manage to upload at least a file?Floriated
successfully upload a small file(80kb) into my vps from home pc,why can't big one?Rival
@it_is_a_literature nice, at least first goal achieved, my guess regarding "big files" maybe a timeout, give a try using TimeoutSec=infinityFloriated
K
2

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

Kocher answered 26/4, 2018 at 8:8 Comment(0)
R
2
[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
TimeoutSec=infinity

[Install]
WantedBy=multi-user.target
Rival answered 29/4, 2018 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.