How to execute a scp command before reboot or shutdown?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-30 04:27:31

问题


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.


回答1:


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 foo@10.10.1.14:

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.




回答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

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




回答3:


[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


来源:https://stackoverflow.com/questions/49984368/how-to-execute-a-scp-command-before-reboot-or-shutdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!